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