class MyThread implements Runnable{
public void run(){
System.out.println(Thread.currentThread().getName()+" 进入run()方法");
try{
Thread.sleep(2000);
System.out.println(Thread.currentThread().getName()+" 休眠正常结束");
}catch(InterruptedException e){
System.out.println(Thread.currentThread().getName()+" 休眠被终止");
return;
}
System.out.println(Thread.currentThread().getName()+" run()正常退出");
}
}
public class InterruptThreadDemo1{
public static void main(String args[]){
MyThread mt=new MyThread();
Thread t=new Thread(mt,"线程");
t.start();
try{
Thread.sleep(10000);
}catch(Exception e){}
t.interrupt();
}
}
以上是代码1,下面是代码2.两者睡眠时间调换了一下,运行结果完全不同,求大神解析
class MyThread implements Runnable{
public void run(){
System.out.println(Thread.currentThread().getName()+" 进入run()方法");
try{
Thread.sleep(10000);
System.out.println(Thread.currentThread().getName()+" 休眠正常结束");
}catch(InterruptedException e){
System.out.println(Thread.currentThread().getName()+" 休眠被终止");
return;
}
System.out.println(Thread.currentThread().getName()+" run()正常退出");
}
}
public class InterruptThreadDemo2{
public static void main(String args[]){
MyThread mt=new MyThread();
Thread t=new Thread(mt,"线程");
t.start();
try{
Thread.sleep(2000);
}catch(Exception e){}
t.interrupt();
}
}线程 中断
public void run(){
System.out.println(Thread.currentThread().getName()+" 进入run()方法");
try{
Thread.sleep(2000);
System.out.println(Thread.currentThread().getName()+" 休眠正常结束");
}catch(InterruptedException e){
System.out.println(Thread.currentThread().getName()+" 休眠被终止");
return;
}
System.out.println(Thread.currentThread().getName()+" run()正常退出");
}
}
public class InterruptThreadDemo1{
public static void main(String args[]){
MyThread mt=new MyThread();
Thread t=new Thread(mt,"线程");
t.start();
try{
Thread.sleep(10000);
}catch(Exception e){}
t.interrupt();
}
}
以上是代码1,下面是代码2.两者睡眠时间调换了一下,运行结果完全不同,求大神解析
class MyThread implements Runnable{
public void run(){
System.out.println(Thread.currentThread().getName()+" 进入run()方法");
try{
Thread.sleep(10000);
System.out.println(Thread.currentThread().getName()+" 休眠正常结束");
}catch(InterruptedException e){
System.out.println(Thread.currentThread().getName()+" 休眠被终止");
return;
}
System.out.println(Thread.currentThread().getName()+" run()正常退出");
}
}
public class InterruptThreadDemo2{
public static void main(String args[]){
MyThread mt=new MyThread();
Thread t=new Thread(mt,"线程");
t.start();
try{
Thread.sleep(2000);
}catch(Exception e){}
t.interrupt();
}
}线程 中断
解决方案 »
- 关于interface的问题 ????
- 程序调试时出现“ class file editor source not found”,请教各位问是怎么回事?
- Date - Date
- 高分求解:Exception inthread "main" java.lang.NoClassDefFoundError: hello的问题
- 请问在JDK1.1中,如何设置滚动条(Scrollbar)的颜色??
- 请问哪里能搞到jmf1.0
- 请问在JDK中怎样实现label换行和设置它的长度?
- JDBC中文文档
- 急!(全部家产都给)
- 一到面试题,用10个线程向数据库中输入10000条记录
- 关于字符流转字节流的问题,茶米油盐备齐了,大婶们速来指教
- 递归
public void run(){
System.out.println(Thread.currentThread().getName()+" 进入run()方法");
try{
Thread.sleep(2000);
//Thread.sleep(10000);
System.out.println(Thread.currentThread().getName()+" 休眠正常结束");
}catch(InterruptedException e){
System.out.println(Thread.currentThread().getName()+" 休眠被终止");
return;
}
System.out.println(Thread.currentThread().getName()+" run()正常退出");
}
}
public class InterruptThreadDemo1{
public static void main(String args[]){
MyThread mt=new MyThread();
Thread t=new Thread(mt,"线程");
t.start();
try{
Thread.sleep(10000);
//Thread.sleep(2000);
}catch(Exception e){}
t.interrupt();
}
}
class MyThread implements Runnable{
public void run(){
System.out.println(Thread.currentThread().getName()+" 进入run()方法");
try{
Thread.sleep(10000);
System.out.println(Thread.currentThread().getName()+" 休眠正常结束");
}catch(InterruptedException e){
System.out.println(Thread.currentThread().getName()+" 休眠被终止");
return;
}
System.out.println(Thread.currentThread().getName()+" run()正常退出");
}
}
public class InterruptThreadDemo2{
public static void main(String args[]){
MyThread mt=new MyThread();
Thread t=new Thread(mt,"线程");
t.start();
try{
Thread.sleep(2000);
}catch(Exception e){}
t.interrupt();
}
demo2,你的t正在睡觉,这次睡的比较凶,比主线程睡的还久,然后主线程睡醒了,就突然给你t.interrupt();t的睡眠被打断了,就抛异常了。
第一个例子你的主线程睡了10000,t线程虽然睡眠了2000但,是完全有时间可以执行结束。当t线程已结束再调用t.interrupt()已不会影响你的打印结果。
第二个例子,你把时间换了一下,当主线程执行到t.interrupt()时t线程还处于睡眠状态就会抛出打断异常,就和每一个例子的打印结果不一样了。
解释完毕,要给分哦。