Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do print out the name of thread currently running on a specific non-thread class?

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.
        }
    }
like image 253
CodyBugstein Avatar asked Jun 24 '14 03:06

CodyBugstein


1 Answers

The name of the current thread is always given by

Thread.currentThread().getName()
like image 169
Dawood ibn Kareem Avatar answered Sep 19 '22 01:09

Dawood ibn Kareem