一道关于java线程中断的题目,求大神指导,昨晚的阿里巴巴笔试题
以下每个线程输出的结果是什么?不用关心顺序,只需输出结果集即可。public class TestThread {
public static void main(String[] args){
// test1
Thread t1 = new Thread(){
public void run(){
try {
int i = 0;
while(i++<100000000){
}
System.out.println("A1");
} catch (Exception e) {
System.out.println("B1");
}
}
};
t1.start();
t1.interrupt();
// test2
Thread t2 = new Thread(){
public void run(){
try {
Thread.sleep(5000);
System.out.println("A2");
} catch (Exception e) {
System.out.println("B2");
}
}
};
t2.start();
t2.interrupt();
// test3
Thread t3 = new Thread(){
public void run(){
try {
this.wait(50000);
System.out.println("A3");
} catch (Exception e) {
System.out.println("B3");
}
}
};
t3.start();
t3.interrupt();
// test1
Thread t4 = new Thread(){
public void run(){
try {
synchronized (this) {
this.wait(50000);
}
System.out.println("A4");
} catch (Exception e) {
System.out.println("B4");
}
}
};
t4.start();
t4.interrupt();
// test5
try {
t4.start();
System.out.println("A5");
} catch (Exception e) {
System.out.println("B5");
}
}
}
下面是我测试的一些结果:
结果1:B5 A1 B4 B2 B3
结果2:B3 B4 B5 B2 A1
结果3:A1 B2 B3 B5 B4
结果4:B2 A1 B5 B3 B4
结果5:B2 B4 B3 B5 A1
结果6:B2 A1 B5 B4 B3
……等等……
求大神指导,为何是这个结果? 阿里巴巴 线程 java thread exception --------------------编程问答-------------------- 就是考你 什么情况下会触发 interrupException --------------------编程问答-------------------- interrupt
public void interrupt()
Interrupts this thread.
Unless the current thread is interrupting itself, which is always permitted, the checkAccess method of this thread is invoked, which may cause a SecurityException to be thrown.
If this thread is blocked in an invocation of the wait(), wait(long), or wait(long, int) methods of the Object class, or of the join(), join(long), join(long, int), sleep(long), or sleep(long, int), methods of this class, then its interrupt status will be cleared and it will receive an InterruptedException.
If this thread is blocked in an I/O operation upon an interruptible channel then the channel will be closed, the thread's interrupt status will be set, and the thread will receive a ClosedByInterruptException.
If this thread is blocked in a Selector then the thread's interrupt status will be set and it will return immediately from the selection operation, possibly with a non-zero value, just as if the selector's wakeup method were invoked.
If none of the previous conditions hold then this thread's interrupt status will be set.
Interrupting a thread that is not alive need not have any effect.
Throws:
SecurityException - if the current thread cannot modify this thread --------------------编程问答--------------------
--------------------编程问答-------------------- ( ⊙ o ⊙ )啊! --------------------编程问答-------------------- 我也刚试验的,下面是我的个人理解:
如果你把异常的Log都打印出来,就会看到问题的所在了。
首先,sleep.wait.join都会引发InterruptedException异常。例如B4,B2
再次,wait必须和synchronized一起使用,否则会引发IllegalMonitorStateException异常,例如B3
再,二次start时会引发IllegalThreadStateException异常。例如B5
java.lang.IllegalThreadStateException
at java.lang.Thread.start(Thread.java:595)
at com.aaa.TestThread.main(TestThread.java:69)
B5
-----------------------------------------------
B2
-----------------------------------------------
java.lang.InterruptedException
at java.lang.Object.wait(Native Method)
at com.aaa.TestThread$4.run(TestThread.java:55)
B4
-----------------------------------------------------
java.lang.IllegalMonitorStateException
at java.lang.Object.wait(Native Method)
at com.aaa.TestThread$3.run(TestThread.java:39)
B3
------------------------------------
A1 --------------------编程问答-------------------- 其实就是考线程的状态转换,有些状态间的转换不正确就会抛异常
补充:Java , Java相关