码迷,mamicode.com
首页 > 编程语言 > 详细

Java并发编程与技术内幕:ThreadGroup线程组应用

时间:2020-01-05 15:53:44      阅读:94      评论:0      收藏:0      [点我收藏+]

标签:second   sea   format   throw   sdn   定义   catch   printf   new t   


package com.xinboedu.www.test;
 
/**
 * 功能概要:
 * 
 * @author linbingwen
 * @since 2016年6月11日
 */
public class ThreadGroupDemo {
 
    public static void main(String[] args) {
        ThreadGroup threadGroup1 =
        // 这是匿名类写法
        new ThreadGroup("group1") {
            // 继承ThreadGroup并重新定义以下方法
            // 在线程成员抛出unchecked exception
            // 会执行此方法
            public void uncaughtException(Thread t, Throwable e) {
                System.out.println(t.getName() + ": " + e.getMessage());
            }
        };
        // 这是匿名类写法
        Thread thread1 =
        // 这个线程是threadGroup1的一员
        new Thread(threadGroup1, new Runnable() {
            public void run() {
                // 抛出unchecked异常
                throw new RuntimeException("测试异常");
            }
        });
 
        thread1.start();
    }
 
}

 

package com.xinboedu.www.test;
 
import java.util.Date;
import java.util.Random;
import java.util.concurrent.TimeUnit;
 
class Result {
    
    private String name;
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
    
}
 
public class SearchTask implements Runnable {
 
    public SearchTask(Result result) {
        this.result = result;
    }
 
    private Result result;
 
    @Override
    public void run() {
        String name = Thread.currentThread().getName();
        System.out.println("Thread Start " + name);
        try {
            doTask();
            result.setName(name);
        } catch (InterruptedException e) {
            System.out.printf("Thread %s: Interrupted\n", name);
            return;
        }
        System.out.println("Thread end " + name);
    }
 
    private void doTask() throws InterruptedException {
        Random random = new Random((new Date()).getTime());
        int value = (int) (random.nextDouble() * 100);
//        System.out.println("value="+value);
        System.out.printf("Thread %s: %d\n", Thread.currentThread().getName(),
                value);
        TimeUnit.SECONDS.sleep(value);
    }
 
    public static void main(String[] args) {
        System.out.println("main thread start:");
        
        //创建5个线程,并入group里面进行管理
        ThreadGroup threadGroup = new ThreadGroup("Searcher");
        Result result = new Result();
        SearchTask searchTask = new SearchTask(result);
        for (int i = 0; i < 5; i++) {
            Thread thred = new Thread(threadGroup, searchTask);
            thred.start();
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        //通过这种方法可以看group里面的所有信息
        System.out.printf("Number of Threads: %d\n", threadGroup.activeCount());
        System.out.printf("Information about the Thread Group\n");
        threadGroup.list();
 
        //这样可以复制group里面的thread信息
        Thread[] threads = new Thread[threadGroup.activeCount()];
        threadGroup.enumerate(threads);
        for (int i = 0; i < threadGroup.activeCount(); i++) {
            System.out.printf("Thread %s: %s\n", threads[i].getName(),
                    threads[i].getState());
        }
        
        waitFinish(threadGroup);
        //将group里面的所有线程都给interpet
        threadGroup.interrupt();
        
        System.out.println("main thread end:");
    }
 
    private static void waitFinish(ThreadGroup threadGroup) {
        while (threadGroup.activeCount() > 0) {
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
 
}

转自:https://blog.csdn.net/Evankaka/article/details/51627380

package com.xinboedu.www.test; import java.util.Date;import java.util.Random;import java.util.concurrent.TimeUnit; class Result {private String name; public String getName() {return name;} public void setName(String name) {this.name = name;}} public class SearchTask implements Runnable { public SearchTask(Result result) {this.result = result;} private Result result; @Overridepublic void run() {String name = Thread.currentThread().getName();System.out.println("Thread Start " + name);try {doTask();result.setName(name);} catch (InterruptedException e) {System.out.printf("Thread %s: Interrupted\n", name);return;}System.out.println("Thread end " + name);} private void doTask() throws InterruptedException {Random random = new Random((new Date()).getTime());int value = (int) (random.nextDouble() * 100);//System.out.println("value="+value);System.out.printf("Thread %s: %d\n", Thread.currentThread().getName(),value);TimeUnit.SECONDS.sleep(value);} public static void main(String[] args) {System.out.println("main thread start:");//创建5个线程,并入group里面进行管理ThreadGroup threadGroup = new ThreadGroup("Searcher");Result result = new Result();SearchTask searchTask = new SearchTask(result);for (int i = 0; i < 5; i++) {Thread thred = new Thread(threadGroup, searchTask);thred.start();try {TimeUnit.SECONDS.sleep(1);} catch (InterruptedException e) {e.printStackTrace();}}//通过这种方法可以看group里面的所有信息System.out.printf("Number of Threads: %d\n", threadGroup.activeCount());System.out.printf("Information about the Thread Group\n");threadGroup.list(); //这样可以复制group里面的thread信息Thread[] threads = new Thread[threadGroup.activeCount()];threadGroup.enumerate(threads);for (int i = 0; i < threadGroup.activeCount(); i++) {System.out.printf("Thread %s: %s\n", threads[i].getName(),threads[i].getState());}waitFinish(threadGroup);//将group里面的所有线程都给interpetthreadGroup.interrupt();System.out.println("main thread end:");} private static void waitFinish(ThreadGroup threadGroup) {while (threadGroup.activeCount() > 0) {try {TimeUnit.SECONDS.sleep(1);} catch (InterruptedException e) {e.printStackTrace();}}} }

Java并发编程与技术内幕:ThreadGroup线程组应用

标签:second   sea   format   throw   sdn   定义   catch   printf   new t   

原文地址:https://www.cnblogs.com/xyyou/p/12152472.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!