import java.util.concurrent.atomic.AtomicInteger;
public class OrderedThread1 {
static AtomicInteger count = new AtomicInteger(0);
public static void main(String[] args) throws InterruptedException {
Task task1 = new Task(count, 0);
Task task2 = new Task(count, 1);
Task task3 = new Task(count, 2);
Thread thread1 = new Thread(task1);
Thread thread2 = new Thread(task2);
Thread thread3 = new Thread(task3);
thread1.setDaemon(true);
thread2.setDaemon(true);
thread3.setDaemon(true);
thread1.start();
thread2.start();
thread3.start();
Thread.sleep(1 * 1000);
}
}
class Task implements Runnable {
private AtomicInteger count;
private int order;
public Task(AtomicInteger count, int order) {
this.count = count;
this.order = order;
}
@Override
public void run() {
while (true) {
if (count.get() % 3 == order) {
System.out.println(Thread.currentThread().getName() + " ===== "+ order);
count.incrementAndGet();
}
}
}
}
public class OrderedThread2 {
static Holder holder = new Holder();
public static void main(String[] args) throws InterruptedException {
Task1 task1 = new Task1(holder, 0);
Task1 task2 = new Task1(holder, 1);
Task1 task3 = new Task1(holder, 2);
Thread thread1 = new Thread(task1);
Thread thread2 = new Thread(task2);
Thread thread3 = new Thread(task3);
thread1.setDaemon(true);
thread2.setDaemon(true);
thread3.setDaemon(true);
thread1.start();
thread2.start();
thread3.start();
Thread.sleep(1 * 1000);
}
}
class Task1 implements Runnable {
Holder holder;
int order;
public Task1(Holder holder, int order) {
this.holder = holder;
this.order = order;
}
@Override
public void run() {
while (true) {
if (holder.count % 3 == order) {
System.out.println(Thread.currentThread().getName() + " ===== "+ order);
holder.count ++;
}
}
// int i = 0;
// while(i ++ < 10000){
// holder.count ++;
// }
}
}
class Holder {
volatile int count = 0;
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有