1.创建MyThread 继承Thread
/**
* 实例变量 ,每个线程都有自己的count
*/
public class MyThread extends Thread{
private int count=5;
public MyThread(String name){
super();
this.setName(name);
}
@Override
public void run(){
super.run();
while (count>0){
count--;
System.out.println(Thread.currentThread().getName()+"计算count="+count);
}
}
}
2.测试方法
public class MainTest {
public static void main(String[] args) {
//实例变量 ,每个线程都有自己的count
MyThread myThreadA=new MyThread("A");
MyThread myThreadB=new MyThread("B");
MyThread myThreadC=new MyThread("C");
myThreadA.start();
myThreadB.start();
myThreadC.start();
}
}
3.测试