Win a copy of Event Streams in Action this week in the Java in General forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

about thread synchronization

 
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
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • 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!
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • 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
 
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

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!