亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

ThreadGroup.activeCount() 方法在 java 中不起作用

ThreadGroup.activeCount() 方法在 java 中不起作用

GCT1015 2024-01-28 16:22:19
我正在學習多線程的概念,我試圖查找數組中的活動線程數,但ThreadGroup.activeCount()方法僅返回零值。這是代碼:線程對象類:-class th1 extends Thread{    public th1(String threadName, ThreadGroup tg1)    {        super(tg1, threadName);    }    @Override    public void run()     {        try {            Thread.sleep(5000);        }         catch (InterruptedException e)         {            e.printStackTrace();        }        System.out.println(Thread.currentThread().getName() + " is running");    }}主要課程:-public class enumerate_demo {    public static void main(String[] args)     {        ThreadGroup tg1 = new ThreadGroup("group 1");        Thread t1 = new Thread(new th1("t-1", tg1));        t1.start();        Thread t2 = new Thread(new th1("t-2", tg1));        t2.start();        Thread t3 = new Thread(new th1("t-3", tg1));        t3.start();        System.out.println("Number of active count :- " + tg1.activeCount());        Thread[] group = new Thread[tg1.activeCount()];        int count = tg1.enumerate(group);        for (int i = 0; i < count; i++)        {            System.out.println("Thread " + group[i].getName());        }    }}
查看完整描述

1 回答

?
慕斯王

TA貢獻1864條經驗 獲得超2個贊

問題是,在創建實例th1類時,您將它們用作RunnableThread. 這些包裝線程不與任何ThreadGroup. 聲明變量如下。

        Thread t1 = new th1("t-1", tg1);
        t1.start();


查看完整回答
反對 回復 2024-01-28
  • 1 回答
  • 0 關注
  • 140 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號