[concurrency-interest] Strange behaviour with LinkedBlockingQueue JDK 1.4 backport

Kris Schneider kris@dotech.com
Mon, 13 Dec 2004 12:46:24 -0500


This seems to work fine on my single-CPU WinXP laptop:

import edu.emory.mathcs.backport.java.util.concurrent.*;
import java.util.*;

public class QueueTest {

    private static final int CAPACITY = 10;
    private static final LinkedBlockingQueue Q = new
LinkedBlockingQueue(CAPACITY);

    private static class Producer implements Runnable {
        public void run() {
            while (true) {
                try {
                    Q.put(new Object());
                    Thread.yield();
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                }
            }
        }
    }

    private static class Consumer implements Runnable {
        private static final int MAX = 50000;
        public void run() {
            long t1 = System.currentTimeMillis();
            List list = new ArrayList();
            int i = 0;
            while (i < MAX) {
                i += Q.drainTo(list);
                list.clear();
                Thread.yield();
            }
            long t2 = System.currentTimeMillis();
            System.out.println("time = " + (t2 - t1) + "ms");
        }
    }

    public static void main(String[] args) {
        Thread c = new Thread(new Consumer());
        c.start();

        Thread p = new Thread(new Producer());
        p.setDaemon(true);
        p.start();
    }
}

Quoting Jean Morissette <jean.morissette666@videotron.ca>:

> Hi,
> Running this test on 
> edu.emory.mathcs.backport.java.util.concurrent.LinkedBlockingQueue cause 
> the Producer and Consumer threads to block indefinitely.  Am I missing 
> something?

8< snip >8

-- 
Kris Schneider <mailto:kris@dotech.com>
D.O.Tech       <http://www.dotech.com/>