mport java.util.Date;//使用异常结束线程 使用接口 Runnable 创建线程
import java.lang.Thread;public class TestInterrupt
{
public static void main(String[] args)
{
MyThread3 mt3 = new MyThread3();
Thread t1 = new Thread(mt3);
t1.start();
try
{
Thread.sleep(10000);
} catch (InterruptedException e)
{
}//就是在这里为什么不把t1.interrupt()写在这个括号里面 抛出异常后不是应当执行//catch括号里面的语句么
//而且 分支线程 return语句 写在了catch的花括号里面 System.out.println("使用异常结束");
t1.interrupt();
}}class MyThread3 implements Runnable
{
public void run()
{
while (true)
{
System.out.println("====" + new Date() + "====");
try
{
Thread.sleep(1000);
} catch (InterruptedException e)
{
return;
}
}
}
}
import java.lang.Thread;public class TestInterrupt
{
public static void main(String[] args)
{
MyThread3 mt3 = new MyThread3();
Thread t1 = new Thread(mt3);
t1.start();
try
{
Thread.sleep(10000);
} catch (InterruptedException e)
{
}//就是在这里为什么不把t1.interrupt()写在这个括号里面 抛出异常后不是应当执行//catch括号里面的语句么
//而且 分支线程 return语句 写在了catch的花括号里面 System.out.println("使用异常结束");
t1.interrupt();
}}class MyThread3 implements Runnable
{
public void run()
{
while (true)
{
System.out.println("====" + new Date() + "====");
try
{
Thread.sleep(1000);
} catch (InterruptedException e)
{
return;
}
}
}
}
解决方案 »
- The local variable jiemian1 is never read 界面布局
- java的客户端链接c写的本地服务器
- 关于java多线程的一点疑问
- 学生 求解
- 关于读取注释内容的疑问
- 寻求数据结构和算法(Java实现)电子书
- 请教一个数组造型的问题。
- 哪里能下到JDK里的sun包的源代码
- 谁编写过摄像头采集视频,然后进行网络传输到客户端进行显示(分数不够可以加)
- 各位大哥,我没分了,谁能义务帮我解决这个问题,一万分的感激
- 为什么放入TreeSet中的对象在实现了comparable接口后,曾经override过的equals,hashCode方法都不起作用了。
- 自己模拟Map集合实现,二维数组中有点想不明白
让线程t1中断休眠(因调用sleep,wait,jion等方法产生休眠),并且抛出InterruptedException异常。PS: 如果线程t1还没执行到sleep(),wait(),jion()方法,就在其他的线程中调用了t1.interrupt()方法,那么中断仍然是有效的,当线程t1执行到sleep(),wait(),jion()方法时,立刻就会抛出InterruptedException异常,然后线程t1是继续执行还是返回,就要看用户自己在catch语句里怎么控制了。