资源简介
创建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)\
- 上一篇:心电信号预处理去噪
- 下一篇:三线程分别显示各自时间
相关资源
- Activiti5.22.0开发指南
- CoreUIVue是基于Bootstrap4的免费Vue管理模
- pthreads-w32-2-9-1-release.zip
- SpringBoot+H2+mybatis-plus59130
- 登录注册界面.zip48872
- 数字华容道
- SSM+Shiro+redis实现单点登陆
- jstl-api-1.2和jstl-impl-1.2
- 基于MVC模式的会员管理系统
- STM32基于rt_thread操作系统的SDHC卡文件
- 国内一家大型软件公司内部的正规软
- 仿windows记事本
- GUI银行管理系统
- 超市收银系统eclipse access大学课程设计
- 模拟ATM柜员机系统--连接数据库
- A*算法的2D演示(带源码)
- 代码审查表和代码审查实例
- 仿126 网易 163 邮箱 界面
- Tomcat6.x
- 简单的行编辑器
- 扫雷(MVC架构)
- 302 Found
- window ping命令加时间并记录日志
- springboot+rabbitmq项目demo(亲测可正常运
- jxbrowser 所有版本通用的破解包
- 2017年-传智播客-张志君老师-SpringBoo
- Blob.js+Export2Excel.js
- 机会路由源代码+仿真工具(SCORP)
- POI中文帮助文档附带api手册.zip
- 2018双十一阿里供应链服务平台讲座
评论
共有 条评论