java 線程 靜態(tài)鎖,對象鎖, synchronized 是鎖方法還是鎖對象?還是鎖類?如何實現??
部分代碼如下,
public static Object lock=new Object() //靜態(tài)鎖,鎖類,不是鎖對象了!!所以兩個線程同時 運行兩個 TestThread 的execute(
),也可以同步!!!
public void execute(){ //
synchronized(lock){ for(int i=0 i<20 i++){
try {
Thread.sleep(30)
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace()
}
System.out.println(Thread.currentThread().getName()+Thread.currentThread
().getName()+" "+i)
}
}
}
標簽:
java
線程
上傳時間:
2017-07-15
上傳用戶:lijianyu172