Cod*_*ein 3 java multithreading this
我在类Thread或类子类中找到了如何打印出特定线程名称的答案Thread.
即 this.getName();
但是,当我上课时,这对我不起作用QueueManager<T>.例如,在我的方法中removeFromQueue(),我想打印出从队列中拉出的线程.但是当我使用时this,它指的是类,QueueManager<T>而不是当前的Thread.
如何引用此类中的当前线程?
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.
}
}
Run Code Online (Sandbox Code Playgroud)