I've found answers on how to print out the name of a specific thread when you are in the class Thread
or a class subclassing Thread
.
i.e. this.getName();
However, this isn't working for me when I'm in my class QueueManager<T>
. For example in my method, removeFromQueue()
, I want to print out which thread is pulling from the queue. But when I use this
, it refers to the class QueueManager<T>
and not the current Thread.
How do I refer to the current thread from inside this class?
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.
}
}
The name of the current thread is always given by
Thread.currentThread().getName()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With