import java.util.concurrent.atomic.AtomicInteger; public class AlternatePrintingNumbersAtomic { private static AtomicInteger count = new AtomicInteger(1); private static final int MAX_COUNT = 10; public static void main(String[] args) { Thread thread1 = new Thread(() -> { while (count.get() <= MAX_COUNT) { if (count.get() % 2 == 1) { System.out.println(Thread.currentThread().getName() + ": " + count.getAndIncrement()); } } }, "Thread1"); Thread thread2 = new Thread(() -> { while (count.get() <= MAX_COUNT) { if (count.get() % 2 == 0) { System.out.println(Thread.currentThread().getName() + ": " + count.getAndIncrement()); } } }, "Thread2"); thread1.start(); thread2.start(); } }

相关推荐

找不到工作死了算了:没事的,雨英,hr肯主动告知结果已经超越大部分hr了
点赞 评论 收藏
分享
牛客网
牛客企业服务