当前位置:文档之家› Java进程并发演示模型

Java进程并发演示模型

进程并发演示模型
一实验截图
二代码
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
{
CZPanel panel;
int st=0;
static int num=3;
public CZThread(int n)
{
panel=new CZPanel(n);
panel.setBounds(0,130+n*100,380,60);
}
public void S()
{
}
public void run()
{
try
{
while(true)
{
while(panel.m<=330&&panel.flag==0)
{
panel.repaint();
sleep(20*num);
}
panel.m=330;
panel.flag=1;
while(panel.m>=20&&panel.flag==1)
{
panel.repaint();
sleep(20*num);
}
panel.m=20;
panel.flag=0;
}
}
catch(Exception e)
{}
}
private class CZPanel extends JPanel
{
public CZPanel(int n)
{
this.n=n;
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.setColor(Color.cyan);
g.fillRect(0,0,20,30);
g.setColor(Color.blue);
g.fillRect(0,30,20,30);
g.setColor(Color.yellow);
g.fillRect(20,0,359,59);
g.setColor(Color.green);
if(m<=330&&flag==0)
{
g.fillRect(m,0,50,60);
g.setColor(Color.red);
g.drawString("进程"+(n+1),m+10,28);
m+=20;
}
else if(m>=20&&flag==1)
{
g.fillRect(m,0,50,60);
g.setColor(Color.red);
g.drawString("进程"+(n+1),m+10,28);
m-=20;
}
}
int m=20,n=0,flag=0;
}
}。

相关主题