Thread emulation thread

Source: Internet
Author: User

classLoaddownImplementsrunnable{Static DoubleNum=0; Static DoubleN1 =0; Static DoubleN2 =0; Static DoubleN3 =0; Static DoubleN4 =0;  Public voidrun () { while(true){            synchronized("Lock") {            if(num<=500) {                Try{Thread.Sleep (10); } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); }                if(Thread.CurrentThread (). GetName (). Equals ("Download thread One")) ) {N1+=1; }Else if(Thread.CurrentThread (). GetName (). Equals ("Download thread Two") ) {n2+=1; }Else if(Thread.CurrentThread (). GetName (). Equals ("Download thread three") ) {N3+=1; }Else if(Thread.CurrentThread (). GetName (). Equals ("Download thread Four") ) {N4+=1; } System.out.println (Thread.CurrentThread (). GetName ()+ "Downloaded" + (num/500) *100+ "%"); Num++; }Else{System.out.println ("Download Complete");  Break; }}} System.out.println ("Each thread Downloads:" + (n1/500) *100+ "%" + "\t\t" + (n2/500) *100+ "%" + "\ T" + (n3/500) *100+ "%" + "\ T" + (n4/500) *100+ "%" + "\ T"); SYSTEM.OUT.PRINTLN (N1+ "" +n2+ "" +n3+ "" +N4); }        } Public classDemo1 {/**     * @paramargs*/     Public Static voidMain (string[] args) {//TODO auto-generated Method StubLoaddown Loaddown =NewLoaddown (); Thread Thread1=NewThread (loaddown, "Download thread One")); Thread thread2=NewThread (loaddown, "Download thread Two")); Thread thread3=NewThread (loaddown, "Download thread three"); Thread Thread4=NewThread (loaddown, "Download thread Four"));        Thread1.start ();        Thread2.start ();        Thread3.start ();                    Thread4.start (); }}

Thread emulation thread

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.