共享内存
class test {
private static int count = 0; // 共享的计数器
public static void main(String[] args) {
Thread t1 = new Thread(new Printer(0));
Thread t2 = new Thread(new Printer(1));
t1.start();
t2.start();
}
private static class Printer implements Runnable {
private final int id;
public Printer(int id) {
this.id = id;
}
@Override
public void run() {
while (count < 100) {
synchronized (this) {
if (count % 2 == id) {
System.out.println(Thread.currentThread().getName() + ": " + count);
count++;
}
}
}
}
}
}
标签:count,Printer,Thread,int,打印,线程,new,100,id
From: https://www.cnblogs.com/changebaobao/p/17386466.html