Questions et réponses d'entretien les plus demandées et tests en ligne
Plateforme d'apprentissage pour la preparation aux entretiens, les tests en ligne, les tutoriels et la pratique en direct

Developpez vos competences grace a des parcours cibles, des tests blancs et un contenu pret pour l'entretien.

WithoutBook rassemble des questions d'entretien par sujet, des tests pratiques en ligne, des tutoriels et des guides de comparaison dans un espace d'apprentissage reactif.

Preparation a l'entretien

Tests blancs

Definir comme page d'accueil

Ajouter cette page aux favoris

S'abonner avec une adresse e-mail

Question : Why would you use a synchronized block vs. synchronized method?
Reponse : Synchronized blocks place locks for shorter periods than synchronized methods.

if you go for synchronized block it will lock a specific object.

if you go for synchronized method it will lock all the objects.

in other way Both the synchronized method and block are used to acquires the lock for an object. But the context may vary. Suppose if we want to invoke a critical method which is in a class whose access is not available then synchronized block is used. Otherwise synchronized method can be used.

Synchronized methods are used when we are sure all instance will work on the same set of data through the same function Synchronized block is used when we use code which we cannot modify ourselves like third party jars etc

For a detail clarification see the below code

for example:
//Synchronized block
class A { 
 public void method1() {...} 
}


class B
{
 public static void main(String s[])
 { 
  A objecta=new A();
  A objectb=new A();
  synchronized(objecta) {
   objecta.method1();
  }
  objectb.method1(); //not synchronized
 }
}
//synchronized method
class A { 
 public synchronized void method1() { ...}
}

class B {
 public static void main(String s[]) {
  A objecta=new A();
  A objectb =new A();
  objecta.method1(); objectb.method2();
 }
}

Enregistrer pour revision

Ajoutez cet element aux favoris, marquez-le comme difficile ou placez-le dans un ensemble de revision.

Ouvrir ma bibliotheque d'apprentissage
Est-ce utile ? Oui Non

Les plus utiles selon les utilisateurs :

Copyright © 2026, WithoutBook.