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条)

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图