zoukankan      html  css  js  c++  java
  • 线程通讯三种方式

    关于线程的通讯:有三种方式

    1#synchronized实现1a2b3c交替执行

    public class Test {
        static Thread t1=null,t2=null;
        public static void main(String[] args) {
            //1a2b3c4d交替执行
            final Object o = new Object();
            char[] c1="1234".toCharArray();
            char[] c2="abcd".toCharArray();
            //线程t1
            t1=new Thread(()->{
                synchronized (o){
                    for (char c:c1){
                        System.out.println(c);
                        try {
                            o.notify();
                            Thread.sleep(1000);//休眠1s
                            o.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    o.notify();
                }
            },"t1");
            //线程t2
            t2=new Thread(()->{
                synchronized (o){
                    for (char c:c2){
                        System.out.println(c);
                        try {
                            o.notify();
                            Thread.sleep(1000);
                            o.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    o.notify();
                }
            },"t2");
            t1.start();
            t2.start();
        }
    }

    2#使用volatile关键字实现通信

    public class Test2 {
        //定义一个共享变量实现通信
        static volatile boolean notice=false;
        static Thread t1=null,t2=null;
        public static void main(String[] args) {
            char[] c1="1234".toCharArray();
            char[] c2="abcd".toCharArray();
            t1=new Thread(()->{
                System.out.println("线程t1开始执行");
                for (char c:c1){
                    System.out.println(c);
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    while (c=='4'){
                        notice=true;
                        break;
                    }
                }
            });
            t2=new Thread(()->{
                while (true){
                    if (notice){
                        System.out.println("线程t2收到通知,开始执行");
                        for (char c:c2){
                            System.out.println(c);
                            try {
                                Thread.sleep(1000);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                        break;
                    }
                }
            });
            t2.start();
            t1.start();
        }
    }

    3#使用Condition控制线程通信

    public class Test3 {
        static Thread t1=null,t2=null;
        public static void main(String[] args) {
            //1a2b3c4d交替执行
            char[] c1="1234".toCharArray();
            char[] c2="abcd".toCharArray();
            ReentrantLock lock = new ReentrantLock();
            Condition condition = lock.newCondition();
            //线程t1
            t1=new Thread(()->{
                lock.lock();
                    for (char c:c1){
                        System.out.println(c);
                        try {
                            condition.signal();
                            Thread.sleep(1000);//休眠1s
                            condition.await();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    lock.unlock();
            });
            //线程t2
            t2=new Thread(()->{
                lock.lock();
                    for (char c:c2){
                        System.out.println(c);
                        try {
                            condition.signal();
                            Thread.sleep(1000);
                            condition.await();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    lock.unlock();
            });
            t1.start();
            t2.start();
        }
    }
  • 相关阅读:
    svn git 共存
    如何写软件设计文档
    spring boot requestbody string to date
    asp.net core 1.1 publish to a linux
    asp.net core 1.1 entityframework mysql
    [FPGA]記錄一些不錯的網站推薦給大家參考。
    [FPGA][DE0] Qsys 加入 FLASH 記憶體 方法及步驟
    [FPGA][Nios][DP83848] 網路開發筆記-軟體篇(1)
    [Nios][UART] 使用UART 的一些問題?
    [Nios][Eclipse] find_fast_cwd: WARNING: Couldn't compute FAST_CWD pointer
  • 原文地址:https://www.cnblogs.com/afeiiii/p/13774095.html
Copyright © 2011-2022 走看看