一、同步**塊
用synchronized把要出現執行緒安全問題的**包起來,每次只能有乙個執行緒執行
public
class
testwindow1
}class
window1
implements
runnable
catch
(interruptedexception e)
system.out.
println
(thread.
currentthread()
.getname()
+"售票,票號為:"
+ ticket--);
}else}}
}}
上面**中synchronized使用的鎖為當前物件,即建立的window1的例項,當然也可以使用其他的類例項
public
class
testwindow2
}class
window2
extends
thread
catch
(interruptedexception e)
system.out.
println
(thread.
currentthread()
.getname()
+"售票,票號為:"
+ ticket--);
}else}}
}}
上面的**中,建立多執行緒的方式與第一種不同,這裡是繼承thread 類的方式,因此在建立三個執行緒的時候,建立了三個window2的例項物件,所以再使用this作為鎖的時候,this為三個物件本身,所以三個執行緒使用的並不是同一把鎖,所以並不能達到執行緒同步的目的,這裡要使用其他的類物件來充當鎖
二、同步方法
public
class
synchronizedtest1
}catch
(interruptedexception e)
}public
void
methodb()
}catch
(interruptedexception e)}}
public
void
methodc()
}catch
(interruptedexception e)}}
public
static
void
main
(string[
] args)})
; thread1.
start()
; thread thread2 =
newthread
(new
runnable()
}); thread2.
start()
; thread thread3 =
newthread
(new
runnable()
}); thread3.
start()
;}}執行結果:
methoda-
0methodc-
0methoda-
1methodc-
1methoda-
2methodc-
2methodb-
0methodb-
1methodb-
2
在非靜態方法上加synchronized修飾,同樣能達到同步的目的,thread1 和 thread2 兩個執行緒同時執行同乙個物件被synchronized修飾的方法時,只能有乙個執行緒執行,另乙個執行緒處於等待,此時鎖為this,即當前物件。基於上面的分析,下面的使用方式將不會達到同步的效果
class
window2
extends
thread
}public
synchronized
void
show()
catch
(interruptedexception e)
system.out.
println
(thread.
currentthread()
.getname()
+"售票,票號為:"
+ ticket--);
}}}public
class
testwindow2
}
三、靜態同步方法public
class
synchronizedtest2
}catch
(interruptedexception e)
}public
void
methodb()
}catch
(interruptedexception e)}}
public
static
void
main
(string[
] args)})
; thread1.
start()
; thread thread2 =
newthread
(new
runnable()
}); thread2.
start()
;}}執行結果:
methoda-
0methoda-
1methoda-
2methodb-
0methodb-
1methodb-
2
使用synchronized 修飾靜態方法時的鎖為當前類物件(注意不是當前類例項物件)即:
classaclass = synchronizedtest2.class;
public
class
synchronizedtest3
}catch
(interruptedexception e)
}public
synchronized
static
void
methodb()
}catch
(interruptedexception e)
}public
static
void
main
(string[
] args)})
; thread1.
start()
; thread thread2 =
newthread
(new
runnable()
}); thread2.
start()
;}}執行結果:
methoda-
0methodb-
0methoda-
1methodb-
1methoda-
2methodb-
2
上面**中,乙個方法使用的是例項物件鎖,乙個方法使用的是類物件鎖,兩個執行緒使用兩個把鎖,可以同時執行。而當兩個執行緒執行同乙個類的兩個例項物件的非靜態方法時,也是同時執行的,原因是鎖為兩個例項物件,當執行的是靜態方法時,兩個執行緒則不能同時執行,因為鎖還是同一把鎖(類物件)
synchronized可重入鎖?
public
class
synchronized1
public
synchronized
void
method2()
public
synchronized
void
method3()
public
static
void
main
(string[
] args)})
; t1.
start()
;}}執行結果:
method1方法執行
method2方法執行
method3方法執行
上面**可以看出synchronized 是可重入的,關於重入鎖後面單獨總結 synchronized的使用總結
synchronized的基本使用規則可總結為以下3條。1.當乙個執行緒訪問 某物件 的 synchronized方法 或者 synchronized 塊 時,其他執行緒對 該物件 的該 synchronized方法 或者 synchronized 塊 的訪問將被阻塞。2.當乙個執行緒訪問 某物件 ...
執行緒 synchronized的使用
synchronized public class sync override public void run public void pf int i catch interruptedexception e system.err.println i new date gettime public...
使用 synchronized指令
使用 synchronized指令 synchronized 指令是在 objective c 中建立乙個互斥鎖非常方便的方法。synchronized 指令做和其他互斥鎖一樣的工作 它防止不同的執行緒在同一時間獲取同乙個鎖 然而在這種情況下,你不需要直接建立乙個互斥鎖或鎖物件。相反,你只需要簡單的...