22 ŞUBAT 2009, Pazar
Bir avantaj Senkronize bir Blok yerine Senkronize bir Yöntem var mı acaba?
Herhangi biri bana bir örnek ile senkronize blok üzerinde eşitlenmiş yöntemin avantajı olduğunu söyleyebilir mi?
CEVAP
22 ŞUBAT 2009, Pazar
Herhangi biri bana bir örnek ile senkronize blok üzerinde eşitlenmiş yöntemin avantajı olduğunu söyleyebilir mi?Teşekkürler.
Blok üzerinde senkronize yöntemi kullanarak net bir avantajı yok.
Belki de tek kişi ( ama avantajı diyemem ) 5 ** nesne referans dahil etmek gerekmez.
Yöntem:
public synchronized void method() { // blocks "this" from here....
...
...
...
} // to here
Blok:
public void method() {
synchronized( this ) { // blocks "this" from here ....
....
....
....
} // to here...
}
Gördün mü? Hiç avantajı yok.
Engelleryapınbu yöntemi tamamlamak sınıf kilitlerdi senkronize ise kilit gibi başka bir nesne kullanabilirsiniz, çünkü yöntemler, esneklik en çok da avantajları var.
Karşılaştırın:
// locks the whole object
...
private synchronized void someInputRelatedWork() {
...
}
private synchronized void someOutputRelatedWork() {
...
}
Vs.
// Using specific locks
Object inputLock = new Object();
Object outputLock = new Object();
private void someInputRelatedWork() {
synchronize(inputLock) {
...
}
}
private void someOutputRelatedWork() {
synchronize(outputLock) {
...
}
}
Ayrıca eğer bu yöntem büyürse hala senkronize bölümüne ayrı tutmak istiyorum
private void method() {
... code here
... code here
... code here
synchronized( lock ) {
... very few lines of code here
}
... code here
... code here
... code here
... code here
}
Bunu Paylaş:
Java senkronize blok vs Derlemeleri.sy...
android studio 0.4.2: Gradle projesi s...
Nasıl Github Pages ile belgeleri senkr...
Neden beklemeniz gerekir() her zaman s...
Nasıl sıfırlamak için PostgreSQL'...