Java6学习笔记58——多线程编程——线程的中断响应

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Color;
import java.awt.Canvas;
import java.awt.Container;
import java.awt.Rectangle;
import java.awt.Component;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.BoxLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class ColorBounce extends JFrame implements ActionListener {
ColorBounceThread thread;
public static void main(String arg[]) {
new ColorBounce();
}
public ColorBounce() {
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e)
{ System.exit(0); } } );

Container container = getContentPane();
BoxLayout boxlayout = new BoxLayout(container,BoxLayout.Y_AXIS);
container.setLayout(boxlayout);

JButton button = new JButton("Color");
button.addActionListener(this);
button.setAlignmentX(Component.CENTER_ALIGNMENT);
container.add(button);
ColorBounceCanvas canvas = new ColorBounceCanvas();
container.add(canvas);
pack();
thread = new ColorBounceThread(canvas);
thread.start();
setLocation(250,100);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
thread.interrupt(); //产生中断
}
}

class ColorBounceThread extends Thread {
ColorBounceCanvas canvas;

ColorBounceThread(ColorBounceCanvas bbc) {
canvas = bbc;
}
public void run() {
while(true) {
try {
sleep(33);
canvas.repaint();
} catch(InterruptedException e) {
canvas.changeColor();
//捕获中断
}
}
}
}

class ColorBounceCanvas extends Canvas {
private double y;
private double x;
private final static int diameter = 10;
private final static double gravity = 0.3;
private double velocity;
private Rectangle rect;
private int width;
private int height;
private Image image = null;
private boolean running;
private Thread looper;
private int bounceCount;
private Color color[] = { Color.black, Color.red, Color.green };
private int colorIndex;
ColorBounceCanvas() {
rect = null;
setSize(100,300);
running = false;
height = 0;
width = 0;
colorIndex = 0;
}
public void update(Graphics g) {
rect = getBounds(rect);
if((height != rect.height) || (rect.width != width)) {
height = rect.height;
width = rect.width;
velocity = 0.0;
bounceCount = 0;
x = width / 2;
y = 0;
image = createImage(rect.width,rect.height);
}
if(velocity > 0.0) {
if(((int)(y) + diameter) > rect.height) {
velocity = -velocity;
velocity *= 0.95;
y = (double)(rect.height - diameter);
if(++bounceCount > 20) {
y = 0;
velocity = 0.0;
bounceCount = 0;
}
}
}
velocity += gravity;
y += velocity;
paint(image.getGraphics());
g.drawImage(image,0,0,this);
}
public void changeColor() {
if(++colorIndex >= color.length)
colorIndex = 0;
}
public void paint(Graphics g) {
if(rect == null)
return;
g.setColor(Color.white);
g.fillRect(0,0,rect.width,rect.height);
g.setColor(color[colorIndex]);
g.fillOval((int)x,(int)y,diameter,diameter);
}
}

而第二个方法是用Thread.interrupted()检查是否发生中断.Thread.interrupted()能告诉你线程是否发生中断,并将清除中断状态标记,所以程序不会两次通知你线程发生了中断.

  1. class ATask implements Runnable{
  2. private double d= 0.0 ;
  3. public void run(){
  4. //检查程序是否发生中断
  5. while (!Thread.interrupted()){
  6. System.out.println("Iamrunning!" );
  7. for ( int i= 0 ;i< 900000 ;i++){
  8. d=d+(Math.PI+Math.E)/d;
  9. }
  10. }
  11. System.out.println("ATask.run()interrupted!" );
  12. }
  13. }

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