注意:对LinkedList的大小没有作限制,但在实际应用过程中该队列的大小应该是要做限制,以免耗尽内存。当对LinkedList的最大元素个数做限制后,实际上就成了一个又固定大小的循环队列。此时要对生产者的行为做一个约定,是覆盖老的数据,还是等待消费者消费掉数据后,又空位置再继续生产。
这里的程序没有对LinkedList大小做限制,代码如下:
点击(此处)折叠或打开
- package ProCon;
- import java.util.LinkedList;
- import java.util.Random;
- import java.util.concurrent.SynchronousQueue;
- import javax.naming.InterruptedNamingException;
- class Worker {
- LinkedList<String> JobQueue = new LinkedList<String>();
-
- Worker() {}
-
- // consumer
- public class Consumer implements Runnable {
- String item = null;
-
- public void run() {
- for ( ; ; ) {
- synchronized (JobQueue) {
- System.out.println("cons get lock");
- while (JobQueue.size() == 0) {
- System.out.println("cons wait . release lock");
- try {
- JobQueue.wait();
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- item = JobQueue.remove();
- System.out.println("consumer get: " + item);
- }
-
- // wait for a while
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
-
- }
- }
-
- // producer
- public class Producer implements Runnable {
- Random r;
- StringBuffer sb = new StringBuffer();
-
- Producer() {
- r = new Random();
- }
- public void run() {
- for ( ; ; ) {
- System.out.println("producer loop ");
- synchronized (JobQueue) {
- System.out.println("producer get lock");
- sb.delete(0, sb.length());
- sb.append("").append(r.nextInt(100));
- System.out.println("put : " + sb.toString());
- JobQueue.add(sb.toString());
- JobQueue.notifyAll();
- }
-
- System.out.println("producer release lock");
- // wait for a while
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- }
-
-
- // work produce
- public void run() {
- Producer p = new Producer();
- Consumer c = new Consumer();
-
- new Thread(p).start();
- new Thread(p).start();
-
- // consumer
- new Thread(c).start();
- new Thread(c).start();
- new Thread(c).start();
- }
- }
- public class TestProCon {
- public static void main(String[] args) throws InterruptedException {
- Worker w = new Worker();
-
- w.run();
- System.out.println("end main");
- Thread.currentThread().join();
- }
- }
点击(此处)折叠或打开
- producer loop
- producer get lock
- Thread-0 put : 56
- producer release lock
- producer loop
- producer get lock
- Thread-1 put : 36
- producer release lock
- cons get lock
- end main
- Thread-2 consumer get: 56
- cons get lock
- Thread-3 consumer get: 36
- cons get lock
- cons wait . release lock
- producer loop
- producer get lock
- Thread-0 put : 98
- Thread-4 consumer get: 98
- producer release lock
- producer loop
- producer get lock
- Thread-1 put : 36
- producer release lock
- cons get lock
- Thread-2 consumer get: 36
- cons get lock
- cons wait . release lock
- cons get lock
- cons wait . release lock
- producer loop
- producer get lock
- Thread-0 put : 15
- producer release lock
- Thread-4 consumer get: 15
- cons wait . release lock
- producer loop
- cons get lock
- cons wait . release lock
- producer get lock
- Thread-1 put : 30
- producer release lock
- Thread-2 consumer get: 30
- cons wait . release lock
- producer loop
- cons get lock
- cons wait . release lock
- producer get lock
- Thread-0 put : 79
- producer release lock
- Thread-4 consumer get: 79
- cons wait . release lock
- producer loop
- cons get lock
- cons wait . release lock
- producer get lock
- Thread-1 put : 13
- producer release lock
- Thread-2 consumer get: 13
- cons wait . release lock
- producer loop
- cons get lock
- cons wait . release lock
- producer get lock
- Thread-0 put : 70
- Thread-4 consumer get: 70
- producer release lock
- cons wait . release lock
- cons get lock
- cons wait . release lock
- producer loop
- producer get lock
- Thread-1 put : 15
- producer release lock
- Thread-2 consumer get: 15
- cons wait . release lock