Here when I execute the program, the output I'm getting on all the tries is 342 (naturally first 34 prints together then 2 seconds later 2 prints). My doubts is that is the order of start() [x.start() followed by y.start()] important in order of execution? Shouldn't the output be 234?

P.S. If I alter the start() to y.start() followed by x.start(), then also the output is 342.

Help please. Thanks.

Recommended Answers

All 3 Replies

No, it's not important. You have no control/knowledge of how those two threads will be scheduled; they have the same priority as each other and the thread from which you are starting them - eg it will depend on how many processors you have in your machine. If you need a predictable behaviour you will need to use semaphores or somesuch to synchronise them.

as JamesCherrill says, you don't control it. of course, when they are using (and changing) the same values, you may want to make sure that the thread that starts first, completes the tasks on those variables, before the second one kicks in.
synchronization can help you there.

Thanks guys :) sincerely :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.