`
mmdev
  • 浏览: 12922003 次
  • 性别: Icon_minigender_1
  • 来自: 大连
文章分类
社区版块
存档分类
最新评论

线程组ThreadGroup

 
阅读更多
ThreadGroup线程组表示一个线程的集合。此外,线程组也可以包含其他线程组。<wbr style="line-height:25px"><br style="line-height:25px"><wbr style="line-height:25px"><span style="color:#003366; line-height:25px">线程组构成一棵树,在树中,除了初始线程组外,每个线程组都有一个父线程组。</span><br style="line-height:25px"><span style="color:#003366; line-height:25px">允许线程访问有关自己的线程组的信息,但是不允许它访问有关其线程组的父线程组或其他任何线程组的信息</span><wbr style="line-height:25px"><span style="color:#003366; line-height:25px">。</span><br style="line-height:25px"> 构造方法<br style="line-height:25px"><wbr style="line-height:25px"><span style="color:#993300; line-height:25px">public</span><span style="color:#ff6600; line-height:25px">ThreadGroup(Stringname)</span><wbr style="line-height:25px"><br style="line-height:25px"> 构造一个新线程组。新线程组的父线程组是目前正在运行线程的线程组。<br style="line-height:25px"> 不使用任何参数调用父线程组的checkAccess方法;这可能导致一个安全性异常。<br style="line-height:25px"> 参数:<br style="line-height:25px"> name-新线程组的名称。<br style="line-height:25px"> 抛出:<br style="line-height:25px"> SecurityException-如果当前线程不能在指定的线程组中创建线程。<br style="line-height:25px"><wbr style="line-height:25px"><span style="color:#993300; line-height:25px">public</span><span style="color:#ff6600; line-height:25px">ThreadGroup(ThreadGroupparent,Stringname)</span><wbr style="line-height:25px"><br style="line-height:25px"> 创建一个新线程组。新线程组的父线程组是指定的线程组。<br style="line-height:25px"> 不使用任何参数调用父线程组的checkAccess方法;这可能导致一个安全性异常。<br style="line-height:25px"> 参数:<br style="line-height:25px"> parent-父线程组。<br style="line-height:25px"> name-新线程组的名称。<br style="line-height:25px"> 抛出:<br style="line-height:25px"> NullPointerException-如果线程组参数为null。<br style="line-height:25px"> SecurityException-如果当前线程不能在指定的线程组中创建线程。<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px"></wbr></span><span style="color:#003366; line-height:25px">在Java中每个线程都属于某个线程组(ThreadGroup)。</span><br style="line-height:25px"><span style="color:#000080; line-height:25px">例如,如果在main()中产生一个线程,则这个线程属于main线程组(其名字为"main")管理的一员</span><wbr style="line-height:25px">,您可以使用下面的指令来获得目前线程所属的线程组名称:<br style="line-height:25px"><span style="color:#0000ff; line-height:25px">Thread.currentThread().getThreadGroup().getName();</span><br style="line-height:25px"><span style="color:#000080; line-height:25px">每一个线程产生时,都会被归入某个线程组,视线程是在哪个线程组中产生而定。如果没有指定,则归入产生该子线程的线程的线程组中。<br style="line-height:25px"> 您也可以自行指定线程组,线程一旦归入某个组,就无法更换组</span>。<br style="line-height:25px"> java.lang.ThreadGroup类正如其名,可以统一管理整个线程组中的线程,您可以使用以下方式来产生线程组,而且一并指定其线程组:<br style="line-height:25px"><span style="color:#3366ff; line-height:25px">ThreadGroupthreadGroup1=newThreadGroup("group1");<br style="line-height:25px"> ThreadGroupthreadGroup2=newThreadGroup("group2");<br style="line-height:25px"> Threadthread1=newThread(threadGroup1,"group1'smember");<br style="line-height:25px"> Threadthread2=newThread(threadGroup2,"group2'smember");</span><br style="line-height:25px"><span style="color:#ff00ff; line-height:25px">ThreadGroup</span><span style="color:#003366; line-height:25px">中的某些方法,可以对所有的线程产生作用,例如interrupt()方法可以interrupt线程组中所有的线程,</span><br style="line-height:25px"><span style="color:#ff6600; line-height:25px">setMaxPriority()</span><span style="color:#003366; line-height:25px">方法可以设置线程组中线程所能拥有的最高优先权(本来就拥有更高优先权的线程不受影响)。<br style="line-height:25px"> 如果您想要一次获得线程组中所有的线程来进行某种操作,可以使用enumerate()方法,</span>例如:<br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Thread[]threads=newThread[threadGroup1.activeCount()];<br style="line-height:25px"> threadGroup1.enumerate(threads);</span><br style="line-height:25px"> activeCount()方法获得线程组中正在运行的线程数量,enumerate()方法要传入一个Thread数组,<br style="line-height:25px"> 它将线程对象设置到每个数组字段中,然后就可以通过数组索引来操作这些线程。<br style="line-height:25px"><span style="color:#ff00ff; line-height:25px">ThreadGroup</span>中有一个<span style="color:#ff6600; line-height:25px">uncaughtException()</span>方法。当线程组中某个线程发生Uncheckedexception异常时,<br style="line-height:25px"> 由执行环境调用此方法进行相关处理,如果有必要,您可以重新定义此方法,直接使用范例15.9来示范如何实现。<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">范例1</wbr></span> <div style="line-height:25px">ThreadGroupDemo.java文件<br style="line-height:25px"><span style="color:#3366ff; line-height:25px">packageonlyfun.caterpillar;<br style="line-height:25px"> importjava.io.*;<br style="line-height:25px"></span><span style="color:#993300; line-height:25px">publicclass</span><span style="color:#3366ff; line-height:25px"></span><span style="color:#ff6600; line-height:25px">ThreadGroupDemo</span><span style="color:#3366ff; line-height:25px">{<br style="line-height:25px"></span><span style="color:#993300; line-height:25px">publicstaticvoid</span><span style="color:#3366ff; line-height:25px"></span><span style="color:#ff6600; line-height:25px">main</span><span style="color:#3366ff; line-height:25px">(String[]args){<br style="line-height:25px"> ThreadGroupthreadGroup1=<br style="line-height:25px"></span><span style="color:#808080; line-height:25px">//这是匿名类写法</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">newThreadGroup("group1"){</span><br style="line-height:25px"><span style="color:#808080; line-height:25px">//继承ThreadGroup并重新定义以下方法<br style="line-height:25px"> //在线程成员抛出uncheckedexception<br style="line-height:25px"> //会执行此方法</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">publicvoiduncaughtException(Threadt,Throwablee){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">System.out.println(t.getName()+":"</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">+e.getMessage());</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">};</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#808080; line-height:25px">//这是匿名类写法</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Threadthread1=</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#808080; line-height:25px">//这个线程是threadGroup1的一员</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">newThread(threadGroup1,</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">newRunnable(){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">publicvoidrun(){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#808080; line-height:25px">//抛出unchecked异常</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">thrownewRuntimeException("测试异常");</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">});</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">thread1.start();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span>在<span style="color:#0000ff; line-height:25px">uncaughtException()</span>方法的参数中,第一个参数可以获得发生异常的线程实例,而第二个参数可以获得异常对象,<br style="line-height:25px"> 范例中显示了线程的名称及异常信息,结果如下所示:<br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Thread-0:测试异常</span><br style="line-height:25px"> 在J2SE5.0之前,如果要统一处理某些线程的UncheckedException,可以使用ThreadGroup来管理。<br style="line-height:25px"> 在继承ThreadGroup之后重新定义其uncaughtException()方法,就如范例1所示。在J2SE5.0之后,就不用这么麻烦,<br style="line-height:25px"> 可以让异常处理类使用Thread.UncaughtExceptionHandler接口,并实现其uncaughtException()方法,<br style="line-height:25px"> 例如可以改写范例1,首先定义一个类让其实现Thread.UncaughtExceptionHandler接口。<br style="line-height:25px"> 范例2</div> <div style="line-height:25px">ThreadExceptionHandler.java文件<br style="line-height:25px"><span style="color:#3366ff; line-height:25px">packageonlyfun.caterpillar;</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">publicclass</span><span style="color:#3366ff; line-height:25px"></span><span style="color:#ff6600; line-height:25px">ThreadExceptionHandler</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">implements</span><span style="color:#3366ff; line-height:25px">Thread.UncaughtExceptionHandler{</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">publicvoid</span><span style="color:#3366ff; line-height:25px">uncaughtException(Threadt,Throwablee){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">System.out.println(t.getName()+":"</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">+e.getMessage());</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"> 接着范例1主流程可以改写为范例3。<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">范例3</wbr></span>ThreadGroupDemo2.java<br style="line-height:25px"><span style="color:#3366ff; line-height:25px">packageonlyfun.caterpillar;<br style="line-height:25px"> importjava.io.*;<br style="line-height:25px"></span><span style="color:#993300; line-height:25px">publicclass</span><span style="color:#3366ff; line-height:25px"></span><span style="color:#ff6600; line-height:25px">ThreadGroupDemo2</span><span style="color:#3366ff; line-height:25px">{<br style="line-height:25px"></span><span style="color:#993300; line-height:25px">publicstaticvoid</span><span style="color:#3366ff; line-height:25px"></span><span style="color:#ff6600; line-height:25px">main</span><span style="color:#3366ff; line-height:25px">(String[]args){<br style="line-height:25px"></span><span style="color:#808080; line-height:25px">//建立异常处理者</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">ThreadExceptionHandlerhandler=</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">newThreadExceptionHandler();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">ThreadGroupthreadGroup1=newThreadGroup("group1");</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#808080; line-height:25px">//这是匿名类写法</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Threadthread1=</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#808080; line-height:25px">//这个线程是threadGroup1的一员</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">newThread(threadGroup1,</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">newRunnable(){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">publicvoidrun(){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#808080; line-height:25px">//抛出unchecked异常</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">thrownewRuntimeException("测试异常");</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">});</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#808080; line-height:25px">//设置异常处理者</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#0000ff; line-height:25px">thread1.setUncaughtExceptionHandler(handler);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">thread1.start();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"> 需要注意的函数:<br style="line-height:25px"> publicfinalvoidcheckAccess()<br style="line-height:25px"> 确定当前运行的线程是否有权修改此线程组。<br style="line-height:25px"> 如果有安全管理器,则用此线程组作为其参数调用checkAccess方法。结果可能是抛出一个SecurityException。<br style="line-height:25px"> 抛出:<br style="line-height:25px"> SecurityException-如果不允许当前线程访问此线程组。<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意</wbr></span><wbr style="line-height:25px">:<span style="color:#000080; line-height:25px">它返回的居然是</span><span style="color:#0000ff; line-height:25px">void</span><span style="color:#000080; line-height:25px">,而不是</span><span style="color:#0000ff; line-height:25px">boolean</span><span style="color:#000080; line-height:25px">.其实如果当前运行的线程没有权修改此线程组,则抛出一个</span><span style="color:#ff9900; line-height:25px">SecurityException</span><span style="color:#000080; line-height:25px">。<br style="line-height:25px"> 否则不抛出</span>。<br style="line-height:25px"><wbr style="line-height:25px"><span style="color:#993300; line-height:25px">publicint</span><span style="color:#ff6600; line-height:25px">enumerate(Thread[]list)</span><wbr style="line-height:25px"><br style="line-height:25px"> 把此线程组及其子组中的所有活动线程复制到指定数组中。<br style="line-height:25px"> 首先,不使用任何参数调用此线程组的checkAccess方法;这可能导致一个安全性异常。<br style="line-height:25px"> 应用程序可以使用activeCount方法获取数组大小的估计数,但是,如果数组太小而无法保持所有线程,则忽略额外的线程。如果获得此线程组及其子组中的所有活动线程非常重要,则调用方应该验证返回的int值是否严格小于list的长度。<br style="line-height:25px"> 由于使用此方法所固有的竞争条件,建议只将此方法用于信息目的。<br style="line-height:25px"> 参数:<br style="line-height:25px"> list-放置线程列表的数组。<br style="line-height:25px"> 返回:<br style="line-height:25px"> 放入数组中的线程数。<br style="line-height:25px"> 抛出:<br style="line-height:25px"> SecurityException-如果不允许当前线程枚举此线程组。<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意</wbr></span><wbr style="line-height:25px">:<wbr style="line-height:25px"><span style="color:#000080; line-height:25px">它的目的是把子组中的所有活动线程复制到指定数组list中。还有一些enumerate的函数,具体参考文档</span><wbr style="line-height:25px">。<br style="line-height:25px"> publicfinalvoidinterrupt()<br style="line-height:25px"> 中断此线程组中的所有线程。<br style="line-height:25px"> 首先,不使用任何参数调用此线程组的checkAccess方法;这可能导致一个安全性异常。<br style="line-height:25px"> 然后,此方法将对此线程组及其所有子组中的所有线程调用interrupt方法。<br style="line-height:25px"> 抛出:<br style="line-height:25px"> SecurityException-如果不允许当前线程访问此线程组或线程组中的任何线程<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意:</wbr></span><span style="color:#000080; line-height:25px">线程组的目的就是对线程进行管理。interrupt()是其中的一个接口</span><wbr style="line-height:25px">。</wbr></wbr></wbr></wbr></wbr></wbr></wbr> </div> </wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics