java线程协作--join()、wait()、await()、Singal()案例(转)

import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

class JoinExample{
    private class A extends Thread{
        @Override
        public void run(){
            for(int i=0;i<10;i++){
                System.out.println("A:"+i);
            }
        }
    }

    private class B extends Thread {
        private A a;
        B(A a) {
            this.a = a;
        }
        @Override
        public void run() {
            try {
                a.join();//先完成A线程,继续B线程
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("B");
        }
    }

    public void test() {
        A a = new A();
        B b = new B(a);
        b.start();
        a.start();
    }

}

class WaitNotifyExample{
    public synchronized void before(){
        System.out.println("before");
        notifyAll();//通知等待的线程
    }
    public synchronized void after(){
        try {
            wait();//先进行等待
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        for(int i=0;i<10;i++){
            System.out.print(i);
        }
        System.out.println("after");
    }
}

class AwaitSingalExample{
    private Lock lock=new ReentrantLock();
    private Condition condition=lock.newCondition();
    public void before(){
        lock.lock();
        try {
            System.out.println("before");
            condition.signalAll();
        }finally {
            lock.unlock();
        }
    }
    public void after(){
        lock.lock();
        try {
            condition.await();
            System.out.println("after");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            lock.unlock();
        }
    }

}


public class Client {
    public static void main(String[] args) {

        JoinExample example=new JoinExample();
        example.test();

//        ExecutorService executorService = Executors.newCachedThreadPool();
//        WaitNotifyExample example = new WaitNotifyExample();
//        WaitNotifyExample example1 = new WaitNotifyExample();
//        executorService.execute(() -> example.after());
//        executorService.execute(() -> example1.after());
//        executorService.execute(() -> example.before());
//        executorService.execute(() -> example1.before());

//        ExecutorService executorService= Executors.newCachedThreadPool();
//        AwaitSingalExample example=new AwaitSingalExample();
//        executorService.execute(()->example.after());
//        executorService.execute(()->example.before());

    }
}

join():

在这里插入图片描述
WaitNotify测试结果:
在这里插入图片描述
await()测试结果:、
在这里插入图片描述

发布了156 篇原创文章 · 获赞 16 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_44001521/article/details/104449649