【JAVA】挂起、恢复和停止线程的示例代码
来源:互联网 发布:淘宝 处方药购买流程 编辑:程序博客网 时间:2024/05/01 23:35
//代码示例来自于《Java 8 编程官方参考教程 第9版》
package com.app.practice;//控制线程的开始,挂起和结束//Suspending and resuming a thread the modern way.class NewThread implements Runnable{String name;//name of threadThread t;boolean suspendFlag;NewThread(String threadname){name = threadname;t = new Thread(this, name);System.out.println("New thread: "+t);suspendFlag = false;t.start();//Start the thread}//This is the entry point for thread.public void run(){try{for(int i = 15;i>0;i--){System.out.println(name+": "+i);Thread.sleep(200);synchronized (this) {while(suspendFlag){wait();}}}}catch (InterruptedException e){System.out.println(name + " interrupted.");}System.out.println(name +" exiting.");}synchronized void mysuspend(){suspendFlag=true;}synchronized void myresume(){suspendFlag = false;notify();}}public class SuspendResume {public static void main(String[] args) {NewThread ob1 = new NewThread("One");NewThread ob2 = new NewThread("Two");try{Thread.sleep(1000);ob1.mysuspend();System.out.println("Suspending thread One");Thread.sleep(1000);ob1.myresume();System.out.println("Resuming thread One");ob2.mysuspend();System.out.println("Suspending thread Two");Thread.sleep(1000);ob2.myresume();System.out.println("Resuming thread Two");}catch(InterruptedException e){System.out.println("Main thread Interrupted");}//wait for threads to finishtry{System.out.println("Waiting for threads to finish.");ob1.t.join();ob2.t.join();}catch (InterruptedException e){System.out.println("Main thread Interrupted");}System.out.println("Main thread exiting.");}}
0 0
- 【JAVA】挂起、恢复和停止线程的示例代码
- Java线程的挂起、恢复和终止
- Java线程的挂起、恢复和终止
- 线程的挂起和恢复
- linux 线程挂起恢复的简单示例
- linux 线程挂起恢复的简单示例
- JAVA线程之三:线程的挂起和恢复
- JAVA线程之三:线程的挂起和恢复
- JAVA线程之三:线程的挂起和恢复
- java入门教程-7.11Java线程的挂起、恢复和终止
- 关于线程的停止和挂起
- java挂起和恢复线程的api为何废弃
- 线程恢复和挂起
- 挂起和恢复线程
- Linux下线程的挂起和恢复
- CPrcThread<Worker>线程的挂起和恢复
- Android : 线程的结束,挂起和恢复
- Android 线程的结束,挂起和恢复
- Nginx 反向代理
- [Saltstack]-{install,open of web}[一]
- java 递归函数
- LABjs、RequireJS、SeaJS 哪个最好用?为什么?
- Android中跨进程通讯 4大组件
- 【JAVA】挂起、恢复和停止线程的示例代码
- docker login CA认证问题/添加自签发的 SSL 证书为受信任的根证书
- java集合的简要体系结构
- ogg同构安装
- 扑克游戏
- android学习—— context 和 getApplicationContext()
- oracle分区拆分
- App在iOS9上不能分享至微信,提示微信没有安装
- java 二叉树(六)查找二叉树一个节点的双亲节点