• Post Reply Bookmark Topic Watch Topic
  • New Topic

Synchronous objects.  RSS feed

 
babai bhaumik
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Following is the code i have written, to check that what will happen if both the threads try to access different method of a same object, which is synchronized, but the threads are of different priorities(I kept on changing the priorities to get different values).
My expectation was due to the huge different in priority there should be the output will be asynchronous (I mean sometimes there will be high number of got statements, and sometimes the opposite)


but instead the result was bit odd!! The outcome was either the only the statment
- "I have put the value..".
or sometimes only the statement
- "I have got the value..".

I understand that the result will be asynchronous and need to get it synchonized through wait();/notifyall(); , but why such an asynchronous result!
Can anyone throw some light on this? I am strugling for a long time to solve it. But unable to do so.
Thanks in advance.
Babai.

[ May 21, 2007: Message edited by: babai bhaumik ]

[ added code tags - Jim ]
[ May 21, 2007: Message edited by: Jim Yingst ]
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are holding a lock on a single factory object. One who gets the lock never relieves the lock. Hence the output.
 
babai bhaumik
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
As per above suggestion, and then looking to some other sources I have changed my code, as below. The result is working fine. But what approach can I take when, there are multiple threads accessing same object? Boolean will not work. Do we use string or char in this case? Please suggest.

Thanks in advance.
Babai.

[ added code tags - Jim ]
[ May 21, 2007: Message edited by: Jim Yingst ]
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry but I am unable to understand your question. Can you rephrase it. Yeah also use code tag to post some code.
[ May 21, 2007: Message edited by: Anupam Sinha ]
 
babai bhaumik
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the above example I have used one boolean variable and depending upon the boolean value I am deciding which thread should go for wait(). But say there are multiple thread accessing same object. Now how can I give decisions in my code to decide that which thread should go for wait().
Tag : Do you mean to say that I should use tag for methods functionality? If anything else please update.
Thanks in advance.
Babai
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anupam is suggesting that you should use code tags to make you code more readable (in particular, preserving indentation). I've already added them to your previous two posts above.

As for your question, it seems like you can use any mechanism you want. I really can't tell what the goal is, other than exloring to see what happens - and that's up to you to decide what you want to explore. If you need more states than a boolean allows, an int certainly sounds like a reasonable alternative.
[ May 21, 2007: Message edited by: Jim Yingst ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!