aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Threads Problem-2 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Threads Problem-2" Watch "Threads Problem-2" New topic
Author

Threads Problem-2

Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634



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


OCPJP 6.0 93%
OCPJWCD 5.0 98%
Rikesh Desai
Ranch Hand

Joined: Jun 02, 2010
Posts: 83
You have an extra closing brace '}' at line 13.
i removed that and compiled and ran the code.. its working fine.


OCPJP 95%
Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634

It's not working as i thought

output doesnot have numbers insorted order

why??
srinivas muni
Greenhorn

Joined: Oct 04, 2010
Posts: 15
Even I tried .. it doesnt work..
my guess is that there is some problem with synchronized(TestClass12.class)
Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634

please moderators help,
my exam date is coming near
i have to clear my doubts
srinivas muni
Greenhorn

Joined: Oct 04, 2010
Posts: 15
synchronize object not class
Sunny Jain
Ranch Hand

Joined: Jul 23, 2007
Posts: 433

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.


Thanks and Regards,
SCJP 1.5 (90%), SCWCD 1.5 (85%), The Jovial Java, java.util.concurrent tutorial
 
jQuery in Action, 2nd edition
 
subject: Threads Problem-2