JAVA中多线程中遇到的问题

有3个类
1:MyFrame:
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();
	}

}




2.MyThread:

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;
	}

}


3.BallThread:

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;
	}
	

}




若将MyFrame中的num设置为3,则重复执行开始、停止、重新开始,可能会出现球擦不干净的情况,若num设置为1,则每次运行球都擦得干净。

你可能感兴趣的:(java,多线程,thread,swing)