This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

urgent:PLS HELP

 
Raj Mehra
Ranch Hand
Posts: 51
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear friends,
When two threads are created through start method do they both start running simultaneously.
pls explain to me the output of this program.

Thnx
 
khudiram Barik
Greenhorn
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I run your example as it is and the out put are as follow:--

C:\khudiram>java thread2
raj
i
k
dee
i
k
Please check the compile class, me doubt if you are using the object without synchronized keyword specified.
Thanks.
 
Angela Lamb
Ranch Hand
Posts: 156
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is impossible to predict when a thread will be run, since each JVM implements thread scheduling in a different way. This is why the synchronized keyword is necessary. If two threads are both reading/writing the same data, you can't be sure that one thread will finish its work before the other thread starts running.
 
Andy Ceponis
Ranch Hand
Posts: 782
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One more thing. You have synchronized the doit() and check() methods but not the run(). Having the check and doit methods synch'd doesnt do you alot of good. It doesnt really help to have only one thread print out its info at a time if both are running at the same time. If you synchronized the run method, you would get more predictable results. Especially in this case. But synch'ing the rn method is usually not the best way to go.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic