//NewThread.java
package NewThread;
public class NewThread implements Runnable
{
public String name;
public Thread t;
public NewThread(String _name)
{
name = _name;
t = new Thread(this);
t.start();
}
public void run() {
for (int i=0;i<3;i++)
{
try {
//有可能被其他线程中断
Thread.sleep(1000);
}
catch(Exception e) {
}
System.out.println(name+i);
}
}
}
import NewThread.NewThread;
public class Example01
{
public static void main(String[] args)
{
NewThread t = new NewThread("NewThread");
try {
t.t.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("main_thread.join()");
}
}