JAVA多线程编程学习总结
package com.taobao.concurrency;public class WaitTest { public static String a = "";// 作为监视器对象 public static void main(String[] args) throws InterruptedException { WaitTest wa = new WaitTest(); TestTask task = wa.new TestTask(); Thread t = new Thread(task); t.start(); Thread.sleep(12000); for (int i = 5; i > 0; i--) { System.out.println("快唤醒挂起的线程************"); Thread.sleep(1000); } System.out.println("收到,马上!唤醒挂起的线程************"); synchronized (a) { a.notifyAll(); } } class TestTask implements Runnable { @Override public void run() { synchronized (a) { try { for (int i = 10; i > 0; i--) { Thread.sleep(1000); System.out.println("我在运行 ***************"); } a.wait(); for (int i = 10; i > 0; i--) { System.out.println("谢谢唤醒**********又开始运行了*******"); } } catch (InterruptedException e) { e.printStackTrace(); } } } }}package com.taobao.concurrency;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.TimeUnit;class Meal { private final int orderNum; public Meal(int orderNum) { this.orderNum = orderNum; } public String toString() { return "Meal " + orderNum; }}class WaitPerson implements Runnable { private Restaurant restaurant; public WaitPerson(Restaurant r) { this.restaurant = r; } @Override public void run() { try { while (!Thread.interrupted()) { synchronized (this) { while (restaurant.meal == null) wait();// ..for the chef to produce a meal } System.out.println("WaitPerson got" + restaurant.meal); synchronized (restaurant.chef) { restaurant.meal = null; restaurant.chef.notifyAll();// ready for another } } TimeUnit.MICROSECONDS.sleep(100); } catch (InterruptedException e) { System.out.println("WaitPerson interrupted"); } }}class Chef implements Runnable { private Restaurant restaurant; private int count = 0; public Chef(Restaurant r) { this.restaurant = r; } @Override public void run() { try { while (!Thread.interrupted()) { synchronized (this) { while (restaurant.meal != null) wait();// ...for the meal to be taken } if (++count == 10) { System.out.println("Out of food,closing"); restaurant.exec.shutdownNow(); } System.out.println("Order up!"); synchronized (restaurant.waitPerson) { restaurant.meal = new Meal(count); restaurant.waitPerson.notifyAll(); } } } catch (InterruptedException e) { } }}public class Restaurant { Meal meal; ExecutorService exec = Executors.newCachedThreadPool(); WaitPerson waitPerson = new WaitPerson(this); Chef chef = new Chef(this); public Restaurant() { exec.execute(chef); exec.execute(waitPerson); } public static void main(String[] args) { new Restaurant(); }}package com.taobao.concurrency;import java.util.concurrent.ArrayBlockingQueue;import java.util.concurrent.BlockingQueue;public class TestBlockingQueues { public static void main(String[] args) { BlockingQueue<String> queue = new ArrayBlockingQueue<String>(20); Thread pro = new Thread(new Producer(queue), "生产者"); pro.start(); for (int i = 0; i < 10; i++) { Thread t = new Thread(new Concumer(queue), "消费者 " + i); t.start(); } }}class Producer implements Runnable { BlockingQueue<String> queue; public Producer(BlockingQueue<String> queue) { this.queue = queue; } @Override public void run() { int i = 0; while (true) { try { System.out.println("生产者生产食物, 食物编号为:" + i); queue.put(" 食物 " + i++); Thread.sleep(1000); } catch (InterruptedException e) { System.out.println("生产者被中断"); } } }}class Concumer implements Runnable { BlockingQueue<String> queue; public Concumer(BlockingQueue<String> queue) { this.queue = queue; } @Override public void run() { while (true) { try { System.out.println(Thread.currentThread().getName() + "消费:" + queue.take()); } catch (InterruptedException e) { System.out.println("消费者被中断"); } } }}执行结果:消费者 0 请求消费
消费者 2 请求消费
消费者 4 请求消费
消费者 6 请求消费
消费者 8 请求消费
消费者 5 请求消费
生产者生产食物, 食物编号为:0
消费者 0消费: 食物 0
消费者 1 请求消费
消费者 3 请求消费
消费者 7 请求消费
消费者 9 请求消费
消费者 0 请求消费
生产者生产食物, 食物编号为:1
消费者 2消费: 食物 1
消费者 2 请求消费
生产者生产食物, 食物编号为:2
消费者 4消费: 食物 2
消费者 4 请求消费
生产者生产食物, 食物编号为:3
消费者 6消费: 食物 3
消费者 6 请求消费
生产者生产食物, 食物编号为:4
消费者 8消费: 食物 4
消费者 8 请求消费
生产者生产食物, 食物编号为:5
消费者 5消费: 食物 5
消费者 5 请求消费
生产者生产食物, 食物编号为:6
消费者 1消费: 食物 6
消费者 1 请求消费
生产者生产食物, 食物编号为:7
消费者 3消费: 食物 7
消费者 3 请求消费
生产者生产食物, 食物编号为:8
消费者 7消费: 食物 8
消费者 7 请求消费
生产者生产食物, 食物编号为:9
消费者 9消费: 食物 9
消费者 9 请求消费
package com.taobao.concurrency;import java.util.concurrent.ArrayBlockingQueue;import java.util.concurrent.BlockingQueue;public class TestBlockingQueues { public static void main(String[] args) { BlockingQueue<String> queue = new ArrayBlockingQueue<String>(20); for (int i = 0; i < 10; i++) { Thread pro = new Thread(new Producer(queue), "生产者" + i); pro.start(); } for (int i = 0; i < 10; i++) { Thread t = new Thread(new Concumer(queue), "消费者 " + i); t.start(); } }}class Producer implements Runnable { BlockingQueue<String> queue; public Producer(BlockingQueue<String> queue) { this.queue = queue; } @Override public void run() { int i = 0; while (true) { try { System.out.println(Thread.currentThread().getName() + "生产食物, 食物编号为:" + Thread.currentThread().getName() + i); queue.put(" 食物 " + Thread.currentThread().getName() + i++); Thread.sleep(10000); } catch (InterruptedException e) { System.out.println("生产者被中断"); } } }}class Concumer implements Runnable { BlockingQueue<String> queue; public Concumer(BlockingQueue<String> queue) { this.queue = queue; } @Override public void run() { while (true) { System.out.println(Thread.currentThread().getName() + " 请求消费"); try { System.out.println(Thread.currentThread().getName() + "消费:" + queue.take()); Thread.sleep(100); } catch (InterruptedException e) { System.out.println("消费者被中断"); } } }}生产者0生产食物, 食物编号为:生产者00生产者2生产食物, 食物编号为:生产者20生产者1生产食物, 食物编号为:生产者10生产者3生产食物, 食物编号为:生产者30生产者4生产食物, 食物编号为:生产者40生产者6生产食物, 食物编号为:生产者60生产者8生产食物, 食物编号为:生产者80生产者5生产食物, 食物编号为:生产者50生产者7生产食物, 食物编号为:生产者70生产者9生产食物, 食物编号为:生产者90消费者 0 请求消费消费者 0消费: 食物 生产者00消费者 2 请求消费消费者 2消费: 食物 生产者20消费者 1 请求消费消费者 1消费: 食物 生产者10消费者 4 请求消费消费者 4消费: 食物 生产者30消费者 3 请求消费消费者 6 请求消费消费者 6消费: 食物 生产者40消费者 3消费: 食物 生产者60消费者 8 请求消费消费者 8消费: 食物 生产者80消费者 5 请求消费消费者 5消费: 食物 生产者50消费者 7 请求消费消费者 7消费: 食物 生产者70消费者 9 请求消费消费者 9消费: 食物 生产者90消费者 0 请求消费消费者 1 请求消费消费者 2 请求消费消费者 4 请求消费消费者 3 请求消费消费者 5 请求消费消费者 7 请求消费消费者 9 请求消费消费者 6 请求消费消费者 8 请求消费生产者0生产食物, 食物编号为:生产者01消费者 0消费: 食物 生产者01生产者2生产食物, 食物编号为:生产者21生产者4生产食物, 食物编号为:生产者41消费者 1消费: 食物 生产者21生产者1生产食物, 食物编号为:生产者11消费者 2消费: 食物 生产者41消费者 4消费: 食物 生产者11生产者3生产食物, 食物编号为:生产者31基于条件的:多线程情况下,某个条件在某个时刻为假,不代表一直为假,可能到某个时刻就好了!
Lock 使用的默认 为非公平锁;condition对象继承了与之相关的锁的共平性特性,如果是公平的锁,线程会依照FIFO的顺序从Condition.wait中被释放;ArrayBlockingQueue中有一个比较不好的地方,生产者每次生产完之后,都要通知消费者,至于有没有性能损失TODO