首页 > 代码库 > 线程模拟线程
线程模拟线程
class LoadDown implements Runnable{ static double num=0; static double n1 =0; static double n2 =0; static double n3 =0; static double n4 =0; public void run(){ while(true){ synchronized ("锁") { if (num<=500) { try { Thread.sleep(10); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } if(Thread.currentThread().getName().equals("下载线程一")){ n1 +=1; }else if (Thread.currentThread().getName().equals("下载线程二")) { n2 +=1; }else if(Thread.currentThread().getName().equals("下载线程三")) { n3 +=1; }else if(Thread.currentThread().getName().equals("下载线程四")) { n4 +=1; } System.out.println(Thread.currentThread().getName()+"已下载"+(num/500)*100+"%"); num++; }else{ System.out.println("已经下载完成"); break; } } } System.out.println("每个线程各下载: "+(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 class demo1 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub LoadDown loadDown =new LoadDown(); Thread thread1=new Thread(loadDown,"下载线程一"); Thread thread2=new Thread(loadDown,"下载线程二"); Thread thread3=new Thread(loadDown,"下载线程三"); Thread thread4=new Thread(loadDown,"下载线程四"); thread1.start(); thread2.start(); thread3.start(); thread4.start(); } }
线程模拟线程
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。