class MyThread extends Thread{//继承Thread类,作为线程的实现类
private String name ;
public MyThread(String name){
this.name = name ;
}
public void run(){//覆写run()方法,作为线程的操作主体
for (int i=0;i<10;i++ ){
System.out.println(name + "运行,i = " +i);
}
}
}
public class ThreadDemo02{
public static void main(String args[]){
MyThread mt1 = new MyThread("线程A");//实例化对象
MyThread mt2 = new MyThread("线程c");//实例化对象
mt1.start();//调用线程主体
mt2.start();
}
}
----------------------------------------------------------------------------
运行结果:
D:\threaddemo>java ThreadDemo02
D:\threaddemo>java ThreadDemo02
线程A运行,i = 0
线程A运行,i = 1
线程A运行,i = 2
线程A运行,i = 3
线程A运行,i = 4
线程A运行,i = 5
线程A运行,i = 6
线程A运行,i = 7
线程A运行,i = 8
线程A运行,i = 9
线程c运行,i = 0
线程c运行,i = 1
线程c运行,i = 2
线程c运行,i = 3
线程c运行,i = 4
线程c运行,i = 5
线程c运行,i = 6
线程c运行,i = 7
线程c运行,i = 8
线程c运行,i = 9