在界面里面加入一个暂停按钮,可以在运行的时候随时暂停

szz958589978 2016-12-22 09:54:20

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

//窗口类
class window extends JFrame{

public JFrame jf;
public JPanel jp3;
public ScrollPane sp1,sp2,sp3;
public Container c;
window(){
jf=new JFrame();
jp3=new JPanel();
sp1=new ScrollPane();
sp2=new ScrollPane();
sp3=new ScrollPane();
c=getContentPane();
c.setLayout(new GridLayout(2,2,10,10));
jf.add(c);
c.add(sp1);
c.add(sp2);
c.add(jp3);
c.add(sp3);

jf.setSize(400,300);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

//Share 类
class Share{
private int u,jishu=0;
private boolean available=false;
public JTextArea jt=new JTextArea("==========生产消费状态==========\n\n");
//同步方法
public synchronized int get(){
jishu++;
while(available==false){
try{wait();}
catch(InterruptedException e){}
jt.append(jishu+" Share中没有资源,消费者等待……\n");
}
available=false;
notifyAll();
jt.append(jishu+" 正在唤醒生产者生产……\n");
return u;
}
public synchronized void put(int value){
jishu++;
while(available==true){
try{wait();}
catch(InterruptedException e){}
jt.append(jishu+" Share中已有资源,生产者等待……\n");
}
u=value;
available=true;
notifyAll();
jt.append(jishu+" 正在唤醒消费者消费……\n");
}
//互斥方法
public int hget(){
jishu++;
jt.append(jishu+" 消费者 正在消费资源……\n");
return u;
}
public void hput(int value){
jishu++;
jt.append(jishu+" 生产者 正在生产资源……\n");
u=value;
}

}

//生产者类
class Producer extends Thread{
private Share shared;
public String jieguo[]=new String[11];
public JTextArea jt=new JTextArea("==========生产者进程==========\n\n");

public Producer(Share s){
shared=s;
}
public void run(){
for(int i=1;i<11;i++){
shared.put(i);
jieguo[i]=i+" 生产者第"+i+"次生产"+" 生产者的生产数据:"+i+"\n";
jt.append(jieguo[i]);
try{
//sleep((int)(Math.random()*100));
sleep(1000);
}catch(InterruptedException e){}

}
}

}

class hProducer extends Thread{
private Share shared;
public String jieguo[]=new String[11];
public JTextArea jt=new JTextArea("==========生产者线程==========\n\n");

public hProducer(Share s){
shared=s;
}
public void run(){
for(int i=1;i<11;i++){
shared.hput(i);
jieguo[i]=i+" 生产者第"+i+"次生产"+" 生产者的生产数据:"+i+"\n";
jt.append(jieguo[i]);
try{
//sleep((int)(Math.random()*100));
sleep(1000);
}catch(InterruptedException e){}

}
}

}

//消费者类
class Consumer extends Thread{
private Share shared;
public String jieguo[]=new String[11];
public JTextArea jt=new JTextArea("==========消费者线程==========\n\n");

public Consumer(Share s){
shared=s;
}
public void run(){
int value=0;
for(int i=1;i<11;i++){
value=shared.get();
jieguo[i]=i+" 消费者第"+i+"次消费"+" 消费者获得的生产数据:"+value+"\n";
jt.append(jieguo[i]);
try{
//sleep((int)(Math.random()*100));
sleep(1000);
}catch(InterruptedException e){}
}
}
}

class hConsumer extends Thread{
private Share shared;
public String jieguo[]=new String[11];
public JTextArea jt=new JTextArea("==========消费者线程==========\n\n");

public hConsumer(Share s){
shared=s;
}
public void run(){
int value=0;
for(int i=1;i<11;i++){
value=shared.hget();
jieguo[i]=i+" 消费者第"+i+"次消费"+" 消费者获得的生产数据:"+value+"\n";
jt.append(jieguo[i]);
/*try{
//sleep((int)(Math.random()*100));
sleep(1000);
}catch(InterruptedException e){}*/
}
}
}

//程序入口
public class PCmx{

public static void main(String[] args){
window win=new window();
BHandler h=new BHandler();
BHandler1 h1=new BHandler1();
JButton jb=new JButton("开始同步线程读写");
JButton jb1=new JButton("开始互斥线程读写");
JButton jb2=new JButton("暂停");
win.jf.setTitle("生产者与消费者同步与互斥演示");
jb.addActionListener(h);
jb1.addActionListener(h1);

win.jp3.add(jb);
win.jp3.add(jb1);
h.winadd(win.jf,win.sp1,win.sp2,win.sp3,win.jp3,win.jp3);
h1.winadd(win.jf,win.sp1,win.sp2,win.sp3,win.jp3,win.jp3);
}
}

//同步按钮事件
class BHandler implements ActionListener{
private ScrollPane sp1,sp2,sp3;
private JPanel jp1,jp2;
private JFrame jf;
JTextArea jt1=new JTextArea();
JTextArea jt2=new JTextArea();
public void actionPerformed(ActionEvent e){
jf.setTitle("生产者与消费者的同步与互斥- 同步演示");
Share s=new Share();
Producer p=new Producer(s);
Consumer c=new Consumer(s);
sp1.add(p.jt);
sp2.add(c.jt);
sp3.add(s.jt);
p.start();
c.start();
}
public void winadd(JFrame jff,ScrollPane s1,ScrollPane s2,ScrollPane s3,JPanel j1,JPanel j2){
jf=jff;
sp1=s1;
sp2=s2;
sp3=s3;
jp1=j1;
jp2=j2;
}
}



//互斥按钮事件
class BHandler1 implements ActionListener{
private ScrollPane sp1,sp2,sp3;
private JPanel jp1,jp2;
private JFrame jf;
JTextArea jt1=new JTextArea();
JTextArea jt2=new JTextArea();
public void actionPerformed(ActionEvent e){
Share s=new Share();
hProducer p=new hProducer(s);
hConsumer c=new hConsumer(s);
sp1.add(p.jt);
sp2.add(c.jt);
sp3.add(s.jt);
jf.setTitle("生产者与消费者的同步与互斥- 互斥演示");
p.start();
c.start();
}
public void winadd(JFrame jff,ScrollPane s1,ScrollPane s2,ScrollPane s3,JPanel j1,JPanel j2){
jf=jff;
sp1=s1;
sp2=s2;
sp3=s3;
jp1=j1;
jp2=j2;
}
}

public class P extends Thread {
private JFrame frm = new JFrame("Demo");
private JButton btnPause = new JButton("Pause");
private JLabel lblTest = new JLabel("0");
private boolean isPaused = false;
private int seconds = 0;

public void run () {
while (true) {
while (isPaused);
seconds ++;
lblTest.setText(seconds + "");
try {
sleep(200);
} catch (InterruptedException ie) {}
}
}

public P () {
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frm.setResizable(false);
frm.setLayout(null);
frm.setSize(150, 200);
frm.setLocationRelativeTo(null);

lblTest.setBounds(20, 50, 100, 25);

btnPause.setBounds(20, 120, 100, 30);
btnPause.addActionListener(new ActionListener() {
public void actionPerformed (ActionEvent ae) {
if (isPaused) {
btnPause.setText("Pause");
isPaused = false;
} else {
btnPause.setText("Resume");
isPaused = true;
}
}
});

frm.add(lblTest);
frm.add(btnPause);
frm.setVisible(true);
start();
}

public static void main (String args[]) {
new P();
}
}

...全文
274 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
szz958589978 2016-12-22
  • 打赏
  • 举报
回复
以上代码可以实现,只需大神稍加修改,加入暂停按钮,代码直接放到帖子里,可以的话帮我下面的生产者和消费者分成两种结果。做的好的话可以给予一定的物质支持。

58,453

社区成员

发帖
与我相关
我的任务
社区描述
Java Eclipse
社区管理员
  • Eclipse
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧