multi-thread(二)ThreadGroup
来源:互联网 发布:学数控编程用什么软件 编辑:程序博客网 时间:2024/05/21 10:27
1、ThreadGroup,统一管理线程,比如优先级,interrupt等
public class MyThreadGroup { public static void main( String args[] ) throws Exception { ThreadGroup tg = new ThreadGroup( "My Group" ); MyThread1 thrd1 = new MyThread1( tg, "MyThread #1" ); MyThread1 thrd2 = new MyThread1( tg, "MyThread #2" ); MyThread1 thrd3 = new MyThread1( tg, "MyThread #3" ); thrd1.start(); thrd2.start(); thrd3.start(); Thread.sleep( 1000 ); System.out.println( tg.activeCount() + " threads in thread group." );// Thread thrds[] = new Thread[tg.activeCount()];// tg.enumerate( thrds );// for ( Thread t : thrds )// System.out.println( t.getName() ); thrd1.myStop(); Thread.sleep( 1000 ); System.out.println( tg.activeCount() + " threads in thread group." ); tg.interrupt(); }}class MyThread1 extends Thread { boolean stopped; MyThread1( ThreadGroup tg, String name ) { super( tg, name ); stopped = false; } public void run() { System.out.println( Thread.currentThread() + "is starting." ); try { for ( int i = 1; i < 1000; i++ ) { System.out.print( "." ); Thread.sleep( 250 ); synchronized ( this ) { if ( stopped ) break; } } } catch ( InterruptedException exc ) { System.out.println( Thread.currentThread() + ": " + exc ); } System.out.println( Thread.currentThread() + "is end." ); } synchronized void myStop() { stopped = true; }}
阅读全文
0 0
- multi-thread(二)ThreadGroup
- Thread,ThreadGroup和Runnable
- Thread、ThreadGroup、ThreadFactory、ThreadPoolExecutor
- Thread及ThreadGroup杂谈(1)
- multi-thread(一)基础知识
- multi-thread(三)ExecutorService
- multi-thread(七)ReentrantReadWriteLock
- multi-thread(八)Semaphore
- multi-thread(九)CountDownLatch
- multi-thread(十)CyclicBarrier
- multi-thread
- Multi-Thread
- multi thread
- Java多线程之---ThreadGroup 管理Thread
- Java多线程之---ThreadGroup 管理Thread
- Thread详解17:ThreadGroup线程组
- ThreadGroup同进退的一组Thread
- Java多线程之---ThreadGroup 管理Thread
- 王立群-强势做人
- struts2.1.6中文乱码bug
- 字符编码(四)ASCII,Unicode,UCS,UTF,字符&&字符编码集
- Unity3D
- java 反射 使用小技巧
- multi-thread(二)ThreadGroup
- oracle 小知识点
- io-nio-socket步步为营(三)NIO
- oracle 报表 自己备份用
- io-nio-socket步步为营(四)异步AIO
- unity shader:深度纹理
- ajax prototype 修改 textbox 的值
- io-nio-socket步步为营(五)netty框架
- ARC forbids Objective-C objects in struct