Java进程并发演示模型
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
进程并发演示模型
一实验截图
二代码
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CZFrame extends JFrame implements ActionListener
{
CZThread thread1,thread2,thread3;
JButton wait1=new JButton("等待"),wait2=new JButton("等待"),wait3=new JButton("等待");
JButton exit=new JButton("退出");
JLabel name=new JLabel("作者:田毛毛");
public CZFrame(int n1,int n2,int n3)
{
setTitle("进程并发演示模型");
setSize(500,550);
setVisible(true);
thread1=new CZThread(n1);
thread2=new CZThread(n2);
thread3=new CZThread(n3);
Container container=getContentPane();
container.setBackground(Color.pink);
container.setLayout(null);
container.add(thread1.panel);
container.add(thread2.panel);
container.add(thread3.panel);
wait1.setBounds(400,150,70,30);
wait2.setBounds(400,250,70,30);
wait3.setBounds(400,350,70,30);
exit.setBounds(200,450,70,30);
container.add(wait1);
container.add(wait2);
container.add(wait3);
container.add(exit);
wait1.addActionListener(this);
wait2.addActionListener(this);
wait3.addActionListener(this);
exit.addActionListener(this);
JLabel label=new JLabel("进程并发演示模型");
Font font=new Font("宋体",Font.BOLD,16);
label.setFont(font);
label.setBounds(175,20,180,50);
container.add(label);
name.setBounds(400,40,100,50);
container.add(name);
thread1.start();
thread2.start();
thread3.start();
}
public void actionPerformed(ActionEvent e)
{
Object s=e.getSource();
if(s==exit)
{
/*thread1.suspend();
thread2.suspend();
thread3.suspend();*/
int i=JOptionPane.showConfirmDialog(null,"确认退出吗?","退出",JOptionPane.YES_NO_OPTION);
if(i==0) System.exit(0);
/*thread1.resume();
thread2.resume();
thread3.resume();*/
}
else if(s==wait1)
{
try
{
if(thread1.st==0)
{
thread1.suspend();
thread1.st=1;
wait1.setText("执行");
thread1.num--;
}
else
{
thread1.resume();
thread1.st=0;
wait1.setText("等待");
thread1.num++;
}
}
catch(Exception e1)
{}
}
else if(s==wait2)
{
try
{
if(thread2.st==0)
{
thread2.suspend();
thread2.st=1;
wait2.setText("执行");
thread1.num--;
}
else
{
thread2.resume();
thread2.st=0;
wait2.setText("等待");
thread1.num++;
}
}
catch(Exception e1)
{}
}
else
{
try
{
if(thread3.st==0)
{
thread3.suspend();
thread3.st=1;
wait3.setText("执行");
thread1.num--;
}
else
{
thread3.resume();
thread3.st=0;
wait3.setText("等待");
thread1.num++;
}
}
catch(Exception e1)
{}
}
}
public static void main(String args[])
{
int n1=0,n2=1,n3=2;
CZFrame frame=new CZFrame(n1,n2,n3);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.show();
}
}
class CZThread extends Thread