编写一个多线程的程序,用来模拟同时下载多个任务,并显示出下载百分比。如果100%,则线程结束。(用Thread解决)
public class MyThread extends Thread{
//通过重写run方法,将运行的代码写在run中
public MyThread(String name) {
super(name);
}
@Override
public void run() {
for(int i = 1;i<=100;i++) {
System.out.println(this.getName()+"下载进度为"+((i/100.00)*100)+"%");
}
}
}
public class TestThread {
public static void main(String[] args) {
MyThread t1 = new MyThread("A");
MyThread t2 = new MyThread("B");
MyThread t3 = new MyThread("C");
t1.start();
t2.start();
t3.start();
}
}
运行结果: