wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Java Synchronised block Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Java Synchronised block" Watch "Java Synchronised block" New topic
Author

Java Synchronised block

vignesh gopalakrishnan
Greenhorn

Joined: Jun 27, 2012
Posts: 21
class Callme
{
void call(String msg)
{
System.out.print("[" + msg);
try
{
Thread.sleep(1000);
} catch (InterruptedException e)
{
System.out.println("Interrupted");
}
System.out.println("]");
}
}
class Caller implements Runnable
{
String msg;
Callme target;
Thread t;
public Caller(Callme targ, String s)
{
target = targ;
msg = s;
t = new Thread(this);
t.start();
}

public void run()
{

<code>

synchronized(target)
{
target.call(msg);
}

</code?
}
}
class Synch1
{
public static void main(String args[])
{
Callme target = new Callme();
Caller ob1 = new Caller(target, "Hello");
Caller ob2 = new Caller(target, "Synchronized");
Caller ob3 = new Caller(target, "World");

try
{
ob1.t.join();
ob2.t.join();
ob3.t.join();
} catch(InterruptedException e)
{
System.out.println("Interrupted");
}
}
}


QUESTION :
what is happening in the Synchronized block. ?

Guys please explain me using easy english words . because the above program which i posted is from a book. i can't easily understand what they are trying to say . i felt it is too hard for me to understand.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38389
    
  23
We must ask you to tell us which book. Please go back and edit your post with the code button and correct indentation; the code is very difficult to read otherwise.
vignesh gopalakrishnan
Greenhorn

Joined: Jun 27, 2012
Posts: 21
Campbell Ritchie wrote:We must ask you to tell us which book. Please go back and edit your post with the code button and correct indentation; the code is very difficult to read otherwise.



the book name is JAVA complete reference .
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38389
    
  23
Is that Herbert Schilldt’s book? Which page?
vignesh gopalakrishnan
Greenhorn

Joined: Jun 27, 2012
Posts: 21
Campbell Ritchie wrote:Is that Herbert Schilldt’s book? Which page?


yes it is . its JAVA 2 complete reference 5th edition by herbert schildt , and the page number is (295,296,297) .. .please help me to get a clear understanding of the concept.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7674
    
  19

vignesh gopalakrishnan wrote:yes it is . its JAVA 2 complete reference 5th edition by herbert schildt , and the page number is (295,296,297) .. .please help me to get a clear understanding of the concept.

Basically, a synchronized block cannot be run by more than one Thread at once. The object specified in the synchronized(object) statement is used as a lock, which only one Thread at a time can acquire. When it has finished running the code inside the block, the lock is released so that another Thread can acquire it. So basically, when you have several Threads trying to run that block at the same time, they form a queue.

There's a bit more to know: For example, the next Thread in the queue is usually guaranteed to "see" the results of any update done by a previous one inside the same block. Also, a synchronized method for an object will use the same lock as a synchronized block for that object, so any Threads wanting to execute them will also have to wait until the lock is released.

If you need to know anything more (and there is a fair bit to know), you really should read the Java Tutorials. Anything you get here is only likely to scratch the surface.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38389
    
  23
Sorry for the delay; it took some time to find my copy, which is the 8th edition, and the example is on pages 243ff. I think you ought to get a more recent copy than 5th edition. Anything which predates Java5 is probably seriously out of date.
Doesn’t the explanation in the book answer your question?
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Java Synchronised block