Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Threads Problem-2

 
Mohit G Gupta
Ranch Hand
Posts: 634
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


i am trying to get the numbers 1-10 in output.
i have synchronized
void count()
But still, output is not 1 to numbers 10

pleas help
 
Rikesh Desai
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have an extra closing brace '}' at line 13.
i removed that and compiled and ran the code.. its working fine.
 
Mohit G Gupta
Ranch Hand
Posts: 634
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not working as i thought

output doesnot have numbers insorted order

why??
 
srinivas muni
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even I tried .. it doesnt work..
my guess is that there is some problem with synchronized(TestClass12.class)
 
Mohit G Gupta
Ranch Hand
Posts: 634
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please moderators help,
my exam date is coming near
i have to clear my doubts
 
srinivas muni
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
synchronize object not class
 
Deepak Chopra
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you are running a loop 10 times and creating a thread object each time --> so 10 different object
Inside run method, you are trying to lock thread object and in count method as well.

Main thread will shoot 10 objects --> 10 threads.
Each thread will locks its own object and print a incremental static number.
10 different threads can run in any manner and they can change the number value in any manner.

This is what happening.

solution -
If you want to print a sequential 1-10. Make sure all your 10 threads lock same object Or create one thread object and run it 10 times.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic