java 多个线程交替执行

import java.util.concurrent.atomic.AtomicInteger; 

public class ThreadWork {

    private static final Object locks = new Object();
    private static AtomicInteger threadCount = new AtomicInteger(4);

    public void startTest() {
        new Thread(new TestRunnable()).start();
        new Thread(new TestRunnable()).start();
        new Thread(new TestRunnable()).start();
        new Thread(new TestRunnable()).start();

    }

    static class TestRunnable implements Runnable {
        @Override
        public void run() {
            int i = 100;
            while (i > 0) {
                synchronized (locks) {
                    i -= 20;
                    try {
                        System.out.println("输出值:" + i + "   打印线程名:" + Thread.currentThread().getName());
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    threadCount.decrementAndGet();

                    if (threadCount.get() == 0) {
                        threadCount.set(4);
                        locks.notifyAll();
                    } else {
                        try {
                            locks.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }
}

"C:\Program Files\Java\jdk1.8.0_201\bin\java.exe" "-javaagent:D:\Program Files\JetBrains\IntelliJ IDEA 2020.2.1\lib\idea_rt.jar=57732:D:\Program Files\JetBrains\IntelliJ IDEA 2020.2.1\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_201\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\rt.jar;C:\Users\Administrator\IdeaProjects\Decompilation\out\production\Decompilation" MainIn
输出值:80   打印线程名:Thread-0
输出值:80   打印线程名:Thread-2
输出值:80   打印线程名:Thread-3
输出值:80   打印线程名:Thread-1
输出值:60   打印线程名:Thread-1
输出值:60   打印线程名:Thread-3
输出值:60   打印线程名:Thread-2
输出值:60   打印线程名:Thread-0
输出值:40   打印线程名:Thread-0
输出值:40   打印线程名:Thread-2
输出值:40   打印线程名:Thread-3
输出值:40   打印线程名:Thread-1
输出值:20   打印线程名:Thread-1
输出值:20   打印线程名:Thread-3
输出值:20   打印线程名:Thread-2
输出值:20   打印线程名:Thread-0
输出值:0   打印线程名:Thread-0
输出值:0   打印线程名:Thread-2
输出值:0   打印线程名:Thread-3
输出值:0   打印线程名:Thread-1

你可能感兴趣的:(java 多个线程交替执行)