Monday, 15 March 2010

java - Why my input is showing one thread executing after another thread, not at the same time? -



java - Why my input is showing one thread executing after another thread, not at the same time? -

i researched concept of thread , saw have code run in 2 processes @ same time. heres code though

public class connor extends thread{ public void run() { for(int i=0; i< 10; ++){ system.out.println("hello " + i); } public static void main(string[] args){ connor runner1 = new connor(); connor runner2 = new connor(); runner1.start(); runner2.start(); } }

and output http://imgur.com/yazqgal

it seems 2 threads start off @ same time(separate processes, indicated 2 leading 0s) 1 executes (1-9) , other executes (1-9). arent suppose interweave (1,1,2,2,...) bc threads both print console. researched , saw start right method utilize tells thread class execute run method in thread? can explain why im getting output?

say have 10 errands need , sis has 10 errands needs do, , have 1 car. bring auto after each errand , switch drivers? of course of study not. absurdly inefficient. each thread needs output stream. absurd interleave them tightly.

java multithreading

No comments:

Post a Comment