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

📄 threadgrouptest.java~64~

📁 一些用java写的工程文件
💻 JAVA~64~
字号:
package thread_group;
import java.awt.*;
import java.awt.event.*;
import  java.util.*;
import javax.swing.*;
import com.borland.jbcl.layout.*;
import com.borland.dbswing.*;

public  class ThreadGroupTest   extends   Frame
{
  XYLayout xYLayout1 = new XYLayout();

  JButton jButton1 = new JButton();
  JButton jButton2 = new JButton();
  JButton jButton3 = new JButton();
  JButton jButton4 = new JButton();
  JButton jButton5 = new JButton();
  JButton jButton6 = new JButton();
  JButton jButton7 = new JButton();
  JButton jButton8 = new JButton();
  JButton jButton9 = new JButton();

  JLabel jLabel2 = new JLabel();
  JLabel jLabel3 = new JLabel();


  TextArea textArea2 = new TextArea();
  TextArea textArea1 = new TextArea();

  int number=1;
  ThreadGroup group=new ThreadGroup("threadgroup");
  Vector threadVector=new  Vector();



  public ThreadGroupTest() {
    try {
      jbInit();
    }
    catch(Exception e) {
      e.printStackTrace();
    }
  }
  public static void main(String[]   args)////////main()
  {

    ThreadGroupTest test=new ThreadGroupTest();
    test.setLocation(100,100);
    test.setSize(520,500);
    test.show();
  }
  private void jbInit() throws Exception {

    jButton1.setFont(new java.awt.Font("黑体", 0, 15));
    jButton1.setText("推出");
    jButton1.addActionListener(new ThreadGroupTest_jButton1_actionAdapter(this));
    jButton2.setFont(new java.awt.Font("Dialog", 0, 14));
    jButton2.setDebugGraphicsOptions(0);
    jButton2.setText("启动一个新的线程");
    jButton2.addActionListener(new ThreadGroupTest_jButton2_actionAdapter(this));
    this.setLayout(xYLayout1);
    jButton3.setText("Suspend");
    jButton3.addActionListener(new ThreadGroupTest_jButton3_actionAdapter(this));
    jButton4.setText("Resume");
    jButton4.addActionListener(new ThreadGroupTest_jButton4_actionAdapter(this));
    jButton5.addActionListener(new ThreadGroupTest_jButton5_actionAdapter(this));
    jButton6.addActionListener(new ThreadGroupTest_jButton6_actionAdapter(this));
    jButton7.setText("Resume");
    jButton7.addActionListener(new ThreadGroupTest_jButton7_actionAdapter(this));
    jButton8.setText("Stop");
    jButton8.addActionListener(new ThreadGroupTest_jButton8_actionAdapter(this));
    jLabel2.setFont(new java.awt.Font("黑体", 0, 17));
    jLabel2.setText("应用于单个线程:");
    jLabel3.setFont(new java.awt.Font("黑体", 0, 17));
    jLabel3.setText("应用于线程组:");
    jButton9.setFont(new java.awt.Font("Dialog", 0, 14));
    jButton9.setText("当前活动线程统计 ");
    jButton9.addActionListener(new ThreadGroupTest_jButton9_actionAdapter(this));

    jButton5.setText("Stop");
    jButton6.setText("Suspend");
    xYLayout1.setWidth(524);
    xYLayout1.setHeight(486);


    textArea1.setText("");
    textArea2.setText("");

    this.add(textArea1,  new XYConstraints(267, 44, 238, 171));
    this.add(jButton1, new XYConstraints(1, 434, 494, 29));
    this.add(jButton7,      new XYConstraints(195, 362, 111, 40));
    this.add(jButton8,    new XYConstraints(380, 363, 106, 40));
    this.add(jButton6,    new XYConstraints(14, 360, 111, 42));
    this.add(jLabel3, new XYConstraints(7, 319, -1, -1));
    this.add(jButton4,   new XYConstraints(195, 268, 109, 40));
    this.add(jButton5,    new XYConstraints(378, 268, 108, 39));
    this.add(jLabel2,  new XYConstraints(7, 230, -1, 29));
    this.add(textArea2,   new XYConstraints(14, 43, 226, 173));
    this.add(textArea1,   new XYConstraints(287, 43, 218, 175));
    this.add(jButton9,     new XYConstraints(265, 8, 242, 32));
    this.add(jButton2,     new XYConstraints(15, 6, 226, 34));
    this.add(jButton3,     new XYConstraints(15, 268, 109, 38));




  }


  void jButton1_actionPerformed(ActionEvent e) {
       action_Performed(e);

  }
  void jButton2_actionPerformed(ActionEvent e) {
      action_Performed(e);
}


  void jButton3_actionPerformed(ActionEvent e) {
       action_Performed(e);
  }

  void jButton4_actionPerformed(ActionEvent e) {
        action_Performed(e);
  }

  void jButton5_actionPerformed(ActionEvent e) {
        action_Performed(e);
  }

  void jButton6_actionPerformed(ActionEvent e) {
         action_Performed(e);
  }

  void jButton7_actionPerformed(ActionEvent e) {
          action_Performed(e);
  }

  void jButton8_actionPerformed(ActionEvent e) {
          action_Performed(e);
  }
  void jButton9_actionPerformed(ActionEvent e) {
           action_Performed(e);
 }//////////////////////////////////////////////


public  void  action_Performed(  ActionEvent env){

  Component comp=(Component)env.getSource(); //得到组件源组件
  if(comp.equals(jButton1))
  {
    System.exit(0);
  }

  else  if(comp.equals(jButton2))
  {
    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++;
    // jList1.add(threadName,comp);
     textArea2.append(threadName);
     textArea2.append("\n");
  }
  else if(comp.equals(jButton3))
  {
    String threadName=textArea2.getSelectedText();
    if(threadName==null)
    return;
    else
    {
        ThreadTest thread=getThread(threadName);
        thread.suspend();
        textArea1.append(threadName+"is suspend\n" );

    }
  }
  else if (comp.equals( jButton4))
  {
    String threadName=textArea2.getSelectedText();
    if(threadName==null)
      return;
    else
    {
        ThreadTest thread=getThread(threadName);
        //继续执行当前线程
        thread.resume();
        textArea1.append(threadName+"is resume\n");

    }
  }
  else if(comp.equals( jButton5))
  {
    String threadName=textArea2.getSelectedText();
    if(threadName==null)
      return;
    else
    {
        ThreadTest thread=getThread(threadName);
        thread.stop();
        threadVector.removeElement(thread);
        textArea1.append(threadName+"is stop\n");
    }
  }
  else if (comp.equals(jButton6))
  {
    group.suspend();
    textArea1.append("All thread supended");
  }
  else if (comp.equals(jButton7))
  {
    group.resume();

    textArea1.append("All thread resumed");
  }
  else if (comp.equals(jButton8))
  {
    group.stop();
    textArea1.append("All thread stoped\n");

    threadVector.removeAllElements();
    }
    else if (comp.equals(jButton9)){
      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;
}

}//end of ThreadGroupTest


class ThreadGroupTest_jButton2_actionAdapter implements java.awt.event.ActionListener {
  ThreadGroupTest adaptee;

  ThreadGroupTest_jButton2_actionAdapter(ThreadGroupTest adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.jButton2_actionPerformed(e);
  }
}

class ThreadGroupTest_jButton9_actionAdapter implements java.awt.event.ActionListener {
  ThreadGroupTest adaptee;

  ThreadGroupTest_jButton9_actionAdapter(ThreadGroupTest adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.jButton9_actionPerformed(e);
  }
}

class ThreadGroupTest_jButton1_actionAdapter implements java.awt.event.ActionListener {
  ThreadGroupTest adaptee;

  ThreadGroupTest_jButton1_actionAdapter(ThreadGroupTest adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.jButton1_actionPerformed(e);
  }
}

class ThreadGroupTest_jButton3_actionAdapter implements java.awt.event.ActionListener {
  ThreadGroupTest adaptee;

  ThreadGroupTest_jButton3_actionAdapter(ThreadGroupTest adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.jButton3_actionPerformed(e);
  }
}

class ThreadGroupTest_jButton4_actionAdapter implements java.awt.event.ActionListener {
  ThreadGroupTest adaptee;

  ThreadGroupTest_jButton4_actionAdapter(ThreadGroupTest adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.jButton4_actionPerformed(e);
  }
}

class ThreadGroupTest_jButton5_actionAdapter implements java.awt.event.ActionListener {
  ThreadGroupTest adaptee;

  ThreadGroupTest_jButton5_actionAdapter(ThreadGroupTest adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.jButton5_actionPerformed(e);
  }
}

class ThreadGroupTest_jButton6_actionAdapter implements java.awt.event.ActionListener {
  ThreadGroupTest adaptee;

  ThreadGroupTest_jButton6_actionAdapter(ThreadGroupTest adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.jButton6_actionPerformed(e);
  }
}

class ThreadGroupTest_jButton7_actionAdapter implements java.awt.event.ActionListener {
  ThreadGroupTest adaptee;

  ThreadGroupTest_jButton7_actionAdapter(ThreadGroupTest adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.jButton7_actionPerformed(e);
  }
}

class ThreadGroupTest_jButton8_actionAdapter implements java.awt.event.ActionListener {
  ThreadGroupTest adaptee;

  ThreadGroupTest_jButton8_actionAdapter(ThreadGroupTest adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.jButton8_actionPerformed(e);
  }
}

⌨️ 快捷键说明

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