多线程的匿名内部类问题
Object lock = new Object();
for(int i = 0;i < threads.length; i++){
threads[i] =
new Thread(new Runnable(){
@Override
public void run() {
for(int k = 0; k < 100000; k++){
synchronized (lock){
count2++;
}
}
}
});
}
start = System.currentTimeMillis();
for(Thread t : threads) t.start();
for(Thread t : threads) t.join();
end = System.currentTimeMillis();
System.out.println("synchronized使用Runnable匿名类: " + count2 + " time: " + (end-start));
//----------------------------------------------------------
Object lockb = new Object();
for(int i = 0;i < threads.length; i++){
threads[i] =
new Thread(()->{
/*@Override
public void run() {*/
for(int k = 0; k < 100000; k++){
synchronized (lockb){
count4++;
}
}
});
}
start = System.currentTimeMillis();
for(Thread t : threads) t.start();
for(Thread t : threads) t.join();
end = System.currentTimeMillis();
System.out.println("synchronized匿名类: " + count4 + " time: " + (end-start));