資源簡介
創(chuàng)建3個線程,每個線程的工作是在自己的界面隨機的顯示26個字母中的一個;可通過界面按鈕,臨時掛起或回復各線程的運行。

代碼片段和文件信息
package?randomcharacter;
import?java.awt.*;
import?java.awt.event.*;
import?javax.swing.*;
public?class?RandomCharacter?extends?Japplet?implements?ActionListener{
????private?String?allchars?=?“ABCDEFGHIJKLMNOPQRSTUVWXYZ“;
????private?final?static?int?SIZE?=?3;
????private?JLabel?[]outputs;
????private?JCheckBox?[]checkBoxes;
????private?Thread?[]threads;
????private?boolean?[]suspended;
????static?int?count?=?0;
????public?void?init(){
????????outputs?=?new?JLabel[SIZE];
????????checkBoxes?=?new?JCheckBox[SIZE];
????????threads?=?new?Thread[SIZE];
????????suspended?=?new?boolean[SIZE];
????????Container?container?=?getContentPane();
????????container.setLayout(new?GridLayout(SIZE255));
????????for(int?count?=?0;count ????????????outputs[count]?=?new?JLabel();
????????????outputs[count].setBackground(Color.GREEN);
????????????outputs[count].setOpaque(true);
????????????container.add(outputs[count]);
????????????checkBoxes[count]?=?new?JCheckBox(“線程掛起“);
????????????checkBoxes[count].addActionListener(this);
????????????container.add(checkBoxes[count]);
????????}
????}
????public?void?start(){
????????for(int?count?=?0;count ????????????threads[count]?=?new?Thread(new?Runnableobject()“線程“+(count+1));
????????????threads[count].start();
????????}
????}
????private?int?getIndex(Thread?current){
????????for(int?count?=?0;count ????????????if(current?==?threads[count]){
????????????????return?count;
????????????}
????????}
????????return?-1;
????}
????public?synchronized?void?stop(){
????????for(int?count?=?0;count ????????????threads[count]?=?null;
????????}
????????notifyAll();
????}
????public?synchronized?void?actionPerformed(ActionEvent?event){
????????for(int?count?=?0;count ????????????if(event.getSource()==checkBoxes[count]){
????????????????suspended[count]?=!suspended[count];
????????????????outputs[count].setBackground(suspended[count]?Color.MAGENTA:Color.GREEN);
????????????????if(!suspended[count]){
????????????????????notifyAll();
????????????????}
????????????????return;
????????????}
????????}
????}
????private?class?Runnableobject?implements?Runnable{
????????public?void?run(){
????????????final?Thread?currentThread?=?Thread.currentThread();
????????????final?int?index?=?getIndex(currentThread);
????????????System.err.println(“index=“+index+“thread=“+currentThread.getName());
????????????while?(threads[index]?==?currentThread){
????????????????try{
????????????????????Thread.sleep((int)(Math.random()*1000));
????????????????????synchronized?(RandomCharacter.this){
????????????????????????while(suspended[index]&&threads[index]==currentThread){
????????????????????????????RandomCharacter.this.wait();
????????????????????????}
????????????????????}
????????????????}
????????????????catch?(InterruptedException?e){
????????????????????e.printStackTrace();
????????????????}
????????????????SwingUtilities.invokeL
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????16763??2018-06-22?17:00??11.8(2)\20180622170012.png
?????文件????????1546??2018-06-22?16:41??11.8(2)\RandomCharacter$Runnableob
?????文件????????2262??2018-06-22?16:41??11.8(2)\RandomCharacter$Runnableob
?????文件????????3543??2018-06-22?16:41??11.8(2)\RandomCharacter.class
?????文件????????3430??2018-05-28?21:14??11.8(2)\RandomCharacter.java
?????文件???????32768??2018-06-22?17:01??11.8(2)\Thumbs.db
?????文件?????????141??2018-06-22?16:41??11.8(2)\java.policy.ap
?????目錄???????????0??2018-09-28?14:30??11.8(2)\
- 上一篇:心電信號預處理去噪
- 下一篇:三線程分別顯示各自時間
評論
共有 條評論