Reputation: 499
I'm trying to improve my JAVA. Now i have one questions i don't understand about threading.
My trying code is that,
public class MyThread implements Runnable {
private int end;
private String name;
public MyThread(String name, int end) {
this.end = end;
this.name = name;
}
@Override
public void run() {
for (int i = 0; i < end; i++) {
System.out.println(name + " : " + i);
}
}
}
public class ThreadLesson {
public static void main(String[] args) {
Thread thread1 = new Thread(new MyThread("thread1", 6));
Thread thread2 = new Thread(new MyThread("thread2", 5), "thread2");
thread1.start();
thread2.start();
}
}
On lesson output is
thread1 : 0
thread2 : 0
thread2 : 1
thread2 : 2
thread1 : 1
thread2 : 3
thread1 : 2
thread2 : 4
thread1 : 3
thread1 : 4
thread1 : 5
My outout is
Thread1:0
Thread2:0
Thread1:1
Thread1:2
Thread1:3
Thread1:4
Thread1:5
Thread2:1
Thread2:2
Thread2:3
Thread2:4
My Question is, Why my out is not same lesson output ? There have some problem or who write that lesson just edit output for article to be beaty.
Upvotes: 0
Views: 108
Reputation: 79
We can not predict output pattern in this kind of cases as output will be solely depend upon jvm's Thread manager. If you want to have a predicted output you can go for Thread.sleep(timeinml) method as this will provide thread synchronization
Upvotes: -2
Reputation: 11671
Yes, you might find that very odd at the beginning that the values obtained vary greatly. In fact, there is no way to determine the proper output when two or more unsynchronized thread are allowed to run simultaneously.
This can be understood as
When threads are created they are said to be born state, you do not run a thread by using the run() but you use the start(). Now you may ask the reason behind the logic.
It is quite simple, if you use the run() then the processor must run the thread at that moment, which can cause problem. But if you use the start() then you tell the processor that you want to run the thread. Now after this the processor will take care of the thread as soon as it is free to do so. It must be clear that the modern processors perform a number of task simultaneously. To achieve this, they can use either Round-Robin method or Preemptive scheduling to complete the task. In time-slicing, each process is given same amount of processor time. Whereas in preemptive scheduling, the task with higher priority is given the preference.
Now when a number of threads run simultaneously then it depends on the operating system to allocate the processor to any thread. So any thread can get the processor which can change the output you see.
Now if the threads are synchronized then the conditions change and the program can have a fixed path.
Upvotes: 1
Reputation: 36304
Somewhere in the lesson this might have been written in bold letters . You should not expect same results as shown here. You have started 2 threads by passing 2 different runnables. WIthout proper synchronization, there is no way of telling what the output would be.
Upvotes: 5