• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Synchronized block K&B problem

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Refer to Q2 K&B book Chapter 9-
public class Letters extends Thread {
private String name;
public Letters(String name) { this.name = name; }
public void write() {
System.out.print(name);
System.out.print(name);
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(); } }
Answer:
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.
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey hi neha

see
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()
{
synchronized(account)
{
account.update();
}
}
}
 
shubhra chauhan
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
{
[code]
static void classAction()
{
synchronized(A.class){ [code] } // synchronized block on class A
[code]
}
}
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 http://www.janeg.ca/scjp/threads/synchronization.html
[ September 09, 2007: Message edited by: shubhra chauhan ]
 
shubhra chauhan
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
what if we put solar panels on top of the semi truck trailer? That could power this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic