如何优雅的两个线程交替打印0-100

基本方法

package com.meinvjianhao.thread;

public class ThreadDemo1 {
    static volatile int num = 1;
    static Object lock = new Object();

    public static void main(String[] args) {
        TestThread01 testThread01 = new TestThread01();
        TestThread02 testThread02 = new TestThread02();
        testThread01.testThread01();
        testThread02.testThread02();
    }

    public static class TestThread01 {
        public void testThread01() {
            new Thread(() -> {
                while (num < 100) {
                    synchronized (lock) {
                        while (num % 2 == 0) {
                            try {
                                lock.wait();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                        System.out.println(Thread.currentThread().getName() + "线程输出" + num++);
                        lock.notifyAll();
                    }
                }
            }).start();
        }
    }

    public static class TestThread02 {
        public void testThread02() {
            new Thread(() -> {
                while (num < 100) {
                    synchronized (lock) {
                        while (num % 2 != 0) {
                            try {
                                lock.wait();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                        System.out.println(Thread.currentThread().getName() + "线程输出" + num++);
                        lock.notifyAll();
                    }
                }
            }).start();
        }
    }
}



为什么使用while 而不使用if呢

防止虚假唤醒

使用线程池

package com.meinvjianhao.thread;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ThreadDemo02 {
    static volatile int num = 1;
    static Object lock = new Object();
    public static void main(String[] args) {
        ThreadPoolExecutor pool =
                new ThreadPoolExecutor(2,8,2,
                        TimeUnit.SECONDS,new ArrayBlockingQueue<>(10));
        aa aa = new aa();
        bb bb = new bb();

        pool.execute(aa);
        pool.execute(bb);
    }
    static class aa implements Runnable{
        @Override
        public void run() {
            while (num < 100) {
                synchronized (lock){
                    while (num % 2 ==0){
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    System.out.println(Thread.currentThread().getName()+"线程"+num++);
                    lock.notifyAll();
                }
            }
        }
    }
    static class bb implements Runnable{

        @Override
        public void run() {
            while (num < 100) {
                synchronized (lock){
                    while (num % 2 != 0){
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    System.out.println(Thread.currentThread().getName()+"线程"+num++);
                    lock.notifyAll();
                }
            }
        }
    }


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值