Java线程简单队列

时间:2012-03-28 21:58:35

标签: java multithreading queue

我正在尝试使用Java Thread创建一个简单的队列,该队列允许一个循环,例如一个10次迭代的for循环,一次迭代n(< 10)个线程并等到这些线程完成后再继续迭代。

这是一个更好的方式来说明我的问题:

for (int i = 1; i <= 10; i++) {
    new Thread ( do_some_work() );

    if ( no_available_threads ) {
        wait_until_available_threads();
    }
}

do_some_work() {
    // do something that takes a long time
}

基本上我想做的是这个的副本:Thread and Queue

我怎样才能以最无痛的方式实现这一目标?

4 个答案:

答案 0 :(得分:11)

我会使用Java 5 Executors而不是自己滚动。如下所示:

ExecutorService service = Executors.newFixedThreadPool(10);
// now submit our jobs
service.submit(new Runnable() {
    public void run() {
        do_some_work();
    }
});
// you can submit any number of jobs and the 10 threads will work on them
// in order
...
// when no more to submit, call shutdown
service.shutdown();
// now wait for the jobs to finish
service.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);

答案 1 :(得分:2)

按照其他人的建议使用执行程序。但是,如果你想要自己做的乐趣,尝试这样的事情。 (小心。我在记事本中写了它,即使我把其他一切都搞定了,你也需要抓住一些例外。记事本在捕捉编码错误方面很差。)这是一个概念而不是实际的解决方案,但这个想法通常很有用。

private ConcurrentLinkedQueue<MyThread>  tQueue =
             new ConcurrentLinkedQueue<MyThread>();

class MyThread  extends Thread  {
    public Runnable  doSomething;

    public void run()  {
        // Do the real work.
        doSomething();
        // Clean up and make MyThread available again.
        tQueue.add( mythread );
        // Might be able to avoid this synch with clever code.
        // (Don't synch if you know no one's waiting.)
        // (But do that later.  Much later.)
        synchronized (tQueue)  {
            // Tell them the queue is no longer empty.
            tQueue.notifyAll();
        }
    }
}

其他地方:

// Put ten MyThreads in tQueue.
for (int i = 0; i < 10; i++)  tQueue.add( new MyThread() );

// Main Loop.  Runs ten threads endlessly.
for (;;)  {
    MyThread  t = tQueue.poll();
    if (t == null)  {
        // Queue empty.  Sleep till someone tells us it's not.
        do  {
            // There's a try-catch combo missing here.
            synchonized( tQueue )  { tQueue.wait() };
            t = tQueue.poll();
        }  while (t == null)  break;  // Watch for fake alert!
    }
    t.doSomething = do_some_work;
    t.start();
}

另外,请注意巧妙地使用ConcurrentLinkedQueue。您可以使用其他类似ArrayList或LinkedList的东西,但您需要同步它们。

答案 2 :(得分:0)

请参阅java.util.concurrent,尤其是Executors和ExecutorService

答案 3 :(得分:0)

Crate Logger.class

public class Logger extends Thread {
    List<String> queue = new ArrayList<String>();
    private final int MAX_QUEUE_SIZE = 20;
    private final int MAX_THREAD_COUNT = 10;

    @Override
    public void start() {
        super.start();
        Runnable task = new Runnable() {
            @Override
            public void run() {
                while (true) {
                    String message = pullMessage();
                    Log.d(Thread.currentThread().getName(), message);
                    // Do another processing
                }
            }
        };
        // Create a Group of Threads for processing
        for (int i = 0; i < MAX_THREAD_COUNT; i++) {
            new Thread(task).start();
        }
    }

    // Pulls a message from the queue
    // Only returns when a new message is retrieves
    // from the queue.
    private synchronized String pullMessage() {
        while (queue.isEmpty()) {
            try {
                wait();
            } catch (InterruptedException e) {
            }
        }
        return queue.remove(0);
    }

    // Push a new message to the tail of the queue if
    // the queue has available positions
    public synchronized void pushMessage(String logMsg) {
        if (queue.size() < MAX_QUEUE_SIZE) {
            queue.add(logMsg);
            notifyAll();
        }

    }
}

然后在主类中插入以下代码:

Logger logger =new Logger();
logger.start();
for ( int i=0; i< 10 ; i++) {
    logger.pushMessage(" DATE : "+"Log Message #"+i);
}