【发布时间】:2014-06-24 03:25:59
【问题描述】:
当您在 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.
}
}
【问题讨论】:
标签: java multithreading this