标签:
参考 http://www.cnblogs.com/dolphin0520/p/3932906.html
package blockthread; import java.util.ArrayList; import java.util.List; import java.util.PriorityQueue; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.PriorityBlockingQueue; public class ThreadBlockingSample { private static int size=10; public static void main(String[] args) throws InterruptedException { // TODO Auto-generated method stub //List<Integer> queue=new ArrayList<Integer>(size);// change the type you can see the difference between ordinary list, non block queue and blocking queue //PriorityQueue<Integer> queue=new PriorityQueue<Integer>(size);//non blocking queue ArrayBlockingQueue<Integer> queue=new ArrayBlockingQueue<Integer>(size); //must specify a size //LinkedBlockingQueue<Integer> queue=new LinkedBlockingQueue<Integer>(); //max size Integer.MAXVAULE //PriorityBlockingQueue<Integer> queue=new PriorityBlockingQueue<Integer>(); // no limit Producer producer=new Producer(queue); Consumer consumer=new Consumer(queue); producer.start(); consumer.start(); } } class Producer extends Thread implements AddInterface{ Object o; public Producer(Object o){ this.o=o; } public void run(){ System.out.println("Producer begins to add..."); while(true){ synchronized(o){ if(o.getClass().getSimpleName().equals("ArrayList")){ List list=(List)o; if(list.size()==10){ try { o.wait(); } catch (InterruptedException e) { // TODO Auto-generated catch block o.notify(); } }else{ add(o,1); o.notify(); } }else{ add(o,1); o.notify(); } } System.out.println("release lock to consumer"); } } @Override public boolean add(Object queue, Integer i) { String queue_type=queue.getClass().getSimpleName(); if(queue_type.equals("ArrayList")){ List q=(List)queue; q.add(i); System.out.println(queue_type+" added 1 and left of size "+q.size()); return true; }else if(queue_type.equals("PriorityQueue")){ PriorityQueue q=(PriorityQueue)queue; q.offer(i); System.out.println(queue_type+" added 1 and left of size "+q.size()); }else if(queue_type.equals("ArrayBlockingQueue")){ ArrayBlockingQueue q=(ArrayBlockingQueue)queue; q.offer(i); System.out.println(queue_type+" added 1 and left of size "+q.size()); }else if(queue_type.equals("LinkedBlockingQueue")){ LinkedBlockingQueue q=(LinkedBlockingQueue)queue; q.offer(i); System.out.println(queue_type+" added 1 and left of size "+q.size()); }else if(queue_type.equals("PriorityBlockingQueue")){ PriorityBlockingQueue q=(PriorityBlockingQueue)queue; q.offer(i); System.out.println(queue_type+" added 1 and left of size "+q.size()); } return false; } } class Consumer extends Thread implements MoveInterface{ Object o; public Consumer(Object o){ this.o=o; } public void run(){ System.out.println("consumer begins to move...."); while(true){ synchronized(o){ if(o.getClass().getSimpleName().equals("ArrayList")){ List list=(List)o; if(list.size()==0){ try { o.wait(); } catch (InterruptedException e) { // TODO Auto-generated catch block o.notify(); } }else{ move(o); o.notify(); } }else{ move(o); o.notify(); } } System.out.println("release lock to producer"); } } @Override public boolean move(Object queue) { String queue_type=queue.getClass().getSimpleName(); if(queue_type.equals("ArrayList")){ List q=(List)queue; q.remove(q.size()-1); System.out.println(queue_type+" moved 1 left size "+q.size()); return true; }else if(queue_type.equals("PriorityQueue")){ PriorityQueue q=(PriorityQueue)queue; q.poll(); System.out.println(queue_type+" moved 1 left size "+q.size()); }else if(queue_type.equals("ArrayBlockingQueue")){ ArrayBlockingQueue q=(ArrayBlockingQueue)queue; q.poll(); System.out.println(queue_type+" moved 1 left size "+q.size()); }else if(queue_type.equals("LinkedBlockingQueue")){ LinkedBlockingQueue q=(LinkedBlockingQueue)queue; q.poll(); System.out.println(queue_type+" moved 1 left size "+q.size()); }else if(queue_type.equals("PriorityBlockingQueue")){ PriorityBlockingQueue q=(PriorityBlockingQueue)queue; q.poll(); System.out.println(queue_type+" moved 1 left size "+q.size()); } return false; } }
标签:
原文地址:http://www.cnblogs.com/IamThat/p/4540401.html