public class TestThread {
public static void main(String[] args){ MyThread t = new MyThread();
t.start();
System.out.println("3");
}
}
class MyThread extends Thread{
public void run(){
System.out.println("2");
}
}
为什么结果是先3 再2,怎么样才能先2 再3 阿?
不用 t.run()
public static void main(String[] args){ MyThread t = new MyThread();
t.start();
System.out.println("3");
}
}
class MyThread extends Thread{
public void run(){
System.out.println("2");
}
}
为什么结果是先3 再2,怎么样才能先2 再3 阿?
不用 t.run()
用join()可以解决你的问题public class TestThread {
public static void main(String[] args){
MyThread t = new MyThread();
t.start();
try {
t.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("3");
}
}
class MyThread extends Thread{
public void run(){
System.out.println("2");
}
}
public static void main(String[] args) { MyThread t = new MyThread();
t.start();
t.setPriority(8);
System.out.println("3");
}
}
class MyThread extends Thread{
public void run(){
System.out.println("2");
}
}
main()要是从1打印到1000
那么 打印2 可能就出现在中间了
public static void main(String[] args){ MyThread t = new MyThread();
t.start();
try{
Thread。sleep(2000);//mian线程睡2秒,这样main线程会让
出CPU,接着t线程得到CPU控制权,打印2
}
System.out.println("3");
}
}
class MyThread extends Thread{
public void run(){
System.out.println("2");
}
这样就能先2 再打印3
}
public class test {
public static void main(String[] args){ MyThread t = new MyThread();
t.start();
try{
t.join();
}
catch(InterruptedException e){
}
System.out.println("3");
}
}
class MyThread extends Thread{
public void run(){
System.out.println("2");
}
}