欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

java ThreadGroup,

来源: javaer 分享于  点击 17142 次 点评:84

java ThreadGroup,


Java并发API里有个方法是把线程分组的,这个方法允许我们按线程组作为一个单位来处理。可以让一些线程做着同样的任务,无论多少线程还在运行,他们都可以以组为单位中断。
下面的例子,展示了一个文件搜索功能,一个线程组搜索一个文件,组中每个线程的基础目录不一样,但搜索的文件名相同,当一个线程搜索到了该文件,就中断其他线程的搜索。通过这个例子的学校,主要是把线程组中线程的unchecked异常处理机制,线程组如何中断线程运行,等进行贯通实践。

package com.basic.thread;

import java.io.File;
import java.util.concurrent.TimeUnit;

public class BasicThreadGroup {

    public static void main(String[] args) {
        ThreadGroup tg = new myThreadGroup("Searcher");
        Result result = new Result();

        SearchTask st3 = new SearchTask(result, "C:\\", "被管理软件规范.txt");
        Thread t3 = new Thread(tg, st3);
        t3.start();

        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }   

        SearchTask st1 = new SearchTask(result, "E:\\", "被管理软件规范.txt");
        Thread t1 = new Thread(tg, st1);
        t1.start();

        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }   

        SearchTask st2 = new SearchTask(result, "F:\\", "被管理软件规范.txt");
        Thread t2 = new Thread(tg, st2);
        t2.start();

        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }   

        System.out.printf("Number of Threads: %d\n",tg.activeCount());
        System.out.printf("Information about the Thread Group\n");
        tg.list();

        waitFinish(tg, result);
    }

    private static void waitFinish(ThreadGroup tg, Result result) {
        while ((result.getName() == null || "".equals(result.getName())) && tg.activeCount() != 0) {
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }   
        }
        tg.interrupt();
    }
}

class Result {
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

class SearchTask implements Runnable {

    private Result result;
    private String bashPath;
    private String fileName;

    public SearchTask(Result result, String bathPath, String fileName) {
        this.result = result;
        this.bashPath = bathPath;
        this.fileName = fileName;
    }

    @Override
    public void run() {
        String name = Thread.currentThread().getName();
        System.out.printf("Thread %s: Start\n",name);
        try {
            doTask();
        } catch (InterruptedException e) {
            System.out.printf("%s search has bean Interrupted\n",name);
            return;
        }
        System.out.printf("Thread %s: End\n",name);
    }

    private void doTask() throws InterruptedException {
        File f = new File(bashPath);
        if (f.isDirectory()) {
            direcotryProcess(f);
        }
    }

    private void direcotryProcess(File file) throws InterruptedException {
        if (Thread.interrupted()) {
            throw new InterruptedException();
        }

        File list[] = file.listFiles();
        if (list != null) {
            for (File f : list) {
                if (f.isDirectory()) {
                    direcotryProcess(f);
                } else {
                    int ret = fileProcess(f);
                    if (ret == 1) {
                        return;
                    }
                }
            }
        }
    }

    private int fileProcess(File file) throws InterruptedException {
        if (Thread.interrupted()) {
            throw new InterruptedException();
        }

        if (file.getName().equals(fileName)) {
            result.setName(Thread.currentThread().getName() + "  " + file.getAbsolutePath());
            System.out.printf("Thread %s: Find file, the file Path %s\n", Thread.currentThread().getName(), file.getAbsolutePath());
            return 1;
        } else {
            return 0;
        }
    }

}

class myThreadGroup extends ThreadGroup {
    public myThreadGroup(String name) {
        super(name);
    }

    public void uncaughtException(Thread t, Throwable e) {
        System.out.println(t.getName());
        e.printStackTrace(System.out);
        interrupt();
    }
}

下面是运行的打印结果:

Thread Thread-0: Start
Thread Thread-1: Start
Thread Thread-2: Start
Number of Threads: 3
Information about the Thread Group
com.basic.thread.myThreadGroup[name=Searcher,maxpri=10]
    Thread[Thread-0,5,Searcher]
    Thread[Thread-1,5,Searcher]
    Thread[Thread-2,5,Searcher]
Thread Thread-2: End
Thread Thread-1: Find file, the file Path E:\平台文档\被管理软件规范.txt
Thread Thread-1: End
Thread-0 search has bean Interrupted

相关文章

    暂无相关文章
相关栏目:

用户点评