我找到了关于如何在类里面打印出特定线程名称的答案 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.
}
}
最佳答案
当前线程的名字总是由
Thread.currentThread().getName()
关于java - 如何打印出当前在特定非线程类上运行的线程名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24377867/