ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

多线程--两个线程间的通讯

2019-08-15 12:52:49  阅读:170  来源: 互联网

标签:通讯 void System flag 线程 print 多线程 public out


  CPU任意切换线程,如果希望 CPU能够有规律的执行,就需要线程间通讯:

this.wait()          当前线程等待

this.notify()        随机唤醒单个线程

this.notifyAll()    唤醒所有线程

public class demon1_notify {
    //等待唤醒机制
    public static void main(String[] args) {
        final printer2 p = new printer2();
        new Thread(){
            public void run() {
                while(true){
                    try {
                        p.print1();
                    } catch (InterruptedException e) {
                        
                        e.printStackTrace();
                    }
                }
            }
        }.start();
        new Thread(){
            public void run() { 
                while(true){
                    try {
                        p.print2();
                    } catch (InterruptedException e) {
                        
                        e.printStackTrace();
                    }
                }
            }
        }.start();
    }
}
class printer2{
    private int flag = 1;
    public synchronized void print1() throws InterruptedException{    
        if (flag !=1) {
            this.wait();
        }
        System.out.print("黑");
        System.out.print("马");
        System.out.print("程");
        System.out.print("序");
        System.out.print("员");
        System.out.print("\r\n");
        flag = 2;    
        this.notify();
    }
    public synchronized void print2() throws InterruptedException{    
        if (flag !=2) {
            this.wait();
        }
        System.out.print("传");
        System.out.print("智");
        System.out.print("播");
        System.out.print("客");
        System.out.print("\r\n");
        flag = 1;    
        this.notify();
    }
}

 

 

public class demon2_notify2 {

    public static void main(String[] args) {
        final printer3 p = new printer3();
        new Thread(){
            public void run() {
                while(true){
                    try {
                        p.print1();
                    } catch (InterruptedException e) {
                        
                        e.printStackTrace();
                    }
                }
            }
        }.start();
        new Thread(){
            public void run() { 
                while(true){
                    try {
                        p.print2();
                    } catch (InterruptedException e) {
                        
                        e.printStackTrace();
                    }
                }
            }
        }.start();
        new Thread(){
            public void run() {
                while(true){
                    try {
                        p.print3();
                    } catch (InterruptedException e) {
                        
                        e.printStackTrace();
                    }
                }
            }
        }.start();
    }
}
class printer3{
    private int flag = 1;
    public synchronized void print1() throws InterruptedException{    
        while (flag !=1) {
            this.wait();
        }
        System.out.print("黑");
        System.out.print("马");
        System.out.print("程");
        System.out.print("序");
        System.out.print("员");
        System.out.print("\r\n");
        flag = 2;    
        //this.notify();
        this.notifyAll();
    }
    public synchronized void print2() throws InterruptedException{    
        while (flag !=2) {
            this.wait();
        }
        System.out.print("传");
        System.out.print("智");
        System.out.print("播");
        System.out.print("客");
        System.out.print("\r\n");
        flag = 3;    
        this.notifyAll();
    }
    public synchronized void print3() throws InterruptedException{    
        while (flag !=3) {
            this.wait();
        }
        System.out.print("i");
        System.out.print("t");
        System.out.print("h");
        System.out.print("m");
        System.out.print("\r\n");
        flag = 1;    
        this.notifyAll();
    }
}

 

 

JDK5之前无法唤醒指定的一个线程,1.5.之后   有互斥锁    ,

  同步
    * 使用ReentrantLock类的lock()和unlock()方法进行同步

  通信

    * 使用ReentrantLock类的newCondition()方法可以获取Condition对象
    * 需要等待的时候使用Condition的await()方法, 唤醒的时候用signal()方法
    * 不同的线程使用不同的Condition, 这样就能区分唤醒的时候找哪个线程了

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

import javax.print.attribute.standard.Sides;

public class demon3_ReentrantLock {

    public static void main(String[] args) {
        final printer4 p4 = new printer4();
        new Thread(){
            public void run() {
                while(true){
                    try {
                        p4.print1();
                    } catch (InterruptedException e) {
                        
                        e.printStackTrace();
                    }
                }
            }
        }.start();
        new Thread(){
            public void run() { 
                while(true){
                    try {
                        p4.print2();
                    } catch (InterruptedException e) {
                        
                        e.printStackTrace();
                    }
                }
            }
        }.start();
        new Thread(){
            public void run() {
                while(true){
                    try {
                        p4.print3();
                    } catch (InterruptedException e) {
                        
                        e.printStackTrace();
                    }
                }
            }
        }.start();
    }
}
class printer4{
    private ReentrantLock r = new ReentrantLock();   // 获取锁
    private Condition c1 = r.newCondition();
    private Condition c2 = r.newCondition();
    private Condition c3 = r.newCondition();
    private int flag = 1;
    public  void print1() throws InterruptedException{
        r.lock();           //唤醒锁
        if (flag !=1) {
            c1.await();
        }
        System.out.print("黑");
        System.out.print("马");
        System.out.print("程");
        System.out.print("序");
        System.out.print("员");
        System.out.print("\r\n");
        flag = 2;    
        c2.signal();           // 唤醒C2
        
        r.unlock();       //   释放锁
    }
    public  void print2() throws InterruptedException{    
        r.lock();
        if (flag !=2) {
            c2.await();
        }
        System.out.print("传");
        System.out.print("智");
        System.out.print("播");
        System.out.print("客");
        System.out.print("\r\n");
        flag = 3;    
        c3.signal();
        r.unlock();
    }
    public  void print3() throws InterruptedException{    
        r.lock();
        if (flag !=3) {
            c3.await();
        }
        System.out.print("i");
        System.out.print("t");
        System.out.print("h");
        System.out.print("m");
        System.out.print("\r\n");
        flag = 1;    
        c1.signal();
        r.unlock();
    }
}

 

标签:通讯,void,System,flag,线程,print,多线程,public,out
来源: https://www.cnblogs.com/yaobiluo/p/11357299.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有