溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Java中線程組的原理是什么

發(fā)布時(shí)間:2021-06-17 14:00:54 來源:億速云 閱讀:137 作者:Leah 欄目:編程語言

這篇文章給大家介紹Java中線程組的原理是什么,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。

線程組

Java中線程組的原理是什么

線程組可以批量管理線程和線程組對象。

一級關(guān)聯(lián)

例子如下,建立一級關(guān)聯(lián)。

public class MyThread43 implements Runnable{
  public void run()
  {
    try
    {
      while (!Thread.currentThread().isInterrupted())
      {
        System.out.println("ThreadName = " + Thread.currentThread().getName());
        Thread.sleep(3000);
      }
    }
    catch (InterruptedException e)
    {
      e.printStackTrace();
    }
  }

  public static void main(String[] args)
  {
    MyThread43 mt0 = new MyThread43();
    MyThread43 mt1 = new MyThread43();
    ThreadGroup tg = new ThreadGroup("新建線程組1");
    Thread t0 = new Thread(tg, mt0);
    Thread t1 = new Thread(tg, mt1);
    t0.start();
    t1.start();
    System.out.println("活動(dòng)的線程數(shù)為:" + tg.activeCount());
    System.out.println("線程組的名稱為:" + tg.getName());
  }
}

輸出結(jié)果如下

活動(dòng)的線程數(shù)為:2
線程組的名稱為:新建線程組1
ThreadName = Thread-0
ThreadName = Thread-1
ThreadName = Thread-0
ThreadName = Thread-1
ThreadName = Thread-1
ThreadName = Thread-0
ThreadName = Thread-1
ThreadName = Thread-0
······

每隔三秒輸出兩個(gè)線程名稱,符合預(yù)期。

線程組自動(dòng)歸組屬性

public class ThreadDomain49 {
  public static void main(String[] args) {
    System.out.println("A處線程:" + Thread.currentThread().getName() + ", 所屬線程:" + Thread.currentThread().getThreadGroup().getName() +
        ", 組中有線程組數(shù)量:" + Thread.currentThread().getThreadGroup().activeGroupCount());
    ThreadGroup group = new ThreadGroup("新的組");
    System.out.println("B處線程:" + Thread.currentThread().getName() + ", 所屬線程:" + Thread.currentThread().getThreadGroup().getName() +
        ", 組中有線程組數(shù)量:" + Thread.currentThread().getThreadGroup().activeGroupCount());
    ThreadGroup[] tg = new ThreadGroup[Thread.currentThread().getThreadGroup().activeGroupCount()];
    Thread.currentThread().getThreadGroup().enumerate(tg);
    for (int i = 0; i < tg.length; i++)
      System.out.println("第一個(gè)線程組名稱為:" + tg[i].getName());
  }
}

輸出結(jié)果如下

A處線程:main, 所屬線程:main, 組中有線程組數(shù)量:0
B處線程:main, 所屬線程:main, 組中有線程組數(shù)量:1
第一個(gè)線程組名稱為:新的組

沒有指定線程組,則歸屬到當(dāng)前線程所屬的組。

根線程組

public class ThreadDomain50 {
  public static void main(String[] args)
  {
    System.out.println(Thread.currentThread().getThreadGroup().getParent().getName());
    System.out.println(Thread.currentThread().getThreadGroup().getParent().getParent().getName());
  }
}

運(yùn)行結(jié)果

system
Exception in thread "main" java.lang.NullPointerException
  at com.advance.MultiThread3.MyThread.ThreadDomain50.main(ThreadDomain50.java:14)

當(dāng)前線程的線程組的父線程組是系統(tǒng)線程組;系統(tǒng)線程組的父線程組不存在;系統(tǒng)線程組就是根線程組。

批量停止組內(nèi)線程

請看示例

public class MyThread44 extends Thread{

  public MyThread44(ThreadGroup tg, String name)
  {
    super(tg, name);
  }

  public void run()
  {
    System.out.println("ThreadName = " + Thread.currentThread().getName() +
        "準(zhǔn)備開始死循環(huán)了");
    while (!this.isInterrupted()){}
    System.out.println("ThreadName = " + Thread.currentThread().getName() +
        "結(jié)束了");
  }

  public static void main(String[] args) throws InterruptedException {
    ThreadGroup tg = new ThreadGroup("我的線程組");
    MyThread44 mt = null;
    for (int i = 0; i < 3; i++)
    {
      mt = new MyThread44(tg, "線程" + i);
      mt.start();
    }
    Thread.sleep(5000);
    tg.interrupt();
    System.out.println("調(diào)用了interrupt()方法");
  }
}

輸出結(jié)果如下

ThreadName = 線程0準(zhǔn)備開始死循環(huán)了
ThreadName = 線程1準(zhǔn)備開始死循環(huán)了
ThreadName = 線程2準(zhǔn)備開始死循環(huán)了
調(diào)用了interrupt()方法
ThreadName = 線程0結(jié)束了
ThreadName = 線程2結(jié)束了
ThreadName = 線程1結(jié)束了

可以看到,ThreadGroup的interrupt方法批量中斷線程組的線程。

關(guān)于Java中線程組的原理是什么就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI