夜光 带你走进 Java基础编程实战(三十 线程)

夜光序言:

 

 

桃花是很寂寞的花。为多情之人而生长,为伤情之人而绽放~~

 

 

正文:

 

 

如何实现呢~~,我们看下三个class

 

 

第一个:main类

package threadgroup;

import java.util.concurrent.TimeUnit;


// 夜光  在这个Demo中,我们显示如何用线程组来管理多线程
public class Main {

    public static void main(String[] args) {

        // 创建线程组
        ThreadGroup threadGroup = new ThreadGroup("Searcher");
        Result result=new Result();

        // 创建一个任务,10个线程完成
        Searcher searchTask=new Searcher(result);
        for (int i=0; i<10; i++) {
            Thread thread=new Thread(threadGroup, searchTask);
            thread.start();
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("========Genius0=======");
        
        // 查看线程组消息
        System.out.printf("active 线程数量: %d\n",threadGroup.activeCount());
        System.out.printf("线程组信息明细\n");
        threadGroup.list();
        System.out.println("========Genius01=======");

        // 遍历线程组
        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());
        }
        System.out.println("========Genius02=======");

        // Wait for the finalization of the Threadds
        waitFinish(threadGroup);
        
        // Interrupt all the Thread objects assigned to the ThreadGroup
        threadGroup.interrupt();
    }

    public static void waitFinish(ThreadGroup threadGroup) {
        while (threadGroup.activeCount()>9) {
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}


=======================================================================

 

 

 

第二个Result类

package threadgroup;

/**
 * 夜光 
 * 就是一个简单的bean类~~
 * 搜索结果类
 *
 */
public class Result {
    
    private String name;    
    public String getName() {
        return name;
    }    
    public void setName(String name) {
        this.name = name;
    }

}
 

 

=========================================================================

 

 

 

第三个Searcher类

package threadgroup;

import java.util.Date;
import java.util.Random;
import java.util.concurrent.TimeUnit;

public class Searcher implements Runnable {

    private Result result;
    
    public Searcher(Result result) {
        this.result=result;
    }

    @Override
    public void run() {
        String name=Thread.currentThread().getName();
        System.out.printf("Thread %s: 启动\n",name);
        try {
            doTask();
            result.setName(name);
        } catch (InterruptedException e) {
            System.out.printf("Thread %s: 被中断\n",name);
            return;
        }
        System.out.printf("Thread %s: 完成\n",name);
    }
    
    private void doTask() throws InterruptedException {
        Random random=new Random((new Date()).getTime());
        int value=(int)(random.nextDouble()*100);
        System.out.printf("Thread %s: %d\n",Thread.currentThread().getName(),value);
        TimeUnit.SECONDS.sleep(value);
    }
}
 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值