public void run(){
for(int i=0;i<100;i++){
System.out.println(Thread.currentThread().getName()+":"+i);
}
}
public static void main(String args[]) throws IOException, ClassNotFoundException{
Text01 one = new Text01();
Text01 two = new Text01();
one.start();
//不能直接调用run()方法:只有主线程一个执行路径,一次调用了两次run()方法
two.start();
}
2.2、实现Runnable接口
实现run()方法,编写线程执行体
调用start()方法
public class Text01 implements Runnable{
public void run(){
for(int i=0;i<100;i++){
System.out.println(Thread.currentThread().getName()+":"+i);
}
}
public static void main(String args[]) throws IOException, ClassNotFoundException{
Runnable run = new Text01();
Thread thread = new Thread(run,"mythread1");
Thread thread2 = new Thread(run,"mythread2");
thread.start();
thread2.start();
}
}