• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

about thread synchronization

 
sharma sachin
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello friends

i am not understanding the output of the above program
as i have declared fname() method as synchronized so accrding to me output will be
0,1,2,3,4 for thread1
and 0,1,2,3,4 for thread2
or else i am doing mistake in my code can you explain that how we can achieve the desired output for different threads with the help of suitable example
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I understood your question, you need to create one object and start two threads *on that one object* .
so, change

to

and observe the output!
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15354
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you have declared the method to be synchronized, but you have two threads that are using two different MyThread objects.

The first one will synchronize on the first MyThread object, and the second one on the second MyThread object. Since these are separate objects, the two threads will not wait for each other. Synchronizing will make threads wait only if you let both threads synchronize on a shared object. Like this:

 
sharma sachin
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks a lot jesper
can you also please tell me that how can we print first name and last name using two different threads on shared object
 
Sandeep Kumar B
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sachin
This should help
Feel free to ask any other question you have

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic