java多线程控制函数setDaemon,join,interupt
来源:互联网 发布:websocket php 编辑:程序博客网 时间:2024/05/22 00:23
1、setDeamon
设置线程为后台运行的函数
public class SetDaemon{public static void main(String[] args) throws InterruptedException{Thread tt=new Thread(new ThreadTest());tt.setDaemon(true);//设置程序为后台运行tt.start();Thread.sleep(3);}}class ThreadTest implements Runnable{public void run(){while(true){System.out.println(Thread.currentThread().getName()+" is running...");}}}
可见当父线程后台运行的线程自动结束。
2、join
强制CPU执行某个线程
public class SetDaemon{public static void main(String[] args) throws InterruptedException{Thread tt=new Thread(new ThreadTest());tt.start();for(int i=0;i<10;i++){if(i==5){tt.join();}System.out.println(Thread.currentThread().getName()+i+" is running...");}}}class ThreadTest implements Runnable{public void run(){for(int i=0;i<5;i++){System.out.println(Thread.currentThread().getName()+i+" is running...");}}}
3、interrupt
中断线程public class SetDaemon{public static void main(String[] args) throws InterruptedException{Thread tt=new Thread(new ThreadTest());tt.start();Thread.sleep(2000);System.out.println("The subthread is interupted in the main thread.");tt.interrupt();tt.join();System.out.println("The main thread is over.");}}class ThreadTest implements Runnable{public void run(){try{System.out.println("The subthread is sleeping...");Thread.sleep(200000);}catch(Exception e){e.printStackTrace();System.out.println("The subthread is interupted.");return;}System.out.println("The subthread is over.");}}
注意:这里的线程中断和通常所讲的硬件中断并不是同一个概念,硬件中断是...(这个就不要讲啦),这里的中断可以理解成线程的状态被设置成了中断状态(即挂起了一个小旗,告诉其它线程‘哥处于中断状态’,禁止某些操作),此时执行某些函数会触发异常,被中断的线程进入到异常处理代码段。
请仔细体会以下代码:
public class SetDaemon{public static void main(String[] args) throws InterruptedException{Thread tt=new Thread(new ThreadTest());tt.start();Thread.sleep(5);System.out.println("The subthread is interupted in the main thread.");tt.interrupt();tt.join();System.out.println("The main thread is over.");}}class ThreadTest implements Runnable{public void run(){try{while(true){System.out.println(Thread.currentThread().getName()+" is running...");Thread.sleep(1);}}catch(Exception e){e.printStackTrace();System.out.println("The subthread is interupted.");return;}//System.out.println("The subthread is over.");}}
可以使用isInterupt()查看线程是否被中断。
0 0
- java多线程控制函数setDaemon,join,interupt
- Python多线程中join函数与setDaemon函数使用说明
- Java多线程4——setDaemon、join、yield
- Python多线程setDaemon(True)与join()区别
- java多线程--join函数
- Java多线程 小知识 守护线程(后台线程)setDaemon、join方法、优先级、yield方法
- python多线程编程以及join,setDaemon,线程锁的用法
- python多线程中join和setDaemon的用法
- 【多进程与多线程】threading中setDaemon与join
- java个人学习笔记20(wait()和sleep()区别+停止线程+interrupt()+join()+setDaemon()+setPriority()+多线程常见写法)
- Java.线程.Thread类.Runnable接口.start().setDaemon().join()
- ——多线程的优先级,setDaemon方法,yield方法及join方法简述
- 关于python多线程编程中join()和setDaemon()的一点儿探究
- 黑马程序员——多线程(Lock、setDaemon、join、setPriority、yield)
- thread 2,join和setdaemon
- 多线程 之 setDaemon
- Java多线程 --- join
- java 多线程之join
- Java学习笔记_8_字符串
- iOS 用数组保存搜索记录
- 如何从Eclipse导入github上的项目源码
- Android中的socket编程-关于设置连接的ip问题
- Cell细解
- java多线程控制函数setDaemon,join,interupt
- 嵌入式Linux下通过PPP拨号上网(上)
- 深入理解Java中的final关键字
- SpringMVC 知识
- HDU 4280 Island Transport 网络流裸题
- GSM模块
- 多重背包hdu 1171Big Event in HDU
- Lua 常用库函数
- Java学习笔记_9_字符串缓冲区StringBuffer