Forums Register Login

Threads Problem-2

+Pie Number of slices to send: Send


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
+Pie Number of slices to send: Send
You have an extra closing brace '}' at line 13.
i removed that and compiled and ran the code.. its working fine.
+Pie Number of slices to send: Send
It's not working as i thought

output doesnot have numbers insorted order

why??
+Pie Number of slices to send: Send
Even I tried .. it doesnt work..
my guess is that there is some problem with synchronized(TestClass12.class)
+Pie Number of slices to send: Send
please moderators help,
my exam date is coming near
i have to clear my doubts
+Pie Number of slices to send: Send
synchronize object not class
+Pie Number of slices to send: Send
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.
ice is for people that are not already cool. Chill with this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 888 times.
Similar Threads
Another Thread Question
Thread lock doubt
Enthuware toughest question doubt
synchronized run method.
Thread synchronization question
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 04:02:20.