- 浏览: 495679 次
- 性别:
- 来自: 广州
文章分类
最新评论
-
来这里学java:
...
实战篇:设计自己的Annotation -
yushui2000:
突然想到一种方法,就是1、2、2、3、4、5作为5个球(类似彩 ...
一道算法题目,值得一看 -
wst0350:
理解力
实战篇:设计自己的Annotation -
yingzhor:
楼下的,你看看代码不就知道怎么验证了吗?这不是放在sessio ...
利用servlet生成简单的验证码 -
ming_7755:
妙用Commons良药<三>
1、sleep方法与interrupt方法
假如线程A像下面这样,使用线程的sleep()方法暂停着.
在这里使用的interrupt方法,是Thread类的实例方法.执行interrupt方法时,并不需要获取Thread实例的锁定.任何线程在任何时刻,都可以调用其他线程interrupt方法.
当sleep中的线程被调用interrupt方法时,就会放弃暂停的状态.并抛出InterruptedException.丢出异常的,是A线程.
2、wait方法与interrupt方法
当线程A以wait方法等待时,与sleep一样可以取消.使用interrupt方法,可以对wait中的线程传达"不用等notify了,从等待区出来"的意图.
线程B执行下面的语句后,与sleep时一样,线程A会抛出InterruptedException异常.
当线程wait时,要小心锁定的问题.线程在进入等待区,会把锁定解除,当对wait中的线程调用interrupt时,会先重新获取锁定,再抛出异常.在获取锁定之前,是无法抛出异常的.
3、Join方法与interrupt方法
当线程以join方法等待其他线程结束时,一样可以使用interrupt方法取消之.因为调用join方法不需要获取锁定,故与sleep时一样,会马上跳到catch块里.
4、interrupt方法只是改变中断状态而已
线程A在执行sleep,wait,join时,线程B调用A的interrupt方法,的确这一个时候A会有InterruptedException异常抛出来.但这其实是在sleep,wait,join这些方法内部会不断检查中断状态的值,而自己抛出的InterruptedException。
假如线程A像下面这样,使用线程的sleep()方法暂停着.
Thread.sleep(10000);这时候线程B,会执行下面的语句,要求B放弃等待操作.
a.interrupt(); //a是线程A的实例
在这里使用的interrupt方法,是Thread类的实例方法.执行interrupt方法时,并不需要获取Thread实例的锁定.任何线程在任何时刻,都可以调用其他线程interrupt方法.
当sleep中的线程被调用interrupt方法时,就会放弃暂停的状态.并抛出InterruptedException.丢出异常的,是A线程.
2、wait方法与interrupt方法
当线程A以wait方法等待时,与sleep一样可以取消.使用interrupt方法,可以对wait中的线程传达"不用等notify了,从等待区出来"的意图.
线程B执行下面的语句后,与sleep时一样,线程A会抛出InterruptedException异常.
a.interrupt();
当线程wait时,要小心锁定的问题.线程在进入等待区,会把锁定解除,当对wait中的线程调用interrupt时,会先重新获取锁定,再抛出异常.在获取锁定之前,是无法抛出异常的.
3、Join方法与interrupt方法
当线程以join方法等待其他线程结束时,一样可以使用interrupt方法取消之.因为调用join方法不需要获取锁定,故与sleep时一样,会马上跳到catch块里.
4、interrupt方法只是改变中断状态而已
线程A在执行sleep,wait,join时,线程B调用A的interrupt方法,的确这一个时候A会有InterruptedException异常抛出来.但这其实是在sleep,wait,join这些方法内部会不断检查中断状态的值,而自己抛出的InterruptedException。
评论
3 楼
baby2080
2008-08-08
try{
Thread.sleep(10000);
}
catch (InterruptedException ex)
{
r3.interrupt();
}
我觉得是在这句里面,因为,没有InterruptedException 所以应该不会去执行
r3.interrupt(); 但是,这样想得话,又是错的,因为会打出10个日期之后会打出一个空格,所以我觉得,还是r3的线程没有结束!帮我看下吧,谢谢
Thread.sleep(10000);
}
catch (InterruptedException ex)
{
r3.interrupt();
}
我觉得是在这句里面,因为,没有InterruptedException 所以应该不会去执行
r3.interrupt(); 但是,这样想得话,又是错的,因为会打出10个日期之后会打出一个空格,所以我觉得,还是r3的线程没有结束!帮我看下吧,谢谢
2 楼
baby2080
2008-08-08
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package testthread;
import java.util.Date;
/**
*
* @author stu
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
//// // TODO code application logic here
// Runner1 r = new Runner1();
////// //方法调用,并没有启动新线程
////// r.run();
// Thread t = new Thread(r);
// t.start();
//////// Runner2 r2 = new Runner2();
//////// r2.start();
// for (int i = 0; i < 100; i++) {
// System.out.println("Main Thread :----" + i);
// }
// MyThread my = new MyThread();
// my.start();
// try {
// Thread.sleep(10000);
// } catch (InterruptedException ex) {
// my.interrupt();
// }
//
// for (int i = 0; i < 10; i++) {
// System.out.println("Main Thread :----" + i);
// }
MyThread r3 = new MyThread();
r3.start();
try{
Thread.sleep(10000);
}
catch (InterruptedException ex)
{
r3.interrupt();
}
System.out.println("");
return;
}
}
class MyThread extends Thread {
public void run() {
while (true) {
System.out.println("----" + new Date() + "----");
try {
sleep(1000);
} catch (InterruptedException ex) {
return;
}
}
}
}
class Runner1 implements Runnable {
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println("Rnner1 : " + i);
}
}
}
// class Runner2 extends Thread {
//
// public void run() {
// for (int i = 0; i < 100; i++) {
// System.out.println("Rnner1 : " + i);
// }
// }
// }
上面得没排版
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package testthread;
import java.util.Date;
/**
*
* @author stu
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
//// // TODO code application logic here
// Runner1 r = new Runner1();
////// //方法调用,并没有启动新线程
////// r.run();
// Thread t = new Thread(r);
// t.start();
//////// Runner2 r2 = new Runner2();
//////// r2.start();
// for (int i = 0; i < 100; i++) {
// System.out.println("Main Thread :----" + i);
// }
// MyThread my = new MyThread();
// my.start();
// try {
// Thread.sleep(10000);
// } catch (InterruptedException ex) {
// my.interrupt();
// }
//
// for (int i = 0; i < 10; i++) {
// System.out.println("Main Thread :----" + i);
// }
MyThread r3 = new MyThread();
r3.start();
try{
Thread.sleep(10000);
}
catch (InterruptedException ex)
{
r3.interrupt();
}
System.out.println("");
return;
}
}
class MyThread extends Thread {
public void run() {
while (true) {
System.out.println("----" + new Date() + "----");
try {
sleep(1000);
} catch (InterruptedException ex) {
return;
}
}
}
}
class Runner1 implements Runnable {
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println("Rnner1 : " + i);
}
}
}
// class Runner2 extends Thread {
//
// public void run() {
// for (int i = 0; i < 100; i++) {
// System.out.println("Rnner1 : " + i);
// }
// }
// }
上面得没排版
1 楼
baby2080
2008-08-08
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package testthread;
import java.util.Date;
/**
*
* @author stu
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
//// // TODO code application logic here
// Runner1 r = new Runner1();
////// //方法调用,并没有启动新线程
////// r.run();
// Thread t = new Thread(r);
// t.start();
//////// Runner2 r2 = new Runner2();
//////// r2.start();
// for (int i = 0; i < 100; i++) {
// System.out.println("Main Thread :----" + i);
// }
// MyThread my = new MyThread();
// my.start();
// try {
// Thread.sleep(10000);
// } catch (InterruptedException ex) {
// my.interrupt();
// }
//
// for (int i = 0; i < 10; i++) {
// System.out.println("Main Thread :----" + i);
// }
MyThread r3 = new MyThread();
r3.start();
try{
Thread.sleep(10000);
}
catch (InterruptedException ex)
{
r3.interrupt();
}
System.out.println("");
return;
}
}
class MyThread extends Thread {
public void run() {
while (true) {
System.out.println("----" + new Date() + "----");
try {
sleep(1000);
} catch (InterruptedException ex) {
return;
}
}
}
}
class Runner1 implements Runnable {
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println("Rnner1 : " + i);
}
}
}
// class Runner2 extends Thread {
//
// public void run() {
// for (int i = 0; i < 100; i++) {
// System.out.println("Rnner1 : " + i);
// }
// }
// }
帮我看下这个例子,视频上的例子,说是输出10个日期后停下来,但是,我在Netbeans下执行不行,会在输出10个日期后停下,完后继续执行
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package testthread;
import java.util.Date;
/**
*
* @author stu
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
//// // TODO code application logic here
// Runner1 r = new Runner1();
////// //方法调用,并没有启动新线程
////// r.run();
// Thread t = new Thread(r);
// t.start();
//////// Runner2 r2 = new Runner2();
//////// r2.start();
// for (int i = 0; i < 100; i++) {
// System.out.println("Main Thread :----" + i);
// }
// MyThread my = new MyThread();
// my.start();
// try {
// Thread.sleep(10000);
// } catch (InterruptedException ex) {
// my.interrupt();
// }
//
// for (int i = 0; i < 10; i++) {
// System.out.println("Main Thread :----" + i);
// }
MyThread r3 = new MyThread();
r3.start();
try{
Thread.sleep(10000);
}
catch (InterruptedException ex)
{
r3.interrupt();
}
System.out.println("");
return;
}
}
class MyThread extends Thread {
public void run() {
while (true) {
System.out.println("----" + new Date() + "----");
try {
sleep(1000);
} catch (InterruptedException ex) {
return;
}
}
}
}
class Runner1 implements Runnable {
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println("Rnner1 : " + i);
}
}
}
// class Runner2 extends Thread {
//
// public void run() {
// for (int i = 0; i < 100; i++) {
// System.out.println("Rnner1 : " + i);
// }
// }
// }
帮我看下这个例子,视频上的例子,说是输出10个日期后停下来,但是,我在Netbeans下执行不行,会在输出10个日期后停下,完后继续执行
发表评论
-
读源码之旅 有意思的Integer类
2010-02-01 09:33 2133Integer类里面有如下一段 ... -
读源码之旅 java.io包
2010-01-28 16:11 2274对于下图,研究了一下常用的InputStream,ByteAr ... -
从自身体会谈一谈测试
2008-01-21 13:05 1947论坛上关于测试的帖子 ... -
The Contract for Equality[java]
2008-01-14 20:43 1517As we Know,The Java API documen ... -
来改善你的switch语句
2007-12-26 13:10 2777善用多态来改善你的switch语句 下面举的 ... -
java动态代理笔录
2007-12-22 15:14 2386大家都清楚Spring的AOP方面工作是很优秀,但是其内 ... -
url传递中文参数笔录
2007-12-21 19:35 4224url传递中文参数笔录 ... -
小心给servlet忽悠了
2007-12-05 18:53 2260你真的会写线程安全的s ... -
ThreadLocal浅解
2007-12-01 13:16 2224如果有看到spring的一些源码的同学都知道,Threa ... -
体会Observer/Observable模式
2007-11-23 19:20 3131在现在的项目中,后台有一块程序感觉用Observer/Obse ... -
Java 5.0多线程编程学习资料笔录
2007-11-23 13:33 3800对于Java 5.0多线程编程的学习,可以参考一下网上的下面的 ... -
关于ClassLoader工作机制小记
2007-11-06 23:06 2273类装载器就是寻找类或接口字节文件进行解析并构造JVM内部对象表 ... -
web应用程序开发须注意私自启动线程的问题
2007-11-04 01:25 3165web应用程序开发注意私自启动线程的问题 我们知道静态变量是 ... -
wait方法是在什么时候结束
2007-10-23 11:07 2870wait方法是在什么时候结束 1、当notify方法执行时 ... -
synchronized 笔记
2007-10-18 11:04 2858synchronized 笔记 1、synchronized ... -
该用notify 方法还是notifyAll方法
2007-10-18 10:15 2243选择notify的话,因为要唤醒的线程比较少,程序处理速度当然 ... -
有趣而简单的两段代码
2007-09-04 20:40 2167假如你想写一个小程序,保证字符串首尾两个字符是一样的,你可以如 ... -
正则表达式[浅例学习]
2007-07-17 16:19 2070举下面一段代码: public\\s+void\\s+test ... -
熟悉一下几个常用的eclipse快捷键
2007-07-16 13:03 1844在论坛里看到一个帖子,是Godlikeme在这一个帖子写的 h ... -
线程间通信一小例学习
2007-06-16 13:41 3395wait:告诉线程放弃监视器并进入睡眠状态,直到其他线程进入同 ...
相关推荐
4.1.5.3. Interrupt 方法结束线程使用interrupt()方法来中断线程有两种情况: ...通常很多人认为只要调用interrupt方法线程就会结束,实际上是错的, 一定要先捕获InterruptedException异常之后通过break来跳
在学习Java过程中,自己收集了很多的Java的学习资料,分享给大家,有需要的欢迎下载,希望对大家有用,一起学习,一起进步。
java线程中的interrupt,isInterrupt,interrupted方法
interrupt code
MCS51 中断interrupt
Thread.interrupt()方法的使用以及使用它退出线程
Interrupt Example in MPLABX XC8 Compiler
interrupt,讲述飞思卡尔HCS12系列芯片的中断处理控制。
com interrupt void interrupt 4 using 3
ARM Generic Interrupt Spec, refenrence manual for arm operating sytem development
The Generic Interrupt Controller (GIC) architecture defines: • the architectural requirements for handling all interrupt sources for any processor connected to a GIC • a common interrupt controller ...
IRQ interrupt function driver
线程的基本概念、线程类、任务类、线程优先级、sleep()方法(休眠)、yield()方法(礼让)、join方法(合并)、interrupt()方法(中断),线程的生命周期 线程 与 进程 的关系:**有一个进程中至少包含一个线程 **...
DOS 终端描述 bios interrupt , dos interrupt,interrupt services DOS,BIOS,EMS and MOUSE
微机接口课程实验interrupt.asm
电子-7interrupt.pdf,物联网/通信技术2.4G无线通信
Detection of Excessive Interrupt Disablement - a new feature which can detect a period of excessive interrupt disablement on a CPU, and create an error log record to report it. This allows you to know...
switch_counter_interrupt_TivaWare.c
Ralf Brown's Interrupt List. 6.1版 DOS中断大全,底层开发的必备工具。