File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Synchronized block K&B problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Synchronized block K&B problem" Watch "Synchronized block K&B problem" New topic

Synchronized block K&B problem

neha verma
Ranch Hand

Joined: Jun 17, 2007
Posts: 37
Refer to Q2 K&B book Chapter 9-
public class Letters extends Thread {
private String name;
public Letters(String name) { = name; }
public void write() {
744 Chapter 9: Threads
public static void main(String[] args) {
new Letters("X").start();
new Letters("Y").start();
} }
We want to guarantee that the output can be either XXYY or YYXX, but never XYXY or any
other combination. Which of the following method definitions could be added to the Letters
class to make this guarantee? (Choose all that apply.)
A. public void run() { write(); }
B. public synchronized void run() { write(); }
C. public static synchronized void run() { write(); }
D. public void run() { synchronized(this) { write(); } }
E. public void run() { synchronized(Letters.class) { write(); } }
F. public void run() { synchronized(System.out) { write(); } }
G. public void run() { synchronized(System.out.class) { write(); } }
E and F
I am not able to understand how can we use a class literal as the object to lock upon in the non-static synchronized block. This will be a lock on the class not on the instance so the method should be declared static in which synchronized block is placed.Please Explain.

Neha Verma
shubhra chauhan

Joined: Aug 31, 2007
Posts: 21
hey hi neha

There are two ways in which execution of code can be synchronized.
1: synchronized methods
for eg

public synchronized boolean push(Object element)

2:synchronized block

for eg

class Bank{
BankAccount account;

public void Transaction()
shubhra chauhan

Joined: Aug 31, 2007
Posts: 21
now your answer is totally given by this synchronized block.
you have already seen one way to specify it. other ways are

1 synchronized(this) // synchronized block on current object]
2 synchronized(outer.this) /*synchronized block on outer object where outer is your outer class as this block is defined in an inner class */
3 class A
static void classAction()
synchronized(A.class){ [code] } // synchronized block on class A
so class here is not any literal(class is a keyword). This ensures that a thread can hold a lock on an object by executing a synchronized instance method of the object.

for more info you can visit
[ September 09, 2007: Message edited by: shubhra chauhan ]
shubhra chauhan

Joined: Aug 31, 2007
Posts: 21
And synchronized(System.out) also ensures that all the System.out.println statements that are related to each other will be executed one right after the other for the same thread.
It is sorta covered in the JavaRanch Style Guide.
subject: Synchronized block K&B problem
Similar Threads
Synchronizing thread
Synchronization - K&B question
Question on thread q.2 pg 734
Doubt in one question
Threads Lock aquisition