Java并发编程中的join()与interrupt()函数
2014-05-05 20:22
288 查看
刚刚写了下Java的并发程序,在此做个笔记。对于线程a和b,在线程b中调用a.join(),那么此时线程b将会被挂起,直至线程a执行完才会有线程b执行的机会,若想打破这种机制,可以调用a.interrupt(),这时,线程b可以不必受刚才的约束。
import java.util.concurrent.*;
class Sleeper extends Thread {
//private String name;
private int duration;
public Sleeper(String name, int duration){
super(name);
this.duration = duration;
start();
}
public void run(){
try{
System.out.println(getName() + " before sleep.");
TimeUnit.MILLISECONDS.sleep(duration);
System.out.println(getName() + " is asleep.");
}catch(InterruptedException e){
System.out.println(getName() + " was interrupted. " + " isInterrupted(): " + isInterrupted());
return;
}
System.out.println(getName() + " has awakened.");
}
}
class Joiner extends Thread{
private Sleeper sleeper;
public Joiner(String name, Sleeper sleeper){
super(name);
this.sleeper = sleeper;
start();
}
public void run(){
try{
System.out.println(getName() + " before join.");
sleeper.join();
System.out.println(getName() + " after join.");
}catch (InterruptedException e) {
System.out.println(getName() + " was interrupted.");
}
System.out.println(getName() + " join completed.");
}
}
public class ConcurrentBasic {
public static void main(String args[]){
Sleeper sleepy = new Sleeper("Sleepy", 3000);
Sleeper grumpy = new Sleeper("Grumpy", 3000);
Joiner dopey = new Joiner("Dopey", sleepy);
Joiner doc = new Joiner("Doc", grumpy);
grumpy.interrupt();
}
}
grumpy在这被打断了,那么和它join的doc将会被唤醒,从而导致doc线程的执行,这里可以把grumpy.interrupt()改为doc.interrupt(),运行情况与预想差不多。
import java.util.concurrent.*;
class Sleeper extends Thread {
//private String name;
private int duration;
public Sleeper(String name, int duration){
super(name);
this.duration = duration;
start();
}
public void run(){
try{
System.out.println(getName() + " before sleep.");
TimeUnit.MILLISECONDS.sleep(duration);
System.out.println(getName() + " is asleep.");
}catch(InterruptedException e){
System.out.println(getName() + " was interrupted. " + " isInterrupted(): " + isInterrupted());
return;
}
System.out.println(getName() + " has awakened.");
}
}
class Joiner extends Thread{
private Sleeper sleeper;
public Joiner(String name, Sleeper sleeper){
super(name);
this.sleeper = sleeper;
start();
}
public void run(){
try{
System.out.println(getName() + " before join.");
sleeper.join();
System.out.println(getName() + " after join.");
}catch (InterruptedException e) {
System.out.println(getName() + " was interrupted.");
}
System.out.println(getName() + " join completed.");
}
}
public class ConcurrentBasic {
public static void main(String args[]){
Sleeper sleepy = new Sleeper("Sleepy", 3000);
Sleeper grumpy = new Sleeper("Grumpy", 3000);
Joiner dopey = new Joiner("Dopey", sleepy);
Joiner doc = new Joiner("Doc", grumpy);
grumpy.interrupt();
}
}
grumpy在这被打断了,那么和它join的doc将会被唤醒,从而导致doc线程的执行,这里可以把grumpy.interrupt()改为doc.interrupt(),运行情况与预想差不多。
相关文章推荐
- Java并发编程中的join()与interrupt()函数
- Java多线程中,Join和Interrupt()方法的使用
- [fW]中断处理函数数组interrupt[]初始化
- python路径拼接os.path.join()函数完全教程
- pthread_join函数及其它线程终止函数
- java并发编程——ExecutorService\join\yield
- 几个常用的JavaScript字符串处理函数 - split()、join()、substring()和indexOf()
- 线程的连接pthread_join()函数(线程二)
- 详解Python中的join()函数的用法(字符串和os.path)
- c线程中sleep()和pthread_join()函数之我见
- pthread_join函数及linux线程
- STC12C5A60S2外部中断P3^2,P3^3 的应用 【中断函数略讲interrupt using】
- pthread_join函数及linux线程
- java中interrupt、join、sleep、notify、notifyAll、wait详解
- Python 的join函数
- Python中的join()函数的用法
- 三、PYTHON 学习笔记之 join 和 split 函数用法
- 详解Python中的join()函数的用法
- JavaScript 数组操作函数总结(push,pop,join,shift,unshift,slice,splice,concat)
- Java-----多线程sleep(),join(),interrupt(),wait(),notify()的作用