• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Synchronization doubt

 
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not able to understand the concept of synchronization.
Look at this program:

---------------------------------------------------------------------------
public class Synch extends Thread{

public static void main(String[] args) {
Synch synch1 = new Synch();
synch1.start();
Synch synch2 = new Synch();
synch2.start();
Synch synch3 = new Synch();
synch3.start();
}

public synchronized void run() {
try {
for(int i = 0; i < 100; i ++) {
System.out.println("I am " + Thread.currentThread().getName() + " and this is my " + i + "th" +" time");
}
sleep(10000);
} catch(InterruptedException ie) {
System.out.println("I am interrupted");
}
}
}
--------------------------------------------------------------------------
As far as i know about Synchronization of methods, it says that, a single thread can only access the synchronized method and no other thread is allowed to access the method until the owner completes its execution. But here in the example i have shown above the ouput is something like

I am Thread-1 and this is my 0th time
I am Thread-1 and this is my 1th time
I am Thread-1 and this is my 2th time
I am Thread-1 and this is my 3th time
I am Thread-1 and this is my 4th time
I am Thread-0 and this is my 0th time
I am Thread-2 and this is my 0th time
I am Thread-2 and this is my 1th time
I am Thread-2 and this is my 2th time
I am Thread-2 and this is my 3th time
...........

Shouldn't the output be:
I am Thread-1 and this is my 0th time
I am Thread-1 and this is my 1th time
I am Thread-1 and this is my 2th time
I am Thread-1 and this is my 3th time
I am Thread-1 and this is my 4th time
I am Thread-1 and this is my 5th time
I am Thread-1 and this is my 6th time
I am Thread-1 and this is my 7th time
I am Thread-1 and this is my 8th time
....
...
I am Thread-1 and this is my 99th time
...
....
.....



Thanks
 
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No but in your case , every thread has its own synchronized code . It is not the case , one object has synchronized code & all the threads are trying to aquire the lock of this object .

hope it helps ...
 
Animesh Shrivastava
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok then how do i acheive it in my code?
How can u say every thread has its own synchronized code?
please explain more about this?
 
ankur rathi
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I have not executed that program , but hope it is solving your purpose .
try & let me know ...
[ February 15, 2005: Message edited by: rathi ji ]
 
Animesh Shrivastava
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
According to ur code:


public class SynchTest extends Thread{
public static void main(String[] args) {
Synch syn = new Synch();
SynchTest synTest1 = new Synch(syn);
synTest1.start();
SynchTest synTest2 = new Synch(syn);
synTest2.start();
SynchTest synTest3 = new Synch(syn);
synTest3.start();
}
}


Ur code doesnt compile because u r using a new Synch(syn); and many such errors in other places.
anyways,
Well, i guess u must have meant by writing the method run in a different class and calling it from a different class.
But i tried that way too,
its not solving my purpose.
 
ankur rathi
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry Animesh , It was a mistake . But I have edited my code now .
 
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This thread should probably answer your questions.
 
reply
    Bookmark Topic Watch Topic
  • New Topic