• 大小: 50KB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2021-06-18
  • 语言: 其他
  • 标签: thread  java  

资源简介

创建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$Runnableobject$1.class
     文件        2262  2018-06-22 16:41  11.8(2)\RandomCharacter$Runnableobject.class
     文件        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.applet
     目录           0  2018-09-28 14:30  11.8(2)\

评论

共有 条评论