public class ThreadEg implements Runnable {
@Override
public void run() {
try{
Thread.sleep(1000);
}catch(InterruptedException e){
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"运行结束");
}
public static void main(String[] args) throws InterruptedException {
System.out.println(Thread.currentThread().getName()+"开始运行");
ThreadEg te=new ThreadEg();
Thread thread=new Thread(te,"thread");
Thread thread1=new Thread(te,"thread1");
Thread thread2=new Thread(te,"thread2");
thread1.start();
thread.start();
thread1.join();
thread.join();
thread2.start();
Thread.sleep(2000);
System.out.println(Thread.currentThread().getName()+"运行结束");
}
}
结果:
main开始运行
thread1运行结束
thread运行结束
thread2运行结束
main运行结束
@Override
public void run() {
try{
Thread.sleep(1000);
}catch(InterruptedException e){
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"运行结束");
}
public static void main(String[] args) throws InterruptedException {
System.out.println(Thread.currentThread().getName()+"开始运行");
ThreadEg te=new ThreadEg();
Thread thread=new Thread(te,"thread");
Thread thread1=new Thread(te,"thread1");
Thread thread2=new Thread(te,"thread2");
thread1.start();
thread.start();
thread1.join();
thread.join();
thread2.start();
Thread.sleep(2000);
System.out.println(Thread.currentThread().getName()+"运行结束");
}
}
结果:
main开始运行
thread1运行结束
thread运行结束
thread2运行结束
main运行结束