线程间的通信

线程间通讯:其实就是多个线程在操作同一个资源,但是操作的动作不同

 1 public class MultithreadingCommunication {

 2     public static void main(String[] args) {

 3         Person p = new Person();

 4 

 5         Input in = new Input(p);

 6         Output ou = new Output(p);

 7 

 8         Thread t1 = new Thread(in);

 9         Thread t2 = new Thread(ou);

10 

11         t1.start();

12         t2.start();

13     }

14 }

15 class Person{

16     String name;

17     String sex;

18 }

19 class Input implements Runnable{

20     private Person p;

21     Input(Person p){

22         this.p = p;

23     }

24     public void run(){

25         int x = 0;

26         while(true){

27             //这里锁不能用this 因为不是同一个对象,p是唯一的对象

28             synchronized(p){

29                 if(x==0){

30                     p.name = "mike";

31                     p.sex = "man";

32                 }

33                 else{

34                     p.name = "丽丽";

35                     p.sex = "女女";

36                 }

37                 x = (x+1)%2;

38             }

39         }

40     }

41 }

42 class Output implements Runnable{

43     private Person p;

44     Output(Person p){

45         this.p = p;

46     }

47     public void run(){

48         while(true){

49             synchronized(p){

50                 System.out.println(p.name+".."+p.sex);

51             }

52         }

53     }

54 }

 

输出结果:

mike..man
mike..man
mike..man
mike..man
mike..man
丽丽..女女
丽丽..女女
丽丽..女女

 

等待唤醒机制:

涉及的方法:
wait():让线程处于冻结状态,被wait的线程会被存储到线程池中
notify():唤醒线程池中任意一个线程
notifyAll():唤醒线程池中的所有线程

 这些方法要定义在同步中,因为这些方法是用于操作线程状态的方法.必须要明确到底操作的是哪个锁上的线程

 1 public class Wait_Notify {

 2     public static void main(String[] args) {

 3         Person p = new Person();

 4 

 5         Input in = new Input(p);

 6         Output ou = new Output(p);

 7 

 8         Thread t1 = new Thread(in);

 9         Thread t2 = new Thread(ou);

10 

11         t1.start();

12         t2.start();

13     }

14 }

15 class Person{

16     String name;

17     String sex;

18     boolean flag = false;//定义标记判断有没有数据

19 }

20 class Input implements Runnable{

21     private Person p;

22     Input(Person p){

23         this.p = p;

24     }

25     public void run(){

26         int x = 0;

27         while(true){

28             synchronized(p){

29                 if(p.flag)

30                     try{p.wait();}catch(Exception e){}//标识哪个锁使用wait方法

31                 if(x==0){

32                     p.name = "mike";

33                     p.sex = "man";

34                 }

35                 else{

36                     p.name = "丽丽";

37                     p.sex = "女女";

38                 }

39                 p.flag = true;

40                 p.notify();

41             }

42             x = (x+1)%2;

43         }

44     }

45 }

46 class Output implements Runnable{

47     private Person p;

48     Output(Person p){

49         this.p = p;

50     }

51     public void run(){

52         while(true){

53             synchronized(p){

54                 if(!p.flag)

55                     try{p.wait();}catch(Exception e){}

56                 System.out.println(p.name+".."+p.sex);

57                 p.flag = false;

58                 p.notify();

59             }

60         }

61     }

62 }

 代码优化:

 1 class InputOutputDemo3{

 2     public static void main(String[] args) {

 3         Person p = new Person();

 4 

 5         Input in = new Input(p);

 6         Output ou = new Output(p);

 7 

 8         Thread t1 = new Thread(in);

 9         Thread t2 = new Thread(ou);

10 

11         t1.start();

12         t2.start();

13     }

14 }

15 

16 class Person{

17     private String name;

18     private String sex;

19     private boolean flag = false;

20 

21     public synchronized void set(String name,String sex){

22         if(flag)

23             try{this.wait();}catch(Exception e){}

24         this.name = name;

25         this.sex = sex;

26         flag = true;

27         this.notify();

28     }

29 

30     public synchronized void out(){

31         if(!flag)

32             try{this.wait();}catch(Exception e){}

33         System.out.println(this.name+".."+this.sex);//this可以省略

34             flag = false;

35             this.notify();

36     }

37 

38 }

39   

40 class Input implements Runnable{

41     private Person p;

42     Input(Person p){

43         this.p = p;

44     }

45     public void run(){

46         int x = 0;

47         while(true){

48             if(x==0){

49                 p.set("mike","man")

50             }

51             else{

52                 p.set("丽丽","女人")

53             }

54             x = (x+1)%2;

55         }

56     }

57 }

58 class Output implements Runnable{

59     private Person p;

60     Output(Person p){

61         this.p = p;

62     }

63     public void run(){

64         while(true){

65             p.out();

66         }

67     }

68 }

 

 

 

你可能感兴趣的:(线程)