本人是一名初学者,因为在需要消费者生产者问题的代码就去网上百度了一番,找到了大佬们传播的知识,因此深受感动,因此也把我的代码放上来了。
本人是用Eclipse 应用,Java语言。
废话不多说,直接上代码。
package ****;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ThreadProcess {
public static void main(String []arg) {
ThreadFrame threadFrame=new ThreadFrame();
threadFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//框架关闭时退出程序
Sign1 sign1=new Sign1(ThreadFrame.jpl1);
Sign2 sign2=new Sign2(ThreadFrame.jpl2);
Sign3 sign3=new Sign3(ThreadFrame.jpl3);
while (true) {
if(sign1.flag==true) {
sign1.run();
}
else {
sign1.yield();
}
if(sign2.flag==true) {
sign2.run();
}
else {
sign2.yield();
}
if(sign3.flag==true) {
sign3.run();
}
else {
sign3.yield();
}
}
}
}
class ThreadFrame extends JFrame {
public static Jpl1 jpl1=new Jpl1();
public static Jpl2 jpl2=new Jpl2();
public static Jpl3 jpl3=new Jpl3();
public ThreadFrame() {
super("模拟进程的并发执行");
setSize(800,600);
setLocationRelativeTo(null);//把窗口位置设置到屏幕中心
Container con=this.getContentPane();//创建容器
GridLayout gridLayout=new GridLayout(1,3);//创建布局,一行三列
con.setLayout(gridLayout);
con.add(jpl1);
con.add(jpl2);
con.add(jpl3);
setVisible(true);//显示GUI组件的
}
}
class Jpl1 extends JPanel {
JButton btn1;
public static JLabel lab1=new JLabel("模拟进程1正进行");
public Jpl1() {
setLayout(null);
setBorder(BorderFactory.createTitledBorder("生产者"));
btn1=new JButton("暂停");
btn1.setBounds(90,500,70,35);
add(btn1);add(lab1);
btn1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
if(Sign1.flag==true)
{
Sign1.flag=false;
btn1.setText("执行");
lab1.setText("模拟进程1已暂停");
}
else
{
Sign1.flag=true;
btn1.setText("暂停");
lab1.setText("模拟进行1正进行");
}
}
}); //按钮监控事件
}
public void paint(Graphics g) {
super.paint(g);
g.drawLine(300,450, 0, 450);//画线,点到点
g.drawLine(300,445, 0, 445);
g.drawRoundRect(Sign1.x+80,Sign1.y, 100, 40, 50, 0);//画方块
g.setColor(Color.blue);
lab1.setBounds(Sign1.x+85,Sign1.y,110,35);
}
}
class Jpl2 extends JPanel {
JButton btn2;
public static JLabel lab2=new JLabel("模拟进程2正进行");
public Jpl2() {
setLayout(null);
setBorder(BorderFactory.createTitledBorder("模拟进程2"));
btn2=new JButton("暂停");
btn2.setBounds(100,500,70,35);
add(btn2);add(lab2);
btn2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
if(Sign2.flag==true) {
Sign2.flag=false;
btn2.setText("执行");
lab2.setText("模拟进行2已暂停");
}
else {
Sign2.flag=true;
btn2.setText("暂停");
lab2.setText("模拟进行2正进行");
}
}
});
}
public void paint(Graphics g) {
super.paint(g);
g.drawLine(300,450, 0, 450);
g.drawLine(300,445, 0, 445);
g.drawRoundRect(Sign2.x2+85,Sign2.y2, 100, 40, 50, 0);
lab2.setBounds(Sign2.x2+90,Sign2.y2,110,35);
}
}
class Jpl3 extends JPanel {
JButton btn3;
public static JLabel lab3=new JLabel("模拟进程3正进行");
public Jpl3() {
setLayout(null);
setBorder(BorderFactory.createTitledBorder("模拟进程3"));
btn3 = new JButton("暂停");
btn3.setBounds(105,500,70,35);
add(btn3);add(lab3);
btn3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
if(Sign3.flag==true) {
Sign3.flag=false;
btn3.setText("执行");
lab3.setText("模拟进程1已暂停");
}
else {
Sign3.flag=true;
btn3.setText("暂停");
lab3.setText("模拟进程1正进行");
}
}
});
}
public void paint(Graphics g) {
super.paint(g);
g.drawLine(300,450, 0, 450);
g.drawLine(300,445, 0, 445);
g.drawRoundRect(Sign3.x3+90,Sign3.y3, 100, 40, 50, 0);
lab3.setBounds(Sign3.x3+95,Sign3.y3,110,35);
}
}
class Sign1 extends Thread {
public static boolean flag=true;//设置变量,判断状态
public Jpl1 jp1;
public static int x;
public static int y;
public Sign1(Jpl1 jpl1) {
this.jp1=jpl1;
}
@Override
public void run() {
if(y<405) {
y+=10;
jp1.repaint();
try {
Thread.sleep(30);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
else {
y=0;
}
}
}
class Sign2 extends Thread {
public static boolean flag=true;
public Jpl2 jp2;
public static int x2;
public static int y2;
public Sign2(Jpl2 panel2) {
this.jp2=panel2;
}
@Override
public void run() {
if(y2<405) {
y2+=10;
jp2.repaint();
try {
Thread.sleep(30);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
else {
y2=0;
}
}
}
class Sign3 extends Thread {
public static boolean flag=true;
public Jpl3 jp3;
public static int x3;
public static int y3;
public Sign3(Jpl3 panel3) {
this.jp3=panel3;
}
public void run() {
if(y3<405) {
y3+=10;
jp3.repaint();
try {
Thread.sleep(30);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
else {
y3=0;
}
}
}
若有什么不正确的地方,希望大佬们的指导留言。