收藏 分销(赏)

进程并发演示模型.doc

上传人:仙人****88 文档编号:11225787 上传时间:2025-07-08 格式:DOC 页数:6 大小:67.38KB 下载积分:10 金币
下载 相关 举报
进程并发演示模型.doc_第1页
第1页 / 共6页
进程并发演示模型.doc_第2页
第2页 / 共6页


点击查看更多>>
资源描述
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.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.white); 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; } }
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 包罗万象 > 大杂烩

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2026 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服