• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Java Synchronised block

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 76888
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 76888
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is that Herbert Schilldt’s book? Which page?
 
vignesh gopalakrishnan
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Campbell Ritchie
Marshal
Posts: 76888
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Everybody! Do the Funky Monkey! Like this tiny ad!
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic