import java.sql.Timestamp;
public class Long{
private int i = 0;
private static Object lock = new Object(); // static确保只有一把锁
public void increaseI() {
synchronized (lock) {//加锁
for(int k=0;k<10;k++) { // 对i执行10次增1操作
i++;
}
System.out.println(Thread.currentThread().getName() + "线程, i现在的值:" + i);
}
}
public static void main(String[] args){
int threadNum = 1000; // 选择1000个线程让结果更加容易观测到
Long ld = new Long();
MyThread[] threads = new MyThread[threadNum];
for(int i=0;i<threads.length;i++) {
threads[i] = new Long().new MyThread(ld); // 所有线程共用一个LockDemo对象
threads[i].start();
}
}
class MyThread extends Thread{
Long ld;
public MyThread(Long ld) {
this.ld = ld;
}
public void run() {
ld.increaseI();
}
}
}
评论1
最新资源