import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class DuoXiancheng extends JApplet implements ActionListener {
//用applet定义变量和类R
private JLabel infor1;
private JLabel infor2;
private JLabel infor3;
private JLabel infor4;
private JLabel infor5;
private JLabel infor6;
private JPanel inforPanel;
private JPanel inforPanel2;
private JLabel shuchus[];
private JCheckBox annius[];
private Thread xianchengs[];
private boolean zhanting[];
private String colorNames[] = { "Black", "Blue", "Cyan",
"Dark Gray", "Gray", "Green", "Light Gray",
"Magenta", "Orange", "Pink", "Red", "White", "Yellow","Red","Blue" };
private Color colors[] = { Color.black, Color.blue,
Color.cyan, Color.darkGray, Color.gray, Color.green,
Color.lightGray, Color.magenta, Color.orange, Color.pink,
Color.red, Color.white, Color.yellow,Color.red,Color.blue, };
// 设置GUI 和数组
public void init()
{
shuchus= new JLabel[15];
annius = new JCheckBox[15];
xianchengs = new Thread[15];
zhanting = new boolean[15];
JLabel infor1=new JLabel("");
JLabel infor2=new JLabel(" 多线程演示程序");
JLabel infor3=new JLabel(" 江南大学");
JLabel infor4=new JLabel(" 信息工程学院");
JLabel infor5=new JLabel(" 软件0202班 刘昌劲");
JLabel infor6=new JLabel(" 6号");
JPanel inforPanel=new JPanel();
inforPanel.setLayout(new GridLayout(1,3));
inforPanel.add(infor1);
inforPanel.add(infor2);
inforPanel.add(infor3);
inforPanel.add(infor4);
inforPanel.add(infor5);
inforPanel.add(infor6);
JPanel inforPanel2=new JPanel();
inforPanel2.setLayout(new GridLayout(5,6,7,7));
Container container = getContentPane();
container.add(inforPanel,BorderLayout.NORTH);
container.add(inforPanel2,BorderLayout.CENTER);
// 创建GUI元素,注册listeners 并关联到content pane
for ( int c = 0; c < 15; c++ ) {
annius[c] = new JCheckBox( "线程"+(c+1)+"停止/开始");
annius[c] .addActionListener( this );
annius[c].setBackground(Color.orange);
inforPanel2.add(annius[c]);
shuchus[c] = new JLabel();
shuchus[c].setOpaque( true );
inforPanel2.add( shuchus[c] );
}
}
//创建和开始线程方法,该方法在init之后和用户背调用
public void start()
{
// 创建线程,每次start()开始时就运行
for (int c=0;c<xianchengs.length; c++) {
// 创建线程,用Runnable对象R初始化
xianchengs[c] = new Thread( new R(),
"第"+(c+1)+"个线程");
//执行线程
xianchengs[c].start();
}
}
// 寻找线程在数组中的位置
private int zhiyin( Thread liuxing)
{
for ( int a=0; a<xianchengs.length;a++)
if (liuxing==xianchengs[a] )
return a;
return -1;
}
//当用户选中按钮就调用该方法停止线程
public synchronized void stop()
{
// 开始令每个线程都运有�
for ( int b=0; b<xianchengs.length; b++ )
xianchengs[b] = null;
// 等待的线程执行
notifyAll();
}
//按钮事件处理
public synchronized void actionPerformed( ActionEvent event )
{
for ( int d=0; d<annius.length;d++ ) {
if ( event.getSource() == annius[d] ) {
zhanting[d] = !zhanting[d];
// 使线程重新运行
if ( !zhanting[d] )
notifyAll();
return;
}
}
}
//inner class R
private class R implements Runnable {
public void run()
{
// 取得当前运行的线程
final Thread currentThread = Thread.currentThread();
// 确定线程位置
final int zhi= zhiyin( currentThread );
while ( xianchengs[zhi] == currentThread ) {
// 0 to 0.25 秒时间阻塞
try {
Thread.sleep( ( int ) ( Math.random() * 250 ) );
// 判断线程时否该停止
synchronized( DuoXiancheng.this ) {
while ( zhanting[zhi] &&
xianchengs[zhi] == currentThread ) {
//暂停运行线程
DuoXiancheng.this.wait();
}
}
}
//当有异常时InterruptedExceptions处理
catch ( InterruptedException interruptedException ) {
System.err.println( "sleep interrupted" );
}
// display color on corresponding label
SwingUtilities.invokeLater(
new Runnable() {
//刷新Swing GUI元素
public void run()
{
// 得随机颜色
int t=( int ) ( Math.random() *15 ) ;
Color xianshi=colors[t];
shuchus[zhi].setBackground(xianshi);
shuchus[zhi].setText(
currentThread.getName() + ": " +
colorNames[t]);
}
} // 结束 anonymous inner class
); // 结束调用SwingUtilities.invokeLater
} // while结束
}
} //inner class R 结束
} //类结束
DuoXiancheng.rar_java多线程
版权申诉
66 浏览量
2022-09-19
12:39:24
上传
评论
收藏 11KB RAR 举报
御道御小黑
- 粉丝: 61
- 资源: 1万+
最新资源
- 基于SHT25温湿度传感器、FREERTOS、STM32F103C8T6、LCD1602温湿度采集显示系统proteus仿真设计
- C# 屏幕放大取色器 随时随地获取屏幕像素颜色
- 下载安装这个软件.apk
- 【数据集详细解释及案例分析】数据集详细解释及案例分析
- 基于SHT71温湿度传感器、STM32F103C8T6、LCD1602温湿度采集显示系统proteus仿真设计
- 基于TH02温湿度传感器、STM32F103C8T6、LCD1602、FREERTOS的温湿度采集系统proteus仿真设计
- 【TCP-IP协议详细解释及案例分析】TCP-IP协议详细解释及案例分析
- 一文搞懂 LSTM(长短期记忆网络).rar
- 【autosar简介及基本案例解析】autosar简介及基本案例解析
- java模拟斗地主洗牌发牌
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈