【未完】Java--线程之间的通信

不带生产者和消费者之间的通信:

 1  
 2 class SynchronizedMethods{
 3     int d;
 4     synchronized void getDate() {
 5         System.out.println("Got data:"+d);
 6     }
 7     synchronized void putdata(int d) {
 8         this.d=d;
 9         System.out.println("Put data:"+d);
10     }
11 }
12 class  Producer extends Thread{
13     SynchronizedMethods t;
14     public Producer (SynchronizedMethods t) {
15         this.t=t;
16     }
17     public void run() {
18         int data=700;
19         while(true ) {
20             System.out.println("Put called by producer");
21             t.putdata(data++);
22         }
23     }
24     
25 }
26 class Consumer extends Thread {
27     SynchronizedMethods t;
28     public Consumer(SynchronizedMethods t) {
29         this.t=t;
30     }
31     public void run() {
32         while(true) {
33             System.out.println("Get called by consumer");
34             t.getDate();
35         }
36     }
37 }
38 public class ProducerConsumer {
39     public static void main(String args[]) {
40         SynchronizedMethods obj1=new SynchronizedMethods();
41         Producer p=new Producer(obj1);
42         Producer c=new Producer(obj1);
43         p.start();
44         c.start();
45     }
46 }
View Code

无限循环

测试结果:

带有生产者和消费真之间的通信:

 1  class SynchronizedMethods{
 2     int d ;
 3     boolean flag=false ;//有无数据,初值为无数据
 4     //由消费者调用
 5     synchronized int getData() {
 6         if(!flag) {//无数据情况下
 7             try {
 8                 wait();//消费者等待
 9             }
10             catch(InterruptedException e) {
11                 System.out.println("Exception caught");
12             }
13         }
14         System.out.println("Got data:"+d);
15         flag =false ;
16         notify();
17         return d;
18     }
19     
20     //生产者
21     synchronized void putData(int d) {
22         if(flag) {//you 数据情况下
23             try {
24                 wait();//生产者等待
25             }
26             catch(InterruptedException e) {
27                 System.out.println("Exception caught");
28             }
29         }
30         this.d=d;//成员变量的值赋值为d
31         flag =true;
32         System.out.println("put data:"+d);
33         notify();//唤醒消费者
34         }
35 }
36     
37 class Producer implements Runnable {    
38     SynchronizedMethods t;
39     public Producer (SynchronizedMethods t) {
40         this.t=t;
41         new Thread (this ,"Producer").start();
42         System.out.println("Put  calledby producer");
43     }
44     public void run() {
45         int data =0;
46         while(true) {
47             data=data+1;
48             t.putData(data);
49         }
50     }
51 }
52 
53 class Consumer implements Runnable {    
54     SynchronizedMethods t;
55     public Consumer(SynchronizedMethods t) {
56         this.t=t;
57         new Thread (this ,"Consumer").start();
58         System.out.println("Put  calledby producer");
59     }
60     public void run() {
61 
62         while(true) {
63             t.getData();
64         }
65     }
66         
67     }
68 
69 public class InterThreadComm{
70         public static void main(String args[]) {
71             SynchronizedMethods obj1=new SynchronizedMethods();
72             Producer p=new Producer(obj1);
73             Consumer c=new Consumer(obj1);
74     }
75 }
View Code

测试结果:

原文地址:https://www.cnblogs.com/Catherinezhilin/p/9056812.html