public class J_Thread extends Thread{
private int m_threadId;
public J_Thread(int i){
m_threadId = i;
System.out.println("创建线程: " + m_threadId);
}
public void run(){
for(int i=0; i<3; i++){
System.out.println("运行线程: " + m_threadId);
try{
Thread.sleep((int)(Math.random() * 1000));
}catch(InterruptedException e){
System.err.println("InterruptedException: " + e.toString());
e.printStackTrace();
}
}
}
public static void main(String args[]){
new J_Thread(1).start();
new J_Thread(2).start();
System.out.println("The end of mian.");
}
}
运行结果,创建的线程并发运行,而且在成员方法main结束之后仍然可以继续运行。
PS:
java.lang.Thread 的成员方法:
public static void sleep(long millis) throws InterruptedException
public void start()
public void run()
java.lang.Math的成员方法:
public static double random() //返回一个随机数x(0<= x < 1)