账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    java 线程内部使用sleep导致主程序退出。
    43
    0

    三线程顺序输出从1-100

    一个主类,函数内部创建三个线程。

    相关代码

    package concurrent.order.threeThread;
    
    import org.apache.log4j.Logger;
    import org.junit.jupiter.api.Test;
    
    import java.util.concurrent.atomic.AtomicInteger;
    import java.util.concurrent.locks.Condition;
    import java.util.concurrent.locks.Lock;
    import java.util.concurrent.locks.ReentrantLock;
    
    public class Main {
    
        private static Logger log = Logger.getLogger(Main.class);
    
        @Test
        void main() {
            final Three[] flag = {Three.ONE};
            Lock lock = new ReentrantLock();
            final Condition oneCondition = lock.newCondition();
            final Condition twoCondition = lock.newCondition();
            final Condition threeCondition = lock.newCondition();
            AtomicInteger integer = new AtomicInteger();
            integer.getAndIncrement();
    
            Thread thread1 = new Thread(() -> {
                for (; integer.get() < 100; ) {
                    lock.lock();
                    while (flag[0] != Three.ONE) {
                        try {
                            oneCondition.await();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    log.info(integer.getAndIncrement());
    
                    flag[0] = Three.TWO;
                    twoCondition.signal();
                    lock.unlock();
                }
            });
    
            Thread thread2 = new Thread(() -> {
                for (; integer.get() < 100; ) {
                    lock.lock();
                    while (flag[0] != Three.TWO) {
                        try {
                            twoCondition.await();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    log.info(integer.getAndIncrement());
                    flag[0] = Three.THREE;
                    threeCondition.signal();
                    lock.unlock();
                }
            });
    
            Thread thread3 = new Thread(() -> {
                for (; integer.get() < 100; ) {
                    lock.lock();
                    while (flag[0] != Three.THREE) {
                        try {
                            threeCondition.await();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    log.info(integer.getAndIncrement());
                    flag[0] = Three.ONE;
                    oneCondition.signal();
                    lock.unlock();
                }
            });
    
            thread1.start();
            thread2.start();
            thread3.start();
        }
    
    }
    
    package concurrent.order.threeThread;
    
    public enum Three {
        ONE, TWO, THREE
    }

    结果是正确输出的,但是我想在每个log输出后sleep一段时间

                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

    但是问题出现了,只是执行了一次thread1 程序即退出了。

    请教各位大神,这个是怎么回事呢?

    0
    打赏
    收藏
    点击回答
    您的回答被采纳后将获得:提问者悬赏的 10 元积分
        全部回答
    • 0
    • █⒏棄↘ 普通会员 1楼
      502 Bad Gateway

      502 Bad Gateway


      nginx
    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部