【问题标题】:How do print out the name of thread currently running on a specific non-thread class?如何打印出当前在特定非线程类上运行的线程的名称?
【发布时间】: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


    【解决方案1】:

    当前线程的名字总是由下式给出

    Thread.currentThread().getName()
    

    【讨论】:

      猜你喜欢
      • 2013-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-10
      • 2015-10-06
      • 2011-10-16
      • 1970-01-01
      相关资源
      最近更新 更多