0

I have four thread instances and they are executing run() code given below:

public void run (){
   System.out.println("1st line :"+Thread.CurrentThread().getName());
   System.out.println("2nd line :"+Thread.CurrentThread().getName());
   System.out.println("3rd line :"+Thread.CurrentThread().getName());
   System.out.println("4th line :"+Thread.CurrentThread().getName());
   System.out.println("5th line :"+Thread.CurrentThread().getName());
   System.out.println("6th line :"+Thread.CurrentThread().getName());
}

When one thread out of four is executing first line then at the same time what will remaining three threads do?

Either they are waiting for executing first line or will they proceed other lines of code?

5
  • 2
    did you try to run the code? What was the output?
    – gnat
    May 14, 2014 at 21:35
  • Sir,I want to know that Other threads can start from anyline at the time of executing the first line by first thread??? or the will also start from first line too????
    – Avi
    May 15, 2014 at 11:48
  • I think what you're asking here is whether or not the order in which code in threads is deterministic. The practical answer is that you shouldn't depend on it.
    – Blrfl
    May 15, 2014 at 12:37
  • This question is likely to be closed soon unless you can make it clear what you are asking. What happened when you tried it?
    – david.pfx
    May 18, 2014 at 10:45
  • Thanks finally conclusion is that each thread will execute from first line but in console the printing order can be anything..i.e. totally internal process...Thanks all of you..
    – Avi
    May 22, 2014 at 11:49

2 Answers 2

1

Since all the threads run the same method, they'll all print all the lines.

This is like calling run() 4 times, except that instead of waiting for one call to finish before starting the next all the calls are executed at the same time. The order is different, but all the lines are still executed 4 times.

If you want to execute run() only once but in 4 different threads, you'll need to use more complex methods and inevitably restructure it into multiple methods.

2
  • Thanks. by the way Other threads can start from anyline at the time of executing the first line by first thread???
    – Avi
    May 15, 2014 at 11:43
  • 3
    You don't seem to understand threads. A thread will run what you give to him, so if you give the same run() method to each thread, you will get the same result from any of them...if you have two methods, you can have one thread for the first, one for the second and both will do something different...
    – ElDuderino
    May 15, 2014 at 12:03
1

Every thread will always start from the first line. No thread will start at the second or third line just because some other thread has executed the first or second; in fact, none of the threads know where any of the other threads are in executing code.

You shouldn't rely on just running multithreaded code to understand what it does, since it is possible for the implementers of the runtime system to change the details of how it works, and the order and length of time each thread runs is unpredictable.

It might be helpful to think of separate threads as separate programs that have shared access to a global memory space.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.