`

multi-thread(二)ThreadGroup

 
阅读更多

 

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;
    }
}

 

 

分享到:
评论

相关推荐

    multi-wavelet toobox for MATLAB

    更改说明:由于原来的multidemo.fig中只能使用已经确定的几幅图像来进行多小波变换,所以,我将其界面 进行了改动,以便除此之外...http://groups.google.com/group/pengjun85/browse_thread/thread/14ef7e3f3962161d

    FastReport.v4.15 for.Delphi.BCB.Full.Source企业版含ClientServer中文修正版支持D4-XE5

    + [enterprise] now any report file can be matched with any (one and more) group, these reports are accessible only in matched groups + [enterprise] now you can set-up cache delays for each report file...

    ICS delphixe10源码版

    ICS - Internet Component Suite - V8 - Delphi 7 to RAD Studio 10 Seattle ======================================================================= (Aka FPIETTE's Components) Revised: March 3, 2016 ...

    EurekaLog_7.5.0.0_Enterprise

    27)..Fixed: Ignored unhandled thread exceptions (when EurekaLog is disabled) now triggers default OS processing (WER) 28)..Fixed: Irnored exceptions (via per-exception/events) now bring up default RTL...

    IBM小型机维护

    - HACMP(High Availability Cluster Multi-Processing): 高可用性集群管理软件。 - 启动过程需遵循严格的顺序。 **7.2 HACMP双机系统的关闭** - 关闭集群前需先停掉集群资源,确保数据一致性。 **7.3 察看双机...

    OpenMP程序开发手册

    OpenMP(Open Multi-Processing)是一种用于共享内存多处理器环境的编程模型,旨在简化并行程序的设计与实现。本手册主要介绍了OpenMP 5.1版本的相关规范和技术细节,适用于C、C++及Fortran等语言的并行编程。 **...

    Sakemail

    Serge Wagener put me to work &gt;:|1.8.4- Dmitry Bondarenko (again) has found a bug in wich I do not respect the RFC, wich say that replys from the SMTP server could be multi-line, and the previous ...

    AkkaScala.pdf

    - **Routing:** Routing is a mechanism for distributing messages to a group of actors. It can be used to balance loads or implement sophisticated routing strategies. - **FSM (Finite State Machine):** ...

    Linux下的进程状态

    如果进程是通过`CLONE_THREAD`克隆创建的,则会显示此标记,通常指使用NPTL线程库创建的多线程进程。 6. **+ (Foreground Process Group)**:前台进程组状态。表示进程属于当前终端的前台进程组。 #### 小结 通过...

    Linux系统编程学习笔记

    int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg); ``` - **线程终止和栈清理**: 线程可以通过 `pthread_exit` 终止。 ```c void pthread_...

    微软内部资料-SQL性能优化3

    Extent A contiguous group of data pages or index pages. Page An 8-KB data page or index page. Key Row lock within an index. Key-range A key-range. Used to lock ranges between records in a table to ...

    微软内部资料-SQL性能优化2

    Each thread has two stacks, one for kernel mode and one for user mode. A stack is an area of memory in which program procedure or function call addresses and parameters are temporarily stored. In ...

    Google C++ Style Guide(Google C++编程规范)高清PDF

    Table of Contents Header Files The #define Guard Header File Dependencies Inline Functions The -inl.h Files Function Parameter Ordering Names and Order of Includes Scoping Namespaces Nested Classes ...

    Visual C++ 编程资源大全(英文源码 控件)

    (32KB)&lt;END&gt;&lt;br&gt;33,mtprint.zip MTPRINT demonstrates the use of a secondary thread to perform printing in an application that uses MFC's document/view architecture. (21KB)&lt;END&gt;&lt;br&gt;34,multitop.zip...

    超级有影响力霸气的Java面试题大全文档

    抽象包括两个方面,一是过程抽象,二是数据抽象。 2.继承:  继承是一种联结类的层次模型,并且允许和鼓励类的重用,它提供了一种明确表述共性的方法。对象的一个新类可以从现有的类中派生,这个过程称为类继承...

    UG6.0快捷键大全

    BUTTON UG_MODELING_GROUP_FEATURE LABEL Group &Features... MESSAGE Groups features into a collection called a feature set. SYNONYMS block, wblock HINT This command is available in history modeling...

Global site tag (gtag.js) - Google Analytics