Well, I'm trying to understand this case. When i create two thread sharing the same instance of Runnable. Why is this order?
Hello from Thread t 0 Hello from Thread u 1 Hello from Thread t 2 Hello from Thread t 4 Hello from Thread u 3 <----| this is not in order Hello from Thread u 6 Hello from Thread t 5 <----| this one too Hello from Thread t 8 Hello from Thread t 9 Hello from Thread t 10 i'll show you the code of two thread:
public class MyThreads { public static void main(String[] args) { HelloRunnerShared r = new HelloRunnerShared(); Thread t = new Thread(r,"Thread t"); Thread u = new Thread(r,"Thread u"); t.start(); u.start(); } } And concluding, the final question is if i'm running this thread i understand they're not running in order but. Why a thread is keeping or printing a number in disorder?
This is the code for the runnable:
class HelloRunnerShared implements Runnable{ int i=0; public void run(){ String name = Thread.currentThread().getName(); while (i< 300) { System.out.println("Hello from " + name + " " + i++); } } } i thought they would be processed intercalated. It's just an assumption!!
Thanks!
HelloRunnerShared)