Java simple thread queue

I am trying to create a simple queue with a Java Thread that would allow a loop, for example for a loop with 10 iterations, to iterate n (<10) threads at a time and until those threads are finished before continuing to iterate.

Here's a better way to illustrate my problem:

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

    if ( no_available_threads ) {

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


Basically what I want to do is a copy of this: Thread and Queue

How can I achieve this most painless path?


source to share

4 answers

I would use Java 5 Executors

instead of rolling around on my own. Something like the following:

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




Use performers as recommended by others. However, if you enjoy doing this, try something like this. (Take care, I wrote it in Notepad and there are some Exceptions you need to catch even if I get everything else okay. Notepad is not good at coding errors.) This is more of a concept than a real solution to anything, but the idea can be generally helpful.

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

class MyThread  extends Thread  {
    public Runnable  doSomething;

    public void run()  {
        // Do the real work.
        // 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 waiting.)
        // (But do that later.  Much later.)
        synchronized (tQueue)  {
            // Tell them the queue is no longer empty.


In the other place:

// 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 not.
        do  {
            // There 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;


Also note the clever use of ConcurrentLinkedQueue. You can use something else like ArrayList or LinkedList, but you need to keep them in sync.



see java.util.concurrent and especially Executors and ExecutorService



Box 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;

    public void start() {
        Runnable task = new Runnable() {
            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 {
            } 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) {



Then paste the following code into your main class:

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




All Articles