Java中怎么使用线程组(java,web开发)

时间:2024-05-10 05:57:31 作者 : 石家庄SEO 分类 : web开发
  • TAG :

Java中线程组(ThreadGroup类)

Java中使用ThreadGroup类来代表线程组,表示一组线程的集合,可以对一批线程和线程组进行管理。可以把线程归属到某一个线程组中,线程组中可以有线程对象,也可以有线程组,组中还可以有线程,这样的组织结构有点类似于树的形式,如图所示。

Java中怎么使用线程组

用户创建的所有线程都属于指定线程组,如果没有显式指定属于哪个线程组,那么该线程就属于默认线程组(即main线程组)。默认情况下,子线程和父线程处于同一个线程组。

此外,只有在创建线程时才能指定其所在的线程组,线程运行中途不能改变它所属的线程组,也就是说线程一旦指定所在的线程组就不能改变。

二.为什么要使用线程组

1.安全

同一个线程组的线程是可以相互修改对方的数据的。但如果在不同的线程组中,那么就不能“跨线程组”修改数据,可以从一定程度上保证数据安全。

2.批量管理

可以批量管理线程或线程组对象,有效地对线程或线程组对象进行组织或控制。

三.线程组使用示例

1.线程关联线程组:一级关联

所谓一级关联就是父对象中有子对象,但并不创建孙对象。比如创建一个线程组,然后将创建的线程归属到该组中,从而对这些线程进行有效的管理。代码示例如下:

publicclassThreadGroupTest{publicstaticvoidmain(String[]args){ThreadGrouprootThreadGroup=newThreadGroup("root线程组");Threadthread0=newThread(rootThreadGroup,newMRunnable(),"线程A");Threadthread1=newThread(rootThreadGroup,newMRunnable(),"线程B");thread0.start();thread1.start();}}classMRunnableimplementsRunnable{@Overridepublicvoidrun(){while(!Thread.currentThread().isInterrupted()){System.out.println("线程名:"+Thread.currentThread().getName()+",所在线程组:"+Thread.currentThread().getThreadGroup().getName());try{Thread.sleep(1000);}catch(InterruptedExceptione){e.printStackTrace();}}}}复制代码

执行结果如下:

线程名:线程A,所在线程组:root线程组线程名:线程B,所在线程组:root线程组复制代码

2.线程关联线程组:多级关联

所谓的多级关联就是父对象中有子对象,子对象中再创建孙对象也就出现了子孙的效果了。比如使用下图第二个构造方法,将子线程组归属到某个线程组,再将创建的线程归属到子线程组,这样就会有线程树的效果了。

Java中怎么使用线程组

代码示例如下:

publicclassThreadGroupTest{publicstaticvoidmain(String[]args){ThreadGrouprootThreadGroup=newThreadGroup("root线程组");Threadthread0=newThread(rootThreadGroup,newMRunnable(),"线程A");Threadthread1=newThread(rootThreadGroup,newMRunnable(),"线程B");thread0.start();thread1.start();ThreadGroupthreadGroup1=newThreadGroup(rootThreadGroup,"子线程组");Threadthread2=newThread(threadGroup1,newMRunnable(),"线程C");Threadthread3=newThread(threadGroup1,newMRunnable(),"线程D");thread2.start();thread3.start();}}classMRunnableimplementsRunnable{@Overridepublicvoidrun(){while(!Thread.currentThread().isInterrupted()){System.out.println("线程名:"+Thread.currentThread().getName()+",所在线程组:"+Thread.currentThread().getThreadGroup().getName()+",父线程组:"+Thread.currentThread().getThreadGroup().getParent().getName());try{Thread.sleep(1000);}catch(InterruptedExceptione){e.printStackTrace();}}}}复制代码

执行结果如下:

线程名:线程A,所在线程组:root线程组,父线程组:main线程名:线程B,所在线程组:root线程组,父线程组:main线程名:线程C,所在线程组:子线程组,父线程组:root线程组线程名:线程D,所在线程组:子线程组,父线程组:root线程组复制代码

3.批量管理组内线程

使用线程组自然是要对线程进行批量管理,比如可以批量中断组内线程,代码示例如下:

publicclassThreadGroupTest{publicstaticvoidmain(String[]args){ThreadGrouprootThreadGroup=newThreadGroup("root线程组");Threadthread0=newThread(rootThreadGroup,newMRunnable(),"线程A");Threadthread1=newThread(rootThreadGroup,newMRunnable(),"线程B");thread0.start();thread1.start();ThreadGroupthreadGroup1=newThreadGroup(rootThreadGroup,"子线程组");Threadthread2=newThread(threadGroup1,newMRunnable(),"线程C");Threadthread3=newThread(threadGroup1,newMRunnable(),"线程D");thread2.start();thread3.start();rootThreadGroup.interrupt();System.out.println("批量中断组内线程");}}classMRunnableimplementsRunnable{@Overridepublicvoidrun(){while(!Thread.currentThread().isInterrupted()){System.out.println("线程名:"+Thread.currentThread().getName()+",所在线程组:"+Thread.currentThread().getThreadGroup().getName()+",父线程组:"+Thread.currentThread().getThreadGroup().getParent().getName());try{Thread.sleep(1000);}catch(InterruptedExceptione){e.printStackTrace();break;}}System.out.println(Thread.currentThread().getName()+"执行结束");}}复制代码

执行结果如下:

线程名:线程A,所在线程组:root线程组,父线程组:main线程名:线程B,所在线程组:root线程组,父线程组:main线程名:线程C,所在线程组:子线程组,父线程组:root线程组线程名:线程D,所在线程组:子线程组,父线程组:root线程组批量中断组内线程线程A执行结束线程B执行结束线程C执行结束线程D执行结束复制代码
 </div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
本文:Java中怎么使用线程组的详细内容,希望对您有所帮助,信息来源于网络。
上一篇:java怎么解决猴子吃桃问题下一篇:

5 人围观 / 0 条评论 ↓快速评论↓

(必须)

(必须,保密)

阿狸1 阿狸2 阿狸3 阿狸4 阿狸5 阿狸6 阿狸7 阿狸8 阿狸9 阿狸10 阿狸11 阿狸12 阿狸13 阿狸14 阿狸15 阿狸16 阿狸17 阿狸18