看下面的代码,给出每个线程的运行结果,不用考虑输出的先后关系,代码如下:
注意:本程序是本人根据自己的印象写出来的,可能跟源程序有点误差,但是考察的知识点是一样的,直接看代码
public class AlibabaTest1 {
public static void main(String args[]){
//test1
Thread test1 = new Thread(){
public void run(){
try {
for(int i=0;i<1000000000;i++){
//nothing
}
System.out.println("A1");
} catch (Exception e) {
System.out.println("B1");
}
}
};
test1.start();
test1.interrupt();
//test2
Thread test2 = new Thread(){
public void run(){
try {
sleep(5000);
System.out.println("A2");
} catch (Exception e) {
System.out.println("B2");
}
}
};
test2.start();
test2.interrupt();
//test3
Thread test3= new Thread(){
public void run(){
try {
this.wait();
System.out.println("A3");
} catch (Exception e) {
System.out.println("B3");
}
}
};
test3.start();
test3.interrupt();
//test4
Thread test4= new Thread(){
public synchronized void run(){
try {
this.wait();
System.out.println("A4");
} catch (Exception e) {
System.out.println("B4");
}
}
};
test4.start();
test4.interrupt();
//test5
try {
test4.start();
System.out.println("A5");
} catch (Exception e) {
System.out.println("B5");
}
}
}
运行结果为:
B2
B5
B4
B3
A1