当一个线程使用的同步方法中用到某个变量,而此变量有需要其他线程修改后才能符合本线程的需要,
那么可以在同步方法中使用wait(),wait方法可以中断线程的执行,使本线程等待,暂时让出CPU的使用权,并允许其他线程使用这个同步方法。
其他线程如果在使用这个同步方法时,不许需要等待,那么它使用这个同步方法。其他线程如果再使用这个同步方法是不需要等待,那么它使用完
这个同步方法的同时,应当用notifyAll()方法通知所有由于使用这个同步方法而处于等待的线程结束等待.曾中断的线程就会从刚才的中断处继续执行
这个同步方法.....
1 /*
2 wait();
3 notify();
4 notifyAll();
5 */
在下面的列子中:
/*
张飞和李逵买电影票。售票员只有两张5元的钱,电影票5元一张。张飞
拿20元一张的人民币排在李逵的前面买票,李逵拿一张5元的人民币买票,因此张飞必须等待。
*/
1 //package Scan_boobs;
2 public class Example12_8 extends window_scan
3 {
4 public static void main(String args [] )
5 {
6 TicketHouse officer = new TicketHouse();
7 Thread zhangfei ,likui;
8 zhangfei = new Thread(officer);
9 zhangfei.setName("张飞");
10 likui = new Thread(officer);
11 likui.setName("李逵");
12 zhangfei.start();
13 likui.start();
14 }
15 }
16
17 class TicketHouse implements Runnable
18 {
19 int fiveAmount=2,tenAmount=0,twentyAmount=0;
20 @Override
21 public void run() {
22 // TODO Auto-generated method stub
23 String name=Thread.currentThread().getName();
24 if(name.equals("张飞")) saleTicket(20);
25 else saleTicket(5);
26
27 }
28 private synchronized void saleTicket(int money)
29 {
30 if(money==5)
31 {
32 fiveAmount++;
33 this.out("给"+Thread.currentThread().getName()+"入场卷 ,"
34 +Thread.currentThread().getName()+"的钱正好");
35 }
36 else if(money==20)
37 {
38 while(fiveAmount<3)
39 {
40 try {
41 this.out("\n"+Thread.currentThread().getName()+"靠边等....");
42 wait();
43 // Thread.sleep(3000);
44 this.out("\n"+Thread.currentThread().getName()+"继续买票");
45 } catch (InterruptedException e) {
46 // TODO Auto-generated catch block
47 e.printStackTrace();
48 }
49 }
50 fiveAmount-=3;
51 twentyAmount++;
52 this.out("给"+Thread.currentThread().getName()+"入场卷,"
53 +Thread.currentThread().getName()+"给20,找赎15元");
54 }
55 notifyAll();
56 }
57 private void out(String name)
58 {
59 System.out.println(name);
60 }
61 }
需要特别注意:
/*
在许多实际的问题中wait方法应当放在一个"while(等待的条件){}"的循环语句中,而不是“if(等待条件){}的分支语句中”
*/
如果咸的蛋疼了,将wait(); ----》改为 Thread.sleep(); 然后呵呵,你会就会这样
代码:
1 package Scan_boobs;
2 public class Example12_8 extends window_scan
3 {
4 public static void main(String args [] )
5 {
6 TicketHouse officer = new TicketHouse();
7 Thread zhangfei ,likui;
8 zhangfei = new Thread(officer);
9 zhangfei.setName("张飞");
10 likui = new Thread(officer);
11 likui.setName("李逵");
12 zhangfei.start();
13 likui.start();
14 }
15 }
16
17 class TicketHouse implements Runnable
18 {
19 int fiveAmount=2,tenAmount=0,twentyAmount=0;
20 @Override
21 public void run() {
22 // TODO Auto-generated method stub
23 String name=Thread.currentThread().getName();
24 if(name.equals("张飞")) saleTicket(20);
25 else saleTicket(5);
26
27 }
28 private synchronized void saleTicket(int money)
29 {
30 if(money==5)
31 {
32 fiveAmount++;
33 this.out("给"+Thread.currentThread().getName()+"入场卷 ,"
34 +Thread.currentThread().getName()+"的钱正好");
35 }
36 else if(money==20)
37 {
38 while(fiveAmount<3)
39 {
40 try {
41 this.out("\n"+Thread.currentThread().getName()+"靠边等....");
42 wait();
43 // Thread.sleep(3000);
44 this.out("\n"+Thread.currentThread().getName()+"继续买票");
45 } catch (InterruptedException e) {
46 // TODO Auto-generated catch block
47 e.printStackTrace();
48 }
49 }
50 fiveAmount-=3;
51 twentyAmount++;
52 this.out("给"+Thread.currentThread().getName()+"入场卷,"
53 +Thread.currentThread().getName()+"给20,找赎15元");
54 }
55 notifyAll();
56 }
57 private void out(String name)
58 {
59 System.out.println(name);
60 }
61 }
效果图: