I am implementing a concurrent circular Queue in terms of an array that makes use of separate locks at the head and the tail of the queue. Each node in the queue looks as follows:
private class Node<T>
{
public T item;
ReentrantLock lock = new ReentrantLock();
Node(){}
void lock() {lock.lock();}
void unlock() {lock.unlock();}
}
I cannot create the queue in the constructor of the queue class.
public Queue(int capacity) {
items = (Node[]) new Object[capacity];//This line gives the problem
head = size = 0;
}
I have found a solution here, but this code:
@SuppressWarnings("unchecked")
Node<T>[] slots = (Node<T>[]) new Node<?>[capacity];
Gives the following compiler error:
Cannot create a generic array of Queue<T>.Node<?>
My question is what is the correct way to initialize an array of generic objects?