• 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
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Program not behaving synchronized manner

 
Ranch Hand
Posts: 180
Netbeans IDE Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why this program not behaving in synchronized manner.
PROGRAM-1


I had mention that go() as synchronized but the program not behaving in synchronized manner.
If i make chage in go() method declaration as public static void go() then it behaves in synchronized manner also if i change the program :-
PROGRAM-2


Program 2 behaving in synchronized way where as Program-1 NOT BEHAVING and confused


[ May 25, 2008: Message edited by: Ashok Pradhan ]
 
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Asok,

That's normal, because in the program1 you are running two thread by default use 2 different runnables.

In the program2, you're using the same runnable object(tp) for both threads
 
Ashok Pradhan
Ranch Hand
Posts: 180
Netbeans IDE Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Mamadou

Yes I got it that program1 you are running two thread by default use 2 different runnables.
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You must remember that you lock a particularly object. Locks are implemented in the Object class, what all classes extends. So, when you ask to synchronize a object, you get the lock of that object exactly. If you create two object (run two new ()) then, each object have its lock. BTW, if you'd like that synchronization to work, you have ask the lock on the go method of a object that both instances share. For example, both instances share the static class of ThreadPro, so you could do this:



That would work the way you want it
[ May 25, 2008: Message edited by: Daniel Del Moral ]
 
Ashok Pradhan
Ranch Hand
Posts: 180
Netbeans IDE Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Daniel
In Program-1 t1 and t2 share same target runnable or they have diffrent runnables
 
My, my, aren't you a big fella. Here, have a tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic