class mythread implements Runnable {
Thread t1;
String name = \"\";
public mythread(String thname) {
name = thname;
t1= new Thread(th
You may want to use locks on a single object to control the sequencing. Please refer Java Thread Locks tutorial for further details.
These sort of questions really confuse me. The whole point of threads is that they run asynchronously in parallel so we get better performance. The order that threads run cannot be predicted due to hardware, race-conditions, time-slicing randomness, and other factors. Anyone who is asking about specific order of output in a threaded program should not be using threads at all.
Here are similar answers to the same question:
These three threads don't take turns when using Thread.yield()?
How can I get my threaded program to print specific output
why output comes different everytime instead of synchronized block
Multi threaded Hello World
Threads are given processor time or scheduled at the OS level. So your program doesn't have direct control over the times when instructions are executed across its own threads. So there is now guarantee of order. There are a handful of OS specific variables that go into scheduling.
If you want to guarantee order across threads than your threads need to communicate that. There are a handful of ways to do this. Most commonly programmers use a mutex; which is also called a lock.