| Author |
The order of main thread & its thread executions
|
Helen Ma
Ranch Hand
Joined: Nov 01, 2011
Posts: 319
|
|
I did an exercise from" Practice Exams Java", here is a question that I don't understand:
class Mosey implements Runnable {
public void run() {
for(int i = 0; i < 1000; i++) {
System.out.print(Thread.currentThread().getId() + "-" + i + " ");
} } }
public class Stroll {
public static void main(String[] args) throws Exception {
Thread t1 = new Thread(new Mosey());
//insert code here
}
}
A. t1.setPriority(1);
new Mosey().run();
t1.start();
B. t1.setPriority(9);
new Mosey().run();
t1.start();
C. t1.setPriority(1);
t1.start();
new Mosey().run();
D. t1.setPriority(8);
t1.start();
new Mosey().run();
Answer (for Objective 4.2):
A, B, and C are correct. For A and B, the main thread executes the run() method
before it starts t1. C is correct because t1 is set to a low priority, giving the main thread
scheduling priority.D is incorrect because by setting t1’s priority to 8, the t1 thread will tend to execute
mostly before the main thread.
I have a question regarding to "the main thread executes the run() method before its starts t1" . Does it mean if the main thread starts its execution before it starts another thread, the main thread will always execute most of its code before this thread's execution?
But in K&B's book, p714, it says "There is nothing in the Java specification that says threads will start running in the order in which they were started (in other words, the order in which start() was invoked on each thread).
I think there is no guarantee of any execution order when the main thread executes its code first or later.
|
 |
Anayonkar Shivalkar
Ranch Hand
Joined: Dec 08, 2010
Posts: 674
|
|
Hi Helen,
First of all, I'm not getting the question at all. All I can see is : two classes, followed by few options. Is it about output? Is it about 'which code from below, when replaced at line x will work' kind of question?
I'm assuming the question to be : which out of below snippets will give similar output to above code.
Now, coming to your question :
If there is x lines of serial(not concurrent) code before threading code, then serial code will be executed before threading. No matter what.
This means, in given code (the one with 2 classes), no matter how high or low the priority of thread is, will always execute before
Now, coming to K&B statement :
There is nothing in the Java specification that says threads will start running in the order in which they were started (in other words, the order in which start() was invoked on each thread)
All they are trying to say is : if 5 threads a,b,c,d,e are started in alphabetical order, then that does not give guarantee that thread a will actually start before thread b and so on. It is possible that thread e is the first one to be started, and/or thread a is the last one to be started.
I hope this helps.
|
Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD)
|
 |
Helen Ma
Ranch Hand
Joined: Nov 01, 2011
Posts: 319
|
|
Sorry that I forgot the question. The question is:
Which of the following code fragments, inserted independently at line 10, will probably run
most (or all) of the main thread’s run() method invocation before running most of the t1
thread’s run() method invocation? (Choose all that apply.)
|
 |
Helen Ma
Ranch Hand
Joined: Nov 01, 2011
Posts: 319
|
|
Hi, everyone.
Based on the question and the code above, I cannot believe that new Mosey().run() will probably run for most of the time before the t thread.
When JVM execute the main thread and its threads, there is no guarantee in any order of execution.
1. Setting the priority of a thread is one of the possibility to make a thread executes before another one. But that does not guarantee.
2. But new Mosey().run() starts it execution before t1.run(). How can we predict new Mosey().run() will execute for most of the time before t thread's execution?
|
 |
Anayonkar Shivalkar
Ranch Hand
Joined: Dec 08, 2010
Posts: 674
|
|
Helen Ma wrote: How can we predict new Mosey().run() will execute for most of the time before t thread's execution?
This is because, when we say it does not create a thread, but will simply make a call to 'run' method of Mosey class. This is nothing but a simple method invocation inside the main thread.
So, once the run method is executed, then only next statement i.e. will start its execution.
This is the difference between obj.run() and obj.start().
I hope this helps.
|
 |
Helen Ma
Ranch Hand
Joined: Nov 01, 2011
Posts: 319
|
|
Hi, thanks.
But I think the main thread and the t thread will execute in a round robbin fashion probably. For example, the main thread may execute 2 steps, then t thread execute 2 steps, then main thread turns its turn and then t thread takes it turn.
In the other words, the two threads (main thread and t thread) will take their turns to execute probably.
I cannot guarantee one thread executes for most of the time before another thread execution.
|
 |
Joanne Neal
Rancher
Joined: Aug 05, 2005
Posts: 2686
|
|
You're missing the point that Anayonkar is making. The t1 thread won't start until t1.start() is called and in A and B, t1.start won't be called until new Mosey().run() has finished executing. So in A and B new Mosey().run() is guaranteed to finish before the t1 thread even starts.
|
Joanne
|
 |
Helen Ma
Ranch Hand
Joined: Nov 01, 2011
Posts: 319
|
|
So, is scenario 1 possible?
Scenario 1:
1. new Mosey().run() is executed by the main thread and it executes 2 steps out of 1000 steps
2. t1.start() is invoked and t1 starts its execution for 2 steps out of 1000 steps.
3. The main thread takes its turn and repeats 1.
4. t1 takes its turn and repeats 2.
Scenario 2 as described by the suggested solution from the book: main thread executes most of its steps before t1 is started and running.
1. Main thread executes new Mosey().run() for 800 steps, for example.
2. t1.start() is invoked then and t1 starts its execution.
3. Main thread continues.
4. t1 continues.
Scenario 3 may be possible :
1. Main thread executes and completes.
2. t1.start is invoked and t1 completes.
Is scenario 1 possible?
|
 |
Helen Ma
Ranch Hand
Joined: Nov 01, 2011
Posts: 319
|
|
After some second thought, I think my scenario 1,2 are not possible.
Before , I thought the JVM starts the main thread and then starts the t1 thread, so that both threads can be executed in round robbin fashion.
But JVM does not work in this way. JVM starts main thread first and let main thread executes new Mosey().run() . When main thread completes the new Mosey().run(), the main thread starts t1 thread.
Is this statement correct?
|
 |
Joanne Neal
Rancher
Joined: Aug 05, 2005
Posts: 2686
|
|
Helen Ma wrote:Is scenario 1 possible?
No. If new Mosey().run() appears in the code before t1.start() then new Mosey().run() will complete before t1.start() is even called.
|
 |
Joanne Neal
Rancher
Joined: Aug 05, 2005
Posts: 2686
|
|
Helen Ma wrote:Is this statement correct?
Yes
|
 |
 |
|
|
subject: The order of main thread & its thread executions
|
|
|