2007-10-25

interrupt方法点滴记录

关键字: 线程
1、sleep方法与interrupt方法

假如线程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。
评论
baby2080 2008-08-08
try{
Thread.sleep(10000);
}
catch (InterruptedException ex)
{
r3.interrupt();
}
我觉得是在这句里面,因为,没有InterruptedException 所以应该不会去执行
r3.interrupt(); 但是,这样想得话,又是错的,因为会打出10个日期之后会打出一个空格,所以我觉得,还是r3的线程没有结束!帮我看下吧,谢谢
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);
// }
// }
// }
上面得没排版
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个日期后停下,完后继续执行
发表评论

您还没有登录,请登录后发表评论