我发现了如何在类
例如:
如何从该类内部引用当前线程?
Thread
或继承Thread
的子类中打印特定线程名称的答案。例如:
this.getName();
然而,当我在我的类QueueManager<T>
中时,这对我不起作用。例如,在我的方法removeFromQueue()
中,我想打印正在从队列中取出的线程。但是当我使用this
时,它指的是类QueueManager<T>
而不是当前线程。如何从该类内部引用当前线程?
public T removeFromQueue(){
synchronized (this) {
T item = null;
if (!isEmpty()){
item = queue.removeLast();
if (WebServer.DEBUG) System.out.println(item + " removed from " + item.getClass() + "Queue" + "\nby " + this.);
//If queue was full until right now, notify waiting socket threads that they can add something
if (getSize() == (maxQueueSize - 1)){
notifyAll();
}
}
return item; //If queue is empty, null is returned.
}
}