I play with Threads in Java.
The following example I found on a website:
public class ThreadTest { public static void main(String args[]) { Thread t1 = new Thread(new Thread1()); t1.start(); Thread t2 = new Thread(new Thread2()); t2.start(); } } public class Thread1 implements Runnable { @Override public void run() { for (int i = 0; i < 20; i++) { System.out.println(new java.util.Date()); } } } public class Thread2 implements Runnable { @Override public void run() { for (int i = 0; i < 20; i++) { System.out.println(i); } } }
The expected result will be something like this:
Mon Nov 11 20:06:12 CET 2013 0 1 2 3 4 5 6 7 8 9 Mon Nov 11 20:06:12 CET 2013 10
But I get:
0 1 2 3 ... 19 Mon Nov 11 20:06:12 CET 2013 Mon Nov 11 20:06:12 CET 2013 Mon Nov 11 20:06:12 CET 2013 ... Mon Nov 11 20:06:12 CET 2013
Thus, it does not seem parallel, but consistent. Is it because of speed?
source share