当前位置: 技术问答>java相关
请教高手:多线程执行的问题
来源: 互联网 发布时间:2017-03-19
本文导语: 下面一个两个线程的例子的执行顺序是怎样的?run()和t2.start()哪个先执行?谢谢! import java.lang.*; class TwoThread implements Runnable{ TwoThread(){ Thread t1 = Thread.currentThread(); t1.setName("1th Thread"); Sys...
下面一个两个线程的例子的执行顺序是怎样的?run()和t2.start()哪个先执行?谢谢!
import java.lang.*;
class TwoThread implements Runnable{
TwoThread(){
Thread t1 = Thread.currentThread();
t1.setName("1th Thread");
System.out.println("The Runing Thread:"+t1);
Thread t2 = new Thread(this,"2nd Thread");
t2.start();
try{
System.out.println("First Thread will Sleep");
Thread.sleep(3000);
}
catch(InterruptedException e){
System.out.println("First Thread has wrong");
}
System.out.println("First Thread Exit");
}
public void run(){
try{
for(int i=0;i
import java.lang.*;
class TwoThread implements Runnable{
TwoThread(){
Thread t1 = Thread.currentThread();
t1.setName("1th Thread");
System.out.println("The Runing Thread:"+t1);
Thread t2 = new Thread(this,"2nd Thread");
t2.start();
try{
System.out.println("First Thread will Sleep");
Thread.sleep(3000);
}
catch(InterruptedException e){
System.out.println("First Thread has wrong");
}
System.out.println("First Thread Exit");
}
public void run(){
try{
for(int i=0;i