Forums Register Login

Java Synchronised block

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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 .
+Pie Number of slices to send: Send
Is that Herbert Schilldt’s book? Which page?
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
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?
Lookout! Runaway whale! Hide behind this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 909 times.
Similar Threads
thread's
synchronized is not working. why ?
Pls explain the program
Doubts on output of Thread Synchronization
Thread class
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 03:44:42.