Java游戏开发中如何暂停和恢复线程

Java游戏开发中如何暂停和恢复线程,java,multithreading,wait,notify,Java,Multithreading,Wait,Notify,我需要知道如何暂停线程并在检查条件后恢复它 Thread t = new Thread () { public void run() { while(true) { update(); repaint(); try { Thread.sleep(50); } catch(InterruptedException e){} } } }; 上面的代码是我的游戏循环,我需要这是暂停和恢

我需要知道如何暂停线程并在检查条件后恢复它

  Thread t = new Thread () {
  public void run() { 
    while(true) {
      update();
      repaint();
      try {  
        Thread.sleep(50);
      } catch(InterruptedException e){}
    }
  }


       };
上面的代码是我的游戏循环,我需要这是暂停和恢复,如果任何条件被检查

我认为线程只能启动一次,因此我们不能使用
suspend()
resume()
,因为它已被弃用

// all imports goes here /** Bouncing Ball (Animation) via custom thread */

public class JavaApplication12 extends JFrame implements KeyListener{ 
    // Define named-constants
    private static final int CANVAS_WIDTH = 640;
    private static final int CANVAS_HEIGHT = 600;
    private static final int UPDATE_INTERVAL = 10;
    milliseconds int xx = 200; int yy = 500;
    Shape a1; Shape a2; float das[] = {10.0f}; int f = 20;

    boolean r = true;

    Font font = new Font("Arial",Font.ROMAN_BASELINE,f);

    BasicStroke color = new BasicStroke (10,BasicStroke.CAP_ROUND,BasicStroke.JOIN_BEVEL,10.0f,das,0.0f);
    GradientPaint dash = new GradientPaint(0,0,Color.BLUE,100,0,Col or.CYAN);

    Rectangle2D a3;

    Rectangle2D a12;
    Rectangle2D a4;
    Rectangle2D a5;
    Rectangle2D a6;
    Rectangle2D a7;
    Rectangle2D a8;
    Rectangle2D a9;
    Rectangle2D a10;
    Rectangle2D a11; Rectangle2D a13;
    Rectangle2D a14;
    Rectangle2D a15; Rectangle2D a16;
    Rectangle2D a17; Rectangle2D a18;
    Rectangle2D a19; Rectangle2D a20;
    Rectangle2D a21; Rectangle2D a22;
    Rectangle2D a23;
    Rectangle2D a25;

    ArrayList s = new ArrayList();

    Color c2 = Color.RED;

    private DrawCanvas canvas; //the drawing canvas (extends JPanel)

    // Attributes of moving object
    // top-left (x, y)
    private int x = 200;
    private int y = 480;
    //width and height
    private int size = 20;
    //moving speed in x and y directions
    private int xSpeed = 3;
    //displacement per step in x and y
    private int ySpeed = 5; 

    /** Constructor to setup the GUI components */
    public JavaApplication12() {
        canvas = new DrawCanvas();

        canvas.setPreferredSize(new Dimension(CANVAS_WIDTH, CANVAS_HEIGHT));

        this.setContentPane(canvas);

        this.setDefaultCloseOperation(EXIT_ON_ CLOSE);
        this.pack();
        this.setTitle("Bouncing Ball");
        this.setVisible(true); addKeyListener(this);

        fun();

    }

    Thread updateThread = new Thread() { @Override public void run() {

        while (r) { update(); // update the (x, y) position

            repaint(); // Refresh the JFrame. Called back paintComponent()

            try { // Delay and give other thread a chance to run

                Thread.sleep(UPDATE_INTERVAL); //milliseconds

            } catch (InterruptedException ignore) {} } }

    };

    /** Update the (x, y) position of the moving object */
    public void update() { 
        x += xSpeed; y += ySpeed; 
        if (x > CANVAS_WIDTH - size || x < 0) { xSpeed = -xSpeed; } 
        if (y > CANVAS_HEIGHT - 80 || y < 0) { ySpeed = -ySpeed;  }

    }

@Override public void keyTyped(KeyEvent ke) { }

@Override

@SuppressWarnings({"deprecation", "deprecation"}) 
public void keyPressed(KeyEvent ke) { 
    int keycode = ke.getKeyCode(); 
    if(keycode == KeyEvent.VK_LEFT) { xx = xx-15; } 
    if(keycode == KeyEvent.VK_SPACE) { 
        if(r == true) { 
            //r = false;
            updateThread.start();
        }

    } 
    if(keycode == KeyEvent.VK_ESCAPE) {    }

    if(keycode == KeyEvent.VK_RIGHT) { xx = xx+15; }

}

@Override public void keyReleased(KeyEvent ke) { 
    // throw new UnsupportedOperationException("Not supported yet.");
}

/** DrawCanvas (inner class) is a JPanel used for custom drawing */
    class DrawCanvas extends JPanel { 
        private static final long serialVersionUID = 1L; 
        int i=0; 
        int liv = 3;

        @Override

        public void paintComponent(Graphics g) { super.paintComponent(g); //paint parent's background Graphics2D d = (Graphics2D)g;

            a1 = new Ellipse2D.Double(x,y,size,size);
            d.drawLine(0,540,640,540);
            for(Rectangle2D r : s){ 
                    d.setStroke(color);
                    //d.setPaint(dash);
                    d.draw(r); 
            }
            a25 = new Rectangle2D.Double(0,540,1000,10);
            d.draw(a25);
            setBackground(Color.WHITE);

                d.setColor(Color.BLUE);

                a2 = new RoundRectangle2D.Double(xx,yy,120,10,2 0,20);

                d.fill(a1); d.setPaint(c2);
                d.fill(a2); d.setFont(font);
                d.drawString("Score : "+i,5,575);
                d.drawString("Life : "+liv,550,575);

                if(a1.intersects(a25.getBounds())) {
                    if(liv>0) { liv = liv-1;}

                    if(liv == 0) { //JOptionPane.showMessageDialog(null, "","",0);

                    } }

                if(s.contains(a3) && a1.intersects(a3.getBounds())) { 
                    i = i+10; s.remove(a3);
                    ySpeed = -ySpeed;
                    try { m(); } 
                    catch (UnsupportedAudioFileException ex) {

                    Logger.getLogger(JavaApplication12.cla ss.getName()).log (Level.SEVERE, null, ex); } catch (IOException ex) {

                    Logger.getLogger(JavaApplication12.cla ss.getName()).log (Level.SEVERE, null, ex); } catch (LineUnavailableException ex) {

                    Logger.getLogger(JavaApplication12.cla ss.getName()).log (Level.SEVERE, null, ex); } } if(s.contains(a4) && a1.intersects(a4.getBounds())) { i = i+10; s.remove(a4);ySpeed = -ySpeed;

                    try { m();

                    } catch (UnsupportedAudioFileException ex) {

                        Logger.getLogger(JavaApplication12.cla ss.getName()).log (Level.SEVERE, null, ex); } catch (IOException ex) {

                        Logger.getLogger(JavaApplication12.cla ss.getName()).log (Level.SEVERE, null, ex); } catch (LineUnavailableException ex) {

                        Logger.getLogger(JavaApplication12.cla ss.getName()).log (Level.SEVERE, null, ex); } } if(s.contains(a5) && a1.intersects(a5.getBounds())) { i = i+10; s.remove(a5);ySpeed = -ySpeed; try { m(); } catch (UnsupportedAudioFileException ex) {

                    Logger.getLogger(JavaApplication12.cla ss.getName()).log (Level.SEVERE, null, ex); } catch (IOException ex) {

                    Logger.getLogger(JavaApplication12.cla ss.getName()).log (Level.SEVERE, null, ex); } catch (LineUnavailableException ex) {

                    Logger.getLogger(JavaApplication12.cla ss.getName()).log (Level.SEVERE, null, ex); } } if(s.contains(a6) && a1.intersects(a6.getBounds())) { i = i+10; s.remove(a6);ySpeed = -ySpeed; try { m(); } catch (UnsupportedAudioFileException ex) {

                    Logger.getLogger(JavaApplication12.cla ss.getName()).log (Level.SEVERE, null, ex); } catch (IOException ex) {

                    Logger.getLogger(JavaApplication12.cla ss.getName()).log (Level.SEVERE, null, ex); } catch (LineUnavailableException ex) {

                    Logger.getLogger(JavaApplication12.cla ss.getName()).log (Level.SEVERE, null, ex); } } if(s.contains(a7) && a1.intersects(a7.getBounds())) { i = i+10; s.remove(a7);ySpeed = -ySpeed; try { m(); } catch (UnsupportedAudioFileException ex) {

                    Logger.getLogger(JavaApplication12.cla ss.getName()).log (Level.SEVERE, null, ex); } catch (IOException ex) {

                    Logger.getLogger(JavaApplication12.cla ss.getName()).log (Level.SEVERE, null, ex); } catch (LineUnavailableException ex) {

                    Logger.getLogger(JavaApplication12.cla ss.getName()).log (Level.SEVERE, null, ex); } } if(s.contains(a8) && a1.intersects(a8.getBounds())) { i = i+10; s.remove(a8);ySpeed = -ySpeed; try { m(); } catch (UnsupportedAudioFileException ex) {

                    Logger.getLogger(JavaApplication12.cla ss.getName()).log (Level.SEVERE, null, ex); } catch (IOException ex) {

                    Logger.getLogger(JavaApplication12.cla ss.getName()).log (Level.SEVERE, null, ex); } catch (LineUnavailableException ex) {

                    Logger.getLogger(JavaApplication12.cla ss.getName()).log (Level.SEVERE, null, ex); }

                } if(s.contains(a9) && a1.intersects(a9.getBounds())) { i = i+10; s.remove(a9);ySpeed = -ySpeed; try { m(); } catch (UnsupportedAudioFileException ex) {

                    Logger.getLogger(JavaApplication12.cla ss.getName()).log (Level.SEVERE, null, ex); } catch (IOException ex) {

                    Logger.getLogger(JavaApplication12.cla ss.getName()).log (Level.SEVERE, null, ex); } catch (LineUnavailableException ex) {

                    Logger.getLogger(JavaApplication12.cla ss.getName()).log (Level.SEVERE, null, ex); } } if(s.contains(a10) && a1.intersects(a10.getBounds())) { i = i+10; s.remove(a10);ySpeed = -ySpeed; try { m(); } catch (UnsupportedAudioFileException ex) {

                    Logger.getLogger(JavaApplication12.cla ss.getName()).log (Level.SEVERE, null, ex); } catch (IOException ex) {

                    Logger.getLogger(JavaApplication12.cla ss.getName()).log (Level.SEVERE, null, ex); } catch (LineUnavailableException ex) {

                    Logger.getLogger(JavaApplication12.cla ss.getName()).log (Level.SEVERE, null, ex); } } if(s.contains(a11) && a1.intersects(a11.getBounds())) { i = i+10; s.remove(a11);ySpeed = -ySpeed; try { m(); } catch (UnsupportedAudioFileException ex) {

                    Logger.getLogger(JavaApplication12.cla ss.getName()).log (Level.SEVERE, null, ex); } catch (IOException ex) {

                    Logger.getLogger(JavaApplication12.cla ss.getName()).log (Level.SEVERE, null, ex); } catch (LineUnavailableException ex) {

                    Logger.getLogger(JavaApplication12.cla ss.getName()).log (Level.SEVERE, null, ex); } } if(s.contains(a12) && a1.intersects(a12.getBounds())) { i = i+10; s.remove(a12);ySpeed = -ySpeed; try { m(); } catch (UnsupportedAudioFileException ex) {

                    Logger.getLogger(JavaApplication12.cla ss.getName()).log (Level.SEVERE, null, ex); } catch (IOException ex) {

                    Logger.getLogger(JavaApplication12.cla ss.getName()).log (Level.SEVERE, null, ex); } catch (LineUnavailableException ex) {

                    Logger.getLogger(JavaApplication12.cla ss.getName()).log (Level.SEVERE, null, ex); } } if(s.contains(a13) && a1.intersects(a13.getBounds())) { i = i+10; s.remove(a13);ySpeed = -ySpeed; try { m(); } catch (UnsupportedAudioFileException ex) {

                    Logger.getLogger(JavaApplication12.cla ss.getName()).log (Level.SEVERE, null, ex); } catch (IOException ex) {

                    Logger.getLogger(JavaApplication12.cla ss.getName()).log (Level.SEVERE, null, ex); } catch (LineUnavailableException ex) {

                    Logger.getLogger(JavaApplication12.cla ss.getName()).log (Level.SEVERE, null, ex); } } if(s.contains(a14) && a1.intersects(a14.getBounds())) { i = i+10; s.remove(a14);ySpeed = -ySpeed; try { m(); } catch (UnsupportedAudioFileException ex) {

                    Logger.getLogger(JavaApplication12.cla ss.getName()).log (Level.SEVERE, null, ex); } catch (IOException ex) {

                    Logger.getLogger(JavaApplication12.cla ss.getName()).log (Level.SEVERE, null, ex); } catch (LineUnavailableException ex) {

                    Logger.getLogger(JavaApplication12.cla ss.getName()).log (Level.SEVERE, null, ex); } } if(s.contains(a15) && a1.intersects(a15.getBounds())) { i = i+10; s.remove(a15);ySpeed = -ySpeed; try { m(); } catch (UnsupportedAudioFileException ex) {

                    Logger.getLogger(JavaApplication12.cla ss.getName()).log (Level.SEVERE, null, ex); } catch (IOException ex) {

                    Logger.getLogger(JavaApplication12.cla ss.getName()).log (Level.SEVERE, null, ex); } catch (LineUnavailableException ex) {

                    Logger.getLogger(JavaApplication12.cla ss.getName()).log (Level.SEVERE, null, ex); } }

                if(s.contains(a16) && a1.intersects(a16.getBounds())) { i = i+10; s.remove(a16);ySpeed = -ySpeed; try { m(); } catch (UnsupportedAudioFileException ex) {

                    Logger.getLogger(JavaApplication12.cla ss.getName()).log (Level.SEVERE, null, ex); } catch (IOException ex) {

                    Logger.getLogger(JavaApplication12.cla ss.getName()).log (Level.SEVERE, null, ex); } catch (LineUnavailableException ex) {

                    Logger.getLogger(JavaApplication12.cla ss.getName()).log (Level.SEVERE, null, ex); } }

                if(s.contains(a17) && a1.intersects(a17.getBounds())) { i = i+10; s.remove(a17);ySpeed = -ySpeed; try { m(); } catch (UnsupportedAudioFileException ex) {

                    Logger.getLogger(JavaApplication12.cla ss.getName()).log (Level.SEVERE, null, ex); } catch (IOException ex) {

                    Logger.getLogger(JavaApplication12.cla ss.getName()).log (Level.SEVERE, null, ex); } catch (LineUnavailableException ex) {

                    Logger.getLogger(JavaApplication12.cla ss.getName()).log (Level.SEVERE, null, ex); } }

                if(s.contains(a18) && a1.intersects(a18.getBounds())) { i = i+10; s.remove(a18);ySpeed = -ySpeed; try { m(); } catch (UnsupportedAudioFileException ex) {

                    Logger.getLogger(JavaApplication12.cla ss.getName()).log (Level.SEVERE, null, ex); } catch (IOException ex) {

                    Logger.getLogger(JavaApplication12.cla ss.getName()).log (Level.SEVERE, null, ex); } catch (LineUnavailableException ex) {

                    Logger.getLogger(JavaApplication12.cla ss.getName()).log (Level.SEVERE, null, ex); } }

                if(s.contains(a19) && a1.intersects(a19.getBounds())) { i = i+10; s.remove(a19);ySpeed = -ySpeed; try { m(); } catch (UnsupportedAudioFileException ex) {

                    Logger.getLogger(JavaApplication12.cla ss.getName()).log (Level.SEVERE, null, ex); } catch (IOException ex) {

                    Logger.getLogger(JavaApplication12.cla ss.getName()).log (Level.SEVERE, null, ex); } catch (LineUnavailableException ex) {

                    Logger.getLogger(JavaApplication12.cla ss.getName()).log (Level.SEVERE, null, ex); } }

                if(s.contains(a20) && a1.intersects(a20.getBounds())) { i = i+10; s.remove(a20);ySpeed = -ySpeed; try { m(); } catch (UnsupportedAudioFileException ex) {

                    Logger.getLogger(JavaApplication12.cla ss.getName()).log (Level.SEVERE, null, ex); } catch (IOException ex) {

                    Logger.getLogger(JavaApplication12.cla ss.getName()).log (Level.SEVERE, null, ex); } catch (LineUnavailableException ex) {

                    Logger.getLogger(JavaApplication12.cla ss.getName()).log (Level.SEVERE, null, ex); } }

                if(s.contains(a21) &&a1.intersects(a21)) { i = i+10; s.remove(a21);ySpeed = -ySpeed; try { m(); } catch (UnsupportedAudioFileException ex) {

                    Logger.getLogger(JavaApplication12.cla ss.getName()).log (Level.SEVERE, null, ex); } catch (IOException ex) {

                    Logger.getLogger(JavaApplication12.cla ss.getName()).log (Level.SEVERE, null, ex); } catch (LineUnavailableException ex) {

                    Logger.getLogger(JavaApplication12.cla ss.getName()).log (Level.SEVERE, null, ex); } }

                if(s.contains(a22) && a1.intersects(a22)) { i = i+10; s.remove(a22); ySpeed = -ySpeed; try { m(); } catch (UnsupportedAudioFileException ex) {

                    Logger.getLogger(JavaApplication12.cla ss.getName()).log (Level.SEVERE, null, ex); } catch (IOException ex) {

                    Logger.getLogger(JavaApplication12.cla ss.getName()).log (Level.SEVERE, null, ex); } catch (LineUnavailableException ex) {

                    Logger.getLogger(JavaApplication12.cla ss.getName()).log (Level.SEVERE, null, ex); } }

                collide(); } public void m() throws UnsupportedAudioFileException, IOException, LineUnavailableException { String a = "chimes.wav"; AudioInputStream aa = AudioSystem.getAudioInputStream(new File (a).getAbsoluteFile()); Clip c = AudioSystem.getClip(); c.open(aa); c.start(); }

    }

    public final void fun() { 
        a3 = new Rectangle2D.Double(20,15,100,20);
        a4 = new Rectangle2D.Double(150,15,100,20);
        a5 = new Rectangle2D.Double(280,15,100,20);

        a6 = new Rectangle2D.Double(400,15,100,20);
        a7 = new Rectangle2D.Double(530,15,100,20);

        a8 = new Rectangle2D.Double(20,50,100,20);
        a9 = new Rectangle2D.Double(150,50,100,20);
        a10 = new Rectangle2D.Double(280,50,100,20);
        a12 = new Rectangle2D.Double(400,50,100,20);
        a11 = new Rectangle2D.Double(530,50,100,20);

        a13 = new Rectangle2D.Double(20,85,100,20);
        a14 = new Rectangle2D.Double(150,85,100,20);
        a15 = new Rectangle2D.Double(280,85,100,20);
        a16 = new Rectangle2D.Double(400,85,100,20);
        a17 = new Rectangle2D.Double(530,85,100,20);

        a18 = new Rectangle2D.Double(20,120,100,20);
        a19 = new Rectangle2D.Double(150,120,100,20);
        a20 = new Rectangle2D.Double(280,120,100,20);
        a21 = new Rectangle2D.Double(400,120,100,20);
        a22 = new Rectangle2D.Double(530,120,100,20);

        s.add(a3); s.add(a4); s.add(a5); s.add(a6);
        s.add(a7); s.add(a8); s.add(a9); s.add(a10);
        s.add(a11); s.add(a12);
        s.add(a13); s.add(a14); s.add(a15); s.add(a16);
        s.add(a17); s.add(a18);
        s.add(a19); s.add(a20); s.add(a21); s.add(a22); }

    public void collide() {

        if(a2.intersects(a1.getBounds())) {

            xSpeed = xSpeed; ySpeed = -ySpeed;

        } }

    public static void main(String[] args) { 
        // Run GUI codes in Event-Dispatching thread for thread safety 
        SwingUtilities.invokeLater(new Runnable() { 
            @Override public void run() { 
                new JavaApplication12(); // Let the constructor do the job
            }
        });
    }
}
//所有导入都在此处进行/**通过自定义线程反弹球(动画)*/
公共类JavaApplication12扩展JFrame实现KeyListener{
//定义命名常量
专用静态最终整型画布宽度=640;
专用静态最终整型画布高度=600;
私有静态最终整数更新间隔=10;
毫秒整数xx=200;整数yy=500;
形状a1;形状a2;浮点das[]={10.0f};整数f=20;
布尔r=真;
Font Font=新字体(“Arial”,Font.ROMAN_基线,f);
基本行程颜色=新的基本行程(10,基本行程圆盖,基本行程连接斜面,10.0f,das,0.0f);
GradientPaint破折号=新的GradientPaint(0,0,颜色。蓝色,100,0,颜色或青色);
矩形2D a3;
矩形2D a12;
矩形;
矩形;
矩形2D a6;
矩形2D a7;
矩形2D a8;
矩形2D a9;
矩形2D a10;
矩形2D a11;矩形2D a13;
矩形2D a14;
矩形2D a15;矩形2D a16;
矩形2D a17;矩形2D a18;
矩形2D a19;矩形2D a20;
矩形2D a21;矩形2D a22;
矩形2D a23;
矩形2D a25;
ArrayList s=新的ArrayList();
颜色c2=颜色。红色;
私有DrawCanvas;//绘图画布(扩展JPanel)
//运动对象的属性
//左上角(x,y)
私有整数x=200;
私人int y=480;
//宽度和高度
私有整数大小=20;
//x和y方向上的移动速度
私有int xSpeed=3;
//x和y中每一步的位移
私人互联网速度=5;
/**构造函数来设置GUI组件*/
公共JavaApplication12(){
画布=新的DrawCanvas();
setPreferredSize(新维度(画布宽度、画布高度));
这个.setContentPane(画布);
此.setDefaultCloseOperation(关闭时退出);
这个包();
这是setTitle(“弹跳球”);
this.setVisible(true);addKeyListener(this);
乐趣();
}
Thread updateThread=new Thread(){@Override public void run(){
while(r){update();//更新(x,y)位置
repaint();//刷新JFrame。回调paintComponent()
尝试{//延迟并给其他线程一个运行的机会
Thread.sleep(更新间隔);//毫秒
}catch(InterruptedException忽略){}
};
/**更新移动对象的(x,y)位置*/
public void update(){
x+=x速度;y+=y速度;
如果(x>CANVAS|u WIDTH-size | x<0){xSpeed=-xSpeed;}
如果(y>CANVAS_HEIGHT-80 | | y<0){ySpeed=-ySpeed;}
}
@重写公共void keyTyped(KeyEvent ke){}
@凌驾
@SuppressWarnings({“弃用”、“弃用”})
按下公共无效键(KeyEvent ke){
int keycode=ke.getKeyCode();
如果(keycode==KeyEvent.VK_LEFT){xx=xx-15;}
如果(keycode==KeyEvent.VK_SPACE){
如果(r==true){
//r=假;
updateThread.start();
}
} 
如果(keycode==KeyEvent.VK_ESCAPE){}
如果(keycode==KeyEvent.VK_RIGHT){xx=xx+15;}
}
@覆盖公共无效密钥释放(KeyEvent ke){
//抛出新的UnsupportedOperationException(“尚未支持”);
}
/**DrawCanvas(内部类)是用于自定义绘图的JPanel*/
类DrawCanvas扩展了JPanel{
私有静态最终长serialVersionUID=1L;
int i=0;
int-liv=3;
@凌驾
public void paintComponent(Graphics g){super.paintComponent(g);//绘制父对象的背景Graphics2D=(Graphics2D)g;
a1=新的椭圆E2D双精度(x,y,尺寸,尺寸);
d、 抽绳(0540640540);
对于(矩形:s){
d、 设定行程(颜色);
//d、 设置油漆(破折号);
d、 抽签(r);
}
a25=新矩形2D.双(05401000,10);
d、 抽签(a25);
挫折地面(颜色:白色);
d、 setColor(Color.BLUE);
a2=新的圆形矩形2D.双(xx,yy,120,10,20,20);
d、 填充(a1);d.设置油漆(c2);
d、 填写(a2);d.设置字体(font);
d、 抽绳(分数:+i,5575);
d、 抽绳(寿命:+liv,550575);
if(a1.相交(a25.getBounds())){
如果(liv>0){liv=liv-1;}
如果(liv==0){//JOptionPane.showMessageDialog(null,“,”,0);
} }
如果(s.contains(a3)&&a1.intersects(a3.getBounds()){
i=i+10;s.移除(a3);
ySpeed=-ySpeed;
试试{m();}
捕获(不支持DaudioFileException ex){
Logger.getLogger(JavaApplication12.cla ss.getName()).log(Level.SEVERE,null,ex);}catch(IOException ex){
Logger.getLogger(JavaApplication12.cla ss.getName()).log(Level.SEVERE,null,ex);}catch(LineUnavailableException ex){
Logger.getLogger(JavaApplication12.cla ss.getName()).log(Level.SEVERE,null,ex);}}if(s.contains(a4)和&a1.intersects(a4.getBounds()){i=i+10;s.remove(a4);ySpeed=-ySpeed;
试试{m();
}捕获(不支持DaudioFileException ex){
Logger.getLogger(JavaApplication12.cla ss.getName()).log(Level.SEVERE,null,ex);}catch(IOException ex){
Logger.getLogger(JavaApplication12.cla ss.getName()).log(Level.SEVERE,null,ex);}catch(LineUnavailableException ex){
//in Foo class
synchronized void doStuffAndWait() throws InterruptedException {
    //do stuff
    while(!someCondition) { 
           wait();
           System.out.println("I woke up!");
    }

//in Bar class; 
synchronized void wakeFooUp(Foo foo){
    foo.someCondition = true; 
    foo.notify();
}
JavaApplication12 extends JFrame implements KeyListener {
...
AtomicBoolean is_running(true);
...
Thread updateThread = new Thread() {
  @Override public void run() {
    while (r) {
      synchronized(is_running) {
        if(!is_running.get()) {
          is_running.wait();
          continue;
        }
      }
      update(); // update the (x, y) position
      repaint(); // Refresh the JFrame. Called back paintComponent()
      try { // Delay and give other thread a chance to run
        Thread.sleep(UPDATE_INTERVAL); //milliseconds
      } catch (InterruptedException ignore) {}
    }
  }
};
...
@SuppressWarnings({"deprecation", "deprecation"}) public void keyPressed(KeyEvent ke) {
  ...
  } if(keycode == KeyEvent.VK_ESCAPE) {
    synchronized(is_running)
      is_running.set(!is_running.get());
      is_running.notify_all();
    }
  }
  ...
}