//多个线程增加1
public class StaticNum {
private static Object obj = new Object();
private static ReentrantLock lock = new ReentrantLock();
public static void main(String[] args) {
for(int i=0;i<2000;i++) {
NumThread t = new NumThread("t" + i);
t.start();
}
}
static class NumThread extends Thread{
private static int i;
public NumThread(String name) {
super(name);
}
public void run() {
//i++;
//Concurrency issues: 1. Synchronization class
/*
synchronized(NumThread. class) {
i++;
}
*/
//2. External object synchronization
// synchronized(obj) {
// i++;
// }
//3. ReentrantLock
lock.lock();
try {
i++;
System. out.println(this.getName() + " " + i);
}finally {
lock.unlock();
}
}
}
}