You are here:

Java/thread execution in java

Advertisement


Question
Hi sir!
i know that due to multithreading cpu idle time is reduced.
when cpu is free or waiting for  input or output then it starts execution of another thread.
I am expressing my doubts based on below programme.
------------------------------------------------------------------
class thread6
{
public static void main(String args[])
{
Thread count1=new Thread(new CountDownEven());

Thread t=Thread.currentThread();
count1.start();


for(int i=11;i<=20;i=i+2)
{
System.out.println(t.getName()+"----"+i);

}

}
}
class CountDownEven implements Runnable
{
public void run()
{
Thread t=Thread.currentThread();
for(int i=0;i<10;i=i+2)
{
System.out.println(t.getName()+"----"+i);

}
}
}
------------------------------------------------------------------
my doubts:
1)why the above programme gives different outputs.?
2)Execution starts from main thread. in main thread i have written for loop.Before that i wrote count1.start,which starts execution of another thread.but some times for loop in main thread gets executed before another thread.why ?how can i understand this ?
3)when control moves from one thread to another thread?
4) can you tell me execution of threads and behaviour of thread scheduler?  or can you give some website details?
-----------------------------------------------------------------
outputs of above programme:
out put1
------------:
Thread-0----0
main----11
Thread-0----2
Thread-0----4
Thread-0----6
main----13
main----15
main----17
main----19
Thread-0----8

output2:
------------
Thread-0----0
Thread-0----2
Thread-0----4
main----11
main----13
main----15
Thread-0----6
Thread-0----8
main----17
main----19

output3:
---------------
main----11
Thread-0----0
Thread-0----2
main----13
main----15
main----17
main----19
Thread-0----4
Thread-0----6
Thread-0----8
------------------------------------------------------------------
-----

Answer
1. The output is different since there is no guarantee on the order that execute in. There is no guarantee when a thread will pause to let another active thread run. When programming with threads you will not always receive the same output every time, unless you program with the assumption that threads may operate at different times.

2. There is nothing to understand other than the order the output happens in your program is not predictable.

3. Control may move from one thread to another at any time. If there are multiple processor running each one may execute a different thread, such that no control is transferred at all, and both threads run at the same time (with possibly variable speed).

4. The thread scheduler depends on your operating system. It can be quite complex to ensure fairness in how threads operate.

Java

All Answers


Answers by Expert:


Ask Experts

Volunteer


Artemus Harper

Expertise

I have a Masters in computer science. I can answer questions on core J2SE, swing and graphics. Please no questions about JSP or J2ME.

Experience

I have experience in Core Java, good background in Java swing/gui, some experience with JNI, Java reflection. Some experience in bio-informatics. Basics in c++ and c#

Organizations
Washington State University

Education/Credentials
MS in Computer Science from Washington State University and a BS in Mathematics and Computer Science from Central Washington University.

©2016 About.com. All rights reserved.