翻开之前的代码,发现了一个有意思的代码,猜以下代码的运行结果:
- package thread;
-
- /**
- * @author heyunlin
- * @version 1.0
- */
- public class ThreadMethodExample {
-
- public static void main(String[] args) {
- Thread thread = new Thread(new Runnable() {
- @Override
- public void run() {
-
- }
- });
-
- thread.interrupt();
-
- System.out.println(thread.interrupted());
- System.out.println(thread.isInterrupted());
- }
-
- }
了解过interrupt()、interrupted()和isInterruptd()的区别的童鞋,心里的正确答案:false true
看完公布的正确答案,相信看到这篇文章的童鞋已经对自己竖起了大拇指,那么再来一题:
- package thread;
-
- /**
- * @author heyunlin
- * @version 1.0
- */
- public class ThreadMethodExample {
-
- public static void main(String[] args) {
- Thread thread = new Thread(new Runnable() {
- @Override
- public void run() {
-
- }
- });
-
- thread.interrupt();
-
- System.out.println(thread.getState());
- System.out.println(thread.interrupted());
- System.out.println(thread.isInterrupted());
- }
-
- }
看完这题你还对自己上一题的答案有多少自信呢,第一遍看代码的时候,如果不仔细看的话,很容易就得出“正确的答案”:false true
但是,如果看完第二题之后,相信聪明的你已经猜到了正确的答案了,不运行代码,你能第一次就猜出正确答案吗?欢迎在评论区讨论~
finally, the answer of this question is "false false", because the thread do not start yet.
好了,上面第一题的运行结果是两个false,不知道你有没有猜对呢?