⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 threadgrouptest.java

📁 java网络高级编程的配套源码,java网络高级编程为清华出版社出版.
💻 JAVA
字号:
/*源代码清单6-2*/

package threadGroup;

import java.awt.*;
import java.awt.event.*;
import borland.jbcl.layout.*;
import borland.jbcl.control.*;
import java.util.*;

public class ThreadGroupTest extends Frame implements ActionListener
{
  PaneLayout paneLayout1 = new PaneLayout();
  PaneLayout paneLayout2 = new PaneLayout();
  PaneLayout paneLayout3 = new PaneLayout();
  PaneLayout paneLayout4 = new PaneLayout();
  GroupBox groupBox1 = new GroupBox();
  GroupBox groupBox2 = new GroupBox();
  GroupBox groupBox3 = new GroupBox();
  Button button1 = new Button();
  Button button2 = new Button();
  Button button3 = new Button();
  Button button4 = new Button();
  Button button5 = new Button();
  Button button6 = new Button();
  Button button7 = new Button();
  Button button8 = new Button();
  Button button9 = new Button();
  List list1 = new List();
  TextArea textArea1 = new TextArea();
  ThreadGroup group=new ThreadGroup("threadGroup");
  int number=1;
  Vector threadVector=new Vector();

  public ThreadGroupTest()
  {
    try
    {
      jbInit();
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
  }

  public static void main(String[] args)
  {
    ThreadGroupTest test=new ThreadGroupTest();
    test.setLocation(100,100);
    test.setSize(450,300);
    test.show();
  }

  private void jbInit() throws Exception
  {
    this.setBackground(new Color(192,192,192));
    groupBox1.setLayout(paneLayout2);
    groupBox1.setLabel("已有线程");
    groupBox2.setLayout(paneLayout3);
    groupBox2.setLabel("应用于单个线程");
    groupBox3.setLayout(paneLayout4);
    groupBox3.setLabel("应用于所有线程");
    button1.setLabel("退出");
    button2.setLabel("启动一个新的线程");
    button3.setLabel("Suspend");
    button4.setLabel("Resume");
    button5.setLabel("Stop");
    button6.setLabel("Suspend");
    button7.setLabel("Resume");
    button8.setLabel("Stop");
    button9.setLabel("当前活动线程统计");
    this.setLayout(paneLayout1);
    this.setSize(new Dimension(369, 300));
    this.setTitle("线程及线程组示例");
    textArea1.setEditable(false);
    this.add(groupBox1, new PaneConstraints("groupBox1", "groupBox1", PaneConstraints.ROOT, 0.5f));
    groupBox1.add(button2, new PaneConstraints("button2", "button2", PaneConstraints.ROOT, 0.5f));
    groupBox1.add(list1, new PaneConstraints("list1", "button2", PaneConstraints.BOTTOM, 0.8125f));
    groupBox1.add(textArea1, new PaneConstraints("textArea1", "list1", PaneConstraints.RIGHT, 0.56363636f));
    groupBox1.add(button9, new PaneConstraints("button9", "button2", PaneConstraints.RIGHT, 0.5590909f));
    this.add(groupBox2, new PaneConstraints("groupBox2", "groupBox1", PaneConstraints.BOTTOM, 0.47985345f));
    groupBox2.add(button3, new PaneConstraints("button3", "button3", PaneConstraints.ROOT, 0.5f));
    groupBox2.add(button4, new PaneConstraints("button4", "button3", PaneConstraints.RIGHT, 0.70090634f));
    groupBox2.add(button5, new PaneConstraints("button5", "button4", PaneConstraints.RIGHT, 0.5905173f));
    this.add(button1, new PaneConstraints("button1", "groupBox2", PaneConstraints.BOTTOM, 0.60305345f));
    this.add(groupBox3, new PaneConstraints("groupBox3", "button1", PaneConstraints.TOP, 0.675f));
    groupBox3.add(button6, new PaneConstraints("button6", "button6", PaneConstraints.ROOT, 0.5f));
    groupBox3.add(button7, new PaneConstraints("button7", "button6", PaneConstraints.RIGHT, 0.69788516f));
    groupBox3.add(button8, new PaneConstraints("button8", "button7", PaneConstraints.RIGHT, 0.57575756f));
    button1.addActionListener(this);
    button2.addActionListener(this);
    button3.addActionListener(this);
    button4.addActionListener(this);
    button5.addActionListener(this);
    button6.addActionListener(this);
    button7.addActionListener(this);
    button8.addActionListener(this);
    button9.addActionListener(this);
  }

  public void actionPerformed(ActionEvent env)
  {
    //得到事件源组件
    Component com=(Component)env.getSource();
	   //事件源为组件类的基类为component类
    if(com.equals(button1))
    {
      System.exit(0);
    }
    else if(com.equals(button2))
    {
      String threadName="thread"+number;
      //创建一个新的线程
      ThreadTest thread=new ThreadTest(this,group,threadName);
      thread.start();
      int priority=Integer.parseInt(threadName.substring(threadName.length()-1));
      if(priority==0)
        priority=1;
      //设置线程优先级  
      thread.setPriority(priority);
      //记录线程
      threadVector.addElement(thread);
      number++;
      //将线程加入到列表中
      list1.add(threadName);
    }
    else if(com.equals(button3))
    {
      String threadName=list1.getSelectedItem();
      if(threadName==null)
        return;
      else
      {
        ThreadTest thread=getThread(threadName);
        //悬挂当前选中线程
        thread.suspend();
        textArea1.append(threadName+"is suspend\n");
      }
    }
    else if(com.equals(button4))
    {
      String threadName=list1.getSelectedItem();
      if(threadName==null)
        return;
      else
      {
        ThreadTest thread=getThread(threadName);
        //继续执行当前选中线程
        thread.resume();
        textArea1.append(threadName+"is resume\n");
      }
    }
    else if(com.equals(button5))
    {
      String threadName=list1.getSelectedItem();
      if(threadName==null)
        return;
      else
      {
        ThreadTest thread=getThread(threadName);
        //停止当前选中线程
        thread.stop();
        list1.remove(threadName);
        //从线程矢量表中删除
        threadVector.removeElement(thread);
        textArea1.append(threadName+"is stop\n");
      }
    }
    else if(com.equals(button6))
    {
      group.suspend();
      //悬挂当前线程组中的所有线程
      textArea1.append("All thread suspend\n");
    }
    else if(com.equals(button7))
    {
      group.resume();
      //继续执行当前线程组中的所有线程
      textArea1.append("All thread resume\n");
    }
    else if(com.equals(button8))
    {
      group.stop();
      //停止当前线程组中的所有线程
      textArea1.append("All thread stop\n");
      list1.removeAll();
      threadVector.removeAllElements();
    }
    else if(com.equals(button9))
    {
      int count=group.activeCount();
      textArea1.append("Active Thread number is:"+count+"\n");
      int maxPriority=group.getMaxPriority();
      textArea1.append("The MaxPriority is:"+maxPriority+"\n");
      String name=group.getName();
      textArea1.append("The groupName is:"+name+"\n");
    }
  }

  //根据线程名得到线程对象
  public ThreadTest getThread(String name)
  {
    ThreadTest thread=null;
    for(int i=0;i<threadVector.size();i++)
    {
      ThreadTest t=(ThreadTest)threadVector.elementAt(i);
      if(name.equals(t.name))
      {
        thread=t;
        break;
      }
    }
    return thread;
  }
  
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -