【问题标题】:Java ThreadPool reportingJava 线程池报告
【发布时间】:2014-03-21 21:57:34
【问题描述】:

我设置了一个工作线程池,它执行一些我想在中心位置登录的工作。

更准确地说,我已经将 Thread 类扩展为一个 worker 类,它检查并发队列的状态。如果它是空的,那么它会等待。当元素被另一个线程添加时, notify() 唤醒工作人员。一旦他们完成了任务,他们就会等待队列中的下一个元素。

让每个线程在每个任务结束时报告其状态的最佳做法是什么?

public class PoolWorker extends Thread {

public ConcurrentLinkedQueue<Device> q;

public PoolWorker(ConcurrentLinkedQueue<Device> q, String type){
    this.q = q;         
    this.type = type;
}

@Override 
public void run(){
    while (true)
    {
        Device d = null;
        try{
            synchronized(q){
                while(q.isEmpty())
                {
                    q.wait(); // wait for a notify()
                }
                d = q.remove();

            }
                            // do some work
                            // report status of work completed 
             }
}

【问题讨论】:

  • 您能发布您的代码以便我们为您提供帮助吗?您能否更准确地解释一下“报告他们的状态”?
  • 为什么不使用BlockingQueue?
  • 另外,“让每个线程在每个任务结束时报告其状态的最佳做法是什么?”
  • 您应该使用ExecutorService 类而不是自己的池。您可以提交某种包装的可运行文件,以便在每个作业完成时,包装的可运行文件记录一些内容。

标签: java multithreading concurrency java.util.concurrent


【解决方案1】:

尝试做这样的事情

ExecutorService exec = Executors.newFixedThreadPool(10);
    Runnable runn = new Runnable() 
    {
        @Override
        public void run() 
        {
            System.out.println("");
        }
    };
    exec.execute(runn);

【讨论】:

    【解决方案2】:

    如前所述,最好的方法是使用 BlockingQueue。下面是示例代码:

    public class PoolWorker extends Thread {
        public ArrayBlockingQueue<String> q;
        public String type;
    
        public PoolWorker(ArrayBlockingQueue<String> q, String type) {
            this.q = q;
            this.type = type;
        }
    
        @Override
        public void run() {
            while(true){
                String work = null;
                try {
                    System.out.println("PoolWorker.run:waiting .............");
                    work = q.take();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("PoolWorker.run..work: " + work);
            }
    
        }
    
        public static void main(String[] args) throws InterruptedException {
            ArrayBlockingQueue<String> pool = new ArrayBlockingQueue<String>(100);
            PoolWorker worker = new PoolWorker(pool, "Something");
    
            worker.start();
            addWork(pool, "work1");
            addWork(pool, "work2");
            addWork(pool, "work3");
            addWork(pool, "work4");
            addWork(pool, "work5");
            //Just give enough time to run
            Thread.sleep(5000);
        }
    
    
        private static void addWork(ArrayBlockingQueue<String> pool, String work) throws InterruptedException {
            System.out.println("PoolWorker.addWork: " + work);
            pool.put(work);
        }
    }
    

    Java 文档中也有很好的示例代码: http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/BlockingQueue.html

    【讨论】:

      猜你喜欢
      • 2013-04-19
      • 2016-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-27
      • 2017-12-29
      • 2011-11-16
      相关资源
      最近更新 更多