lzz405791584 2010-08-06 16:11
浏览 261
已采纳

JAVA中多线程中遇到的问题

有3个类
1:MyFrame:
[code="java"]import java.awt.event.ActionEvent;

public class MyFrame extends javax.swing.JFrame {

private java.awt.Graphics g;
private int x, y;
private java.util.Random rand = new java.util.Random();
public static MyThread mt;
public static MyFrame mf = new MyFrame();
public static int num = 3;

public void init() {

    this.setTitle("线程的暂停与继续");
    this.setSize(700, 700);

    this.setLayout(new java.awt.FlowLayout());
    // 添加开始按钮
    final javax.swing.JButton bu = new javax.swing.JButton("开始");
    this.add(bu);
    final javax.swing.JButton bu2 = new javax.swing.JButton("暂停");
    this.add(bu2);
    // 添加进度条
    final javax.swing.JProgressBar probar = new javax.swing.JProgressBar();
    this.add(probar);

    this.setDefaultCloseOperation(3);
    this.setLocationRelativeTo(null);

    this.setVisible(true);
    g = this.getGraphics();
    // 按钮1
    bu.addActionListener(new java.awt.event.ActionListener() {

        public void actionPerformed(ActionEvent e) {
            mt = new MyThread(probar);
            String str = new String();
            if ("开始".equals(e.getActionCommand())) {

                for (int i = 0; i < num; i++) {
                    x = rand.nextInt(500) + 100;
                    y = rand.nextInt(500) + 100;
                    BallThread bt = new BallThread(mf, g, x, y);
                    bt.start();
                }

                mt.start();
                bu.setText("停止");
            }
            if ("停止".equals(e.getActionCommand())) {
                mt.stopThread();
                bu.setText("重新开始");
                bu2.setText("暂停");
            }
            if ("重新开始".equals(e.getActionCommand())) {
                mt.restart();
                mt.start();
                for (int i = 0; i < num; i++) {
                    x = rand.nextInt(500) + 100;
                    y = rand.nextInt(500) + 100;
                    BallThread bt = new BallThread(mf, g, x, y);
                    bt.start();
                }
                bu.setText("停止");
            }

        }

    });
    // 按钮2
    bu2.addActionListener(new java.awt.event.ActionListener() {

        public void actionPerformed(ActionEvent e) {
            String str = new String();
            if ("暂停".equals(e.getActionCommand())) {
                mt.pauseThread();
                bu2.setText("继续");
            }
            if ("继续".equals(e.getActionCommand())) {
                mt.continueThread();
                bu2.setText("暂停");
            }
        }
    });
}

public static void main(String args[]) {
    MyFrame mf = new MyFrame();
    mf.init();
}

}[/code]

2.MyThread:

[code="java"]public class MyThread extends Thread {

public static boolean isFinish = false;// 是否停止
public static boolean isPause = false;// 是否暂停
private javax.swing.JProgressBar probar;
private int value = 0;

public MyThread(javax.swing.JProgressBar probar) {
    this.probar = probar;
}

public void run() {
    this.changeValue();
}

public void changeValue() {

    while (!isFinish) {
        // System.out.println("暂停中");
        while (!isPause) {
            // System.out.println("运行中");
            probar.setValue(value);
            value++;
            try {
                Thread.sleep(100);
            } catch (Exception ef) {
                ef.printStackTrace();
            }
        }
        try {
            Thread.sleep(100);
        } catch (Exception ef) {
            ef.printStackTrace();
        }

    }
    // System.out.println("结束了");

}

public void stopThread() {
    isPause = true;
    isFinish = true;
}

public void continueThread() {
    isPause = false;
}

public void pauseThread() {
    isPause = true;
}

public void restart() {
    value = 0;
    isPause = false;
    isFinish = false;
}

}[/code]

3.BallThread:

[code="java"]public class BallThread extends Thread {

private java.awt.Graphics g;
private int x = 20, y = 20;
private MyFrame mf = MyFrame.mf;
private java.util.Random rand = new java.util.Random();
public static int xx[] = new int[30];
public static int yy[] = new int[30];

public BallThread(MyFrame mf, java.awt.Graphics g, int x, int y) {
    this.mf = MyFrame.mf;
    this.g = g;
    this.x = x;
    this.y = y;
}

public BallThread(int x,int y) {
    this.x = x;
    this.y = y;
}

public void run() {
    changBall();
}

public synchronized  void changBall() {

    while (!MyThread.isFinish) {
    //  System.out.println("运行了");
        while (!MyThread.isPause) {
    //      System.out.println("运行了");
            g.setColor(mf.getBackground());
            g.fillOval(x, y, 20, 20);

            x += 20;
            y += 20;
            g.setColor(java.awt.Color.BLUE);
            g.fillOval(x, y, 20, 20);


            try {
                Thread.sleep(100);
            } catch (Exception ef) {
                ef.printStackTrace();
            }


        }

    }
    if(MyThread.isFinish) {
        g.setColor(mf.getBackground());
        g.fillOval(x, y, 20, 20);
    }

}

public int getX() {
    return this.x;
}

public int getY() {
    return this.y;
}

}[/code]

若将MyFrame中的num设置为3,则重复执行开始、停止、重新开始,可能会出现球擦不干净的情况,若num设置为1,则每次运行球都擦得干净。
不知道是什么原因,期待高手讲解。

  • 写回答

2条回答 默认 最新

  • cuiran 博客专家认证 2010-08-09 12:50
    关注

    你的程序调用的时候是,每次生成一个
    BallThread bt = new BallThread(mf, g, x, y);

    bt.start(); 然后就开始运行,这样在运行的过程中就容易出错,你可以先产生对象然后在运行.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 vscode的红色箭头爆红和has no default export报错
  • ¥15 关于#sql#的问题:#情况描述 在用vs对项目进行调试时,出现找不到网络路径,然后查看SQL配置工具,发现SQL服务显示远程调用过程失败(相关搜索:防火墙)
  • ¥15 eNSP中基于默认路由及浮动路由的公司与分部互联和校园网综合项目
  • ¥15 主要进行描述泥浆在管路不同区段泥浆的密度不相同,泥浆的密度有高有低,此时管路的摩阻分布需要怎么计算,(标签-matlab)
  • ¥40 通过编制程序计算圆管内层流充分发展对流换热,参数如图5-4,以及公式5-16所示,要求用表5-6对程序计算准确性进行验证
  • ¥20 该学习哪个编程语言? AI会取代程序员吗?
  • ¥15 ensp如何拼通IP地址
  • ¥15 saber软件导入Ibis模型报错
  • ¥15 商城生产日期批次库存问题
  • ¥15 esp8266控制共阳极wrgb灯板无法关闭所有led灯