?? threadgrouptest.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 + -