A good memory is better than a bad pen. 72-Multithreading-false multithreading

Source: Internet
Author: User

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
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.

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.