码迷,mamicode.com
首页 > 其他好文 > 详细

非阻塞队列 普通队列 阻塞队列 学习笔记

时间:2015-05-30 16:25:15      阅读:115      评论:0      收藏:0      [点我收藏+]

标签:

参考   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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!