需求一
模拟四个窗口同时卖100张火车票的问题
1 package work.thread; 2 3 import java.util.Objects; 4 5 /** 6 * Created by coolkid on 2014/12/17 0017. 7 */ 8 9 class Ticket implements Runnable{ 10 11 //票数 12 private int count = 100; 13 14 private Object object = new Object(); 15 private void saleTicket(){ 16 while (true){ 17 synchronized (object){ 18 if (count > 0){ 19 count--; 20 System.out.println(Thread.currentThread().getName()+" 卖出第 "+(100-count)+" 张票"); 21 }else { 22 break; 23 } 24 } 25 } 26 27 } 28 @Override 29 public void run() { 30 saleTicket(); 31 } 32 } 33 public class Thread01 { 34 public static void main(String[] args) { 35 Ticket t = new Ticket(); 36 Thread t1 = new Thread(t); 37 Thread t2 = new Thread(t); 38 Thread t3 = new Thread(t); 39 Thread t4 = new Thread(t); 40 41 t1.start(); 42 t2.start(); 43 t3.start(); 44 t4.start(); 45 } 46 }
对于卖票的过程,必须使用同步代码段,否则就会出现同一张票被多次售卖的问题
需求二
生产者消费者问题
1 package work.thread; 2 3 4 /** 5 * Created by coolkid on 2014/12/17 0017. 6 */ 7 8 class Resource02{ 9 private String name; 10 private String sex; 11 private int count = 0; 12 13 public synchronized void set(String name,String sex){ 14 while (count>10){ 15 try { 16 this.wait(); 17 } catch (InterruptedException e) { 18 e.printStackTrace(); 19 } 20 } 21 this.name = name; 22 this.sex = sex; 23 count++; 24 this.notifyAll(); 25 } 26 27 public synchronized void out() { 28 while (count<1){ 29 try { 30 this.wait(); 31 } catch (InterruptedException e) { 32 e.printStackTrace(); 33 } 34 } 35 System.out.println(Thread.currentThread().getName()+" name = " + name+" sex = " + sex); 36 count--; 37 this.notifyAll(); 38 } 39 } 40 41 class Input02 implements Runnable{ 42 private Resource02 r; 43 44 public Input02(Resource02 r) { 45 this.r = r; 46 } 47 48 @Override 49 public void run() { 50 int x = 0; 51 while (true){ 52 if (x == 0){ 53 r.set("mike","nan"); 54 }else { 55 r.set("莉莉","女.........."); 56 } 57 x = (x+1)%2; 58 } 59 60 } 61 } 62 63 class Output02 implements Runnable{ 64 private Resource02 r; 65 66 public Output02(Resource02 r) { 67 this.r = r; 68 } 69 70 @Override 71 public void run() { 72 while (true){ 73 r.out(); 74 } 75 } 76 } 77 78 public class Thread02 { 79 public static void main(String[] args) { 80 //资源 81 Resource02 r = new Resource02(); 82 //生产者 83 Input02 i = new Input02(r); 84 //消费者 85 Output02 o = new Output02(r); 86 87 //执行路径 88 Thread t1 = new Thread(i); 89 Thread t2 = new Thread(i); 90 91 Thread t3 = new Thread(o); 92 93 t1.start(); 94 t2.start(); 95 t3.start(); 96 97 } 98 }
需求三
使用监视器实现生产者消费者问题
1 package work.thread; 2 3 4 import java.util.concurrent.locks.Condition; 5 import java.util.concurrent.locks.Lock; 6 import java.util.concurrent.locks.ReentrantLock; 7 8 /** 9 * Created by coolkid on 2014/12/17 0017. 10 */ 11 12 class Resource02{ 13 private String name; 14 private String sex; 15 private int count = 0; 16 17 Lock lock = new ReentrantLock(); 18 Condition pro_con = lock.newCondition(); 19 Condition con_con = lock.newCondition(); 20 21 public void set(String name,String sex){ 22 lock.lock(); 23 try { 24 while (count>10){ 25 pro_con.await(); 26 } 27 this.name = name; 28 this.sex = sex; 29 count++; 30 con_con.signal(); 31 } catch (InterruptedException e) { 32 e.printStackTrace(); 33 } finally { 34 lock.unlock(); 35 } 36 } 37 38 public void out() { 39 lock.lock(); 40 try { 41 while (count<1){ 42 con_con.await(); 43 } 44 System.out.println(Thread.currentThread().getName()+" name = " + name+" sex = " + sex); 45 count--; 46 pro_con.signal(); 47 } catch (InterruptedException e) { 48 e.printStackTrace(); 49 } finally { 50 lock.unlock(); 51 } 52 } 53 } 54 55 class Input02 implements Runnable{ 56 private Resource02 r; 57 58 public Input02(Resource02 r) { 59 this.r = r; 60 } 61 62 @Override 63 public void run() { 64 int x = 0; 65 while (true){ 66 if (x == 0){ 67 r.set("mike","nan"); 68 }else { 69 r.set("莉莉","女.........."); 70 } 71 x = (x+1)%2; 72 } 73 74 } 75 } 76 77 class Output02 implements Runnable{ 78 private Resource02 r; 79 80 public Output02(Resource02 r) { 81 this.r = r; 82 } 83 84 @Override 85 public void run() { 86 while (true){ 87 r.out(); 88 } 89 } 90 } 91 92 public class Thread02 { 93 public static void main(String[] args) { 94 //资源 95 Resource02 r = new Resource02(); 96 //生产者 97 Input02 i = new Input02(r); 98 //消费者 99 Output02 o = new Output02(r); 100 101 //执行路径 102 Thread t1 = new Thread(i); 103 Thread t2 = new Thread(i); 104 105 Thread t3 = new Thread(o); 106 107 t1.start(); 108 t2.start(); 109 t3.start(); 110 111 } 112 }