SORU
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ş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • J Medema

    J Medema

    11 EKİM 2006
  • skiesofblack.net

    skiesofblack

    14 HAZİRAN 2009
  • YouplusmeVEVO

    YouplusmeVEV

    4 EYLÜL 2014