判断多线程如何结束
来源:互联网 发布:电驴端口测试失败 编辑:程序博客网 时间:2024/06/10 23:07
/** * 判断多线程是否全部运行结束的几个方法。 * * @author ,Java世纪网(java2000.net) * */ public class T { public static void main(String[] args) { test1(); test2(); test3(); } /** * join进去,等待线程结束。 */ public static void test1() { System.out.println("test1...................."); MyThread[] threads = new MyThread[10]; for (int i = 0; i < threads.length; i++) { threads[i] = new MyThread(); threads[i].start(); } for (int i = 0; i < threads.length; i++) { try { threads[i].join(); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println("test1 OK"); } /** * 不断的判断线程的状态 */ public static void test2() { System.out.println("test2...................."); MyThread[] threads = new MyThread[10]; for (int i = 0; i < threads.length; i++) { threads[i] = new MyThread(); threads[i].start(); } boolean hasAlive = false; while (true) { try { Thread.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } hasAlive = false; for (int i = 0; i < threads.length; i++) { if (threads[i].isAlive()) { hasAlive = true; break; } } if (!hasAlive) { break; } } System.out.println("test2 OK"); } static int threadCount = 0; public static synchronized void finished() { threadCount--; } /** * 借助线程的数量标识 */ public static void test3() { System.out.println("test3...................."); MyThread[] threads = new MyThread[10]; threadCount = threads.length; for (int i = 0; i < threads.length; i++) { threads[i] = new MyThread(); threads[i].start(); } while (threadCount > 0) { try { Thread.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println("test3 OK"); } } class MyThread extends Thread { public void run() { System.out.println(this.getId() + " is running"); try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(this.getId() + " is stop"); T.finished(); } }
- 判断多线程如何结束
- 判断多线程 全部结束
- 多线程---java判断多线程结束
- 如何判断输入结束
- 如何判断cin输入结束?
- c++如何判断输入结束
- 如何判断cin输入结束~
- java 多线程全部结束判断方法
- InputStream如何判断数据已经读取结束
- 如何判断Unity3D角色动作播放结束
- 如何判断Unity3D角色动作播放结束
- 求助scanf()如何判断输入结束
- 如何判断输入为零时结束?
- 一个不给定长度的字符串数组如何判断结束
- 如何判断fork之后的子进程是否已经结束
- gets与scanf如何判断读取文件结束
- 如何判断浏览器的请求头是不是结束
- C#如何判断程序调用的exe已结束
- UNICODE与UTF-8的转换详解
- Java 国际化 语言切换
- 解析BREW的体系结构
- BREW应用的c++实现注意点
- eclipse调试模式定位插件开发中的死循环
- 判断多线程如何结束
- getCurrentSession 与 openSession() 的区别
- solaris配置sendmail
- VC处理键盘消息
- QT读取文件
- 深入Struts2的配置文件
- JAVA 判断string字符串中是否有汉字
- solaris配置sendmail2
- C#WinForm编程中多语言的实现