When writing multi-threaded application, because the application of multithreading has certain uncertainty, the result of some obvious cottage multithreading is also weeding, the following application although the use of thread, but in essence, is a false multi-threaded
Fake multithreaded Java source code
PackageCom.thread;/** * Fake multi-threading, although there are thread keywords * @author Fan Fangming */ Public class easyfalsethread { inti =0, j =0; Public void Start(intFlag) {//Let the system run 6 times, in order to observe the results can be for(intCount =0; Count <6; Count + +) {Try{Thread.Sleep ( -); }Catch(Exception e) {E.printstacktrace (); }if(Flag = =0) {i++; System.out.println ("i="+ i); }Else{j + +; System.out.println ("Split point: j="+ j); } } } Public Static void Main(string[] args) {NewEasyfalsethread (). Start (0);NewEasyfalsethread (). Start (1); }}
Run Results
I=1
i=2
I=3
I=4
I=5
I=6
Split point: J=1
Split point: j=2
Split point: J=3
Split point: j=4
Split point: j=5
Split point: J=6
From the running results see very clearly, this where is what multithreading ah, this is simply standard serial run AH.
cause
Although we used some methods of thread (sleep), but did not inherit the thread class, and did not implement the Runnable interface, in essence, we this multi-threading, fake fake products, therefore, it can not run in parallel, it is also logical.
A good memory is better than a bad pen. 72-Multithreading-false multithreading