【问题标题】:Timer Won't Fire Correctly计时器无法正确触发
【发布时间】:2011-11-27 16:37:14
【问题描述】:

我正在使用 Timer 来切换布尔值,但不是像预期的那样每 250 毫秒触发一次,而是尽可能快地触发。这是我的代码:

package com.cgp.tetris;

import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;

import javax.imageio.ImageIO;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.sound.sampled.DataLine.Info;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JPanel;
import javax.swing.KeyStroke;

public class SinglePlayerMenu extends JPanel implements Runnable {
    private static final long serialVersionUID = 1L;
    private Thread thread;
    private AudioInputStream themestream, blipstream;
    private Clip clip, blipclip;
    private BufferedImage base, bi1, bi2, bi3, bi4, bi1s, bi2s, bi3s, bi4s;
    private boolean typeselected = false, b1 = true, b2 = false, b3 = true, b4 = false, b5 = false, b6 = false, b1a = true, b2a = false;

    public SinglePlayerMenu() {
        super();
        this.addComponentListener(new ComponentAdapter() {
            public void componentShown(ComponentEvent e) {
                SinglePlayerMenu.this.requestFocusInWindow();
            }
        });
    }

    public void addNotify() {
        super.addNotify();
        thread = new Thread(this);
        thread.start();
    }

    public void paint(Graphics g) {
        super.paint(g);

        g.drawImage(base, 0, 0, 640, 576, null);

        if (b1) {
            g.drawImage(bi1s, 80, 156, 240, 40, null);
        } else if (!b1) {
            g.drawImage(bi1, 80, 156, 240, 40, null);
        }
        if (b2) {
            g.drawImage(bi2s, 324, 156, 236, 40, null);
        } else if (!b2) {
            g.drawImage(bi2, 324, 156, 236, 40, null);
        }
        if (b3) {
            g.drawImage(bi1s, 80, 380, 240, 40, null);
        } else if (!b3) {
            g.drawImage(bi1, 80, 380, 240, 40, null);
        }
        if (b4) {
            g.drawImage(bi2s, 324, 380, 236, 40, null);
        } else if (!b4) {
            g.drawImage(bi2, 324, 380, 236, 40, null);
        }
        if (b5) {
            g.drawImage(bi3s, 80, 444, 240, 40, null);
        } else if (!b5) {
            g.drawImage(bi3, 80, 444, 240, 40, null);
        }
        if (b6) {
            g.drawImage(bi4s, 324, 444, 236, 40, null);
        } else if (!b6) {
            g.drawImage(bi4, 324, 444, 236, 40, null);
        }
    }

    public void run() {
        sound();
        loadImages();
        bind();
        while (true) {
            repaint();
            try {
                Thread.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            checks();
        }
    }

    private void checks() {
        Timer timer = new Timer();
        timer.scheduleAtFixedRate(new TimerTask() {
            public void run() {
                System.out.println("Being run");
                if (b1a && !typeselected) {
                    b1 = !b1;
                }
            }
        }, java.util.Calendar.getInstance().getTime(), 250);
    }

    private void bind() {
        InputMap im = getInputMap();
        ActionMap am = getActionMap();

        im.put(KeyStroke.getKeyStroke("LEFT"), "left");
        am.put("left", new AbstractAction() {
            private static final long serialVersionUID = 1L;

            public void actionPerformed(ActionEvent e) {
                if (!typeselected) {
                    if (!b1a) {
                        blipclip.start();
                        blipclip.setFramePosition(0);
                    }
                    b1 = true;
                    b2 = false;
                    b1a = true;
                    b2a = false;
                } else if (typeselected) {

                }
            }
        });

        im.put(KeyStroke.getKeyStroke("RIGHT"), "right");
        am.put("right", new AbstractAction() {
            private static final long serialVersionUID = 1L;

            public void actionPerformed(ActionEvent e) {
                if (!typeselected) {
                    if (!b2a) {
                        blipclip.start();
                        blipclip.setFramePosition(0);
                    }
                    b2 = true;
                    b1 = false;
                    b1a = false;
                    b2a = true;
                } else if (typeselected) {
                }
            }
        });

        im.put(KeyStroke.getKeyStroke("SPACE"), "space");
        am.put("space", new AbstractAction() {
            private static final long serialVersionUID = 1L;

            public void actionPerformed(ActionEvent e) {
                if (!typeselected) {
                    typeselected = true;
                    b1=b1a;
                    b2=b2a;
                } else if (typeselected) {

                }
            }
        });
    }

    private void loadImages() {
        try {
            base = ImageIO.read(new File("res/base.png"));
            bi1 = ImageIO.read(new File("res/1.png"));
            bi2 = ImageIO.read(new File("res/2.png"));
            bi3 = ImageIO.read(new File("res/3.png"));
            bi4 = ImageIO.read(new File("res/4.png"));
            bi1s = ImageIO.read(new File("res/1s.png"));
            bi2s = ImageIO.read(new File("res/2s.png"));
            bi3s = ImageIO.read(new File("res/3s.png"));
            bi4s = ImageIO.read(new File("res/4s.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void sound() {
        File theme = new File("res/playtheme.wav");
        try {
            themestream = AudioSystem.getAudioInputStream(theme);
        } catch (UnsupportedAudioFileException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        DataLine.Info info = new DataLine.Info(Clip.class, themestream.getFormat());
        clip = null;
        try {
            clip = (Clip) AudioSystem.getLine(info);
        } catch (LineUnavailableException e) {
            e.printStackTrace();
        }
        try {
            clip.open(themestream);
        } catch (LineUnavailableException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        File blip = new File("res/blip.wav");
        try {
            blipstream = AudioSystem.getAudioInputStream(blip);
        } catch (UnsupportedAudioFileException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        DataLine.Info blipinfo = new DataLine.Info(Clip.class, blipstream.getFormat());
        blipclip = null;
        try {
            blipclip = (Clip) AudioSystem.getLine(blipinfo);
        } catch (LineUnavailableException e) {
            e.printStackTrace();
        }
        try {
            blipclip.open(blipstream);
        } catch (LineUnavailableException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void startClip() {
        clip.loop(Clip.LOOP_CONTINUOUSLY);
        blipclip.start();
        blipclip.setFramePosition(0);
    }
}

遗憾的是,Eclipse 的行为很奇怪,所以 SSCCE 不可能是 ATM。

【问题讨论】:

  • 您如何确定它确实没有等待 250 毫秒?您可以通过打印出 System.currentTimeMillis() 来检查吗?
  • 250 毫秒是 1/4 秒。它每秒发射大约 50 次。

标签: java swing timer concurrency


【解决方案1】:

你的基本代码在我看来是错误的:

  1. 自定义绘画应该在paintComponent()方法中完成。

  2. 使用 Swing 时,您应该使用 Swing 计时器。

  3. 您有一个每 2 毫秒重绘一次的线程。是你的问题吗?

  4. 您不需要带有 while () 循环的线程。这就是您使用 Timer 的目的。所以我猜想线程和定时器应该组合成一个摆动定时器。当时间触发时,您更新变量,然后调用 repaint()。

【讨论】:

  • 关于经期和绘画的出色表现+1
  • 现在它可以工作了,但是我的声音现在有问题。很奇怪。
  • 这里有一个空指针异常:clip.loop(Clip.LOOP_CONTINUOUSLY);
  • 现在知道了,必须添加回addNotify()
【解决方案2】:

来自Timer.scheduleAtFixedRate-documentation:

在固定速率执行中,每次执行都是相对于 初始执​​行的预定执行时间。如果执行是 因任何原因延迟(例如垃圾收集或其他背景 活动),两个或多个处决将快速连续发生 “跟上来。”从长远来看,执行频率将是 正好是指定周期的倒数(假设系统 底层 Object.wait(long) 的时钟是准确的)。

如果您的任务执行时间超过 250 毫秒,则将在第一个任务之后立即执行以下任务,然后执行该任务,依此类推。也许您打算改用 Timer.schedule

安排指定任务重复固定延迟执行, 从指定时间开始。随后的处决发生在 大约是固定的时间间隔,由指定的时间段分隔。

在固定延迟执行中,每次执行都是相对于 上一次执行的实际执行时间。如果执行是 因任何原因延迟(例如垃圾收集或其他背景 活动),后续的执行也会延迟。

【讨论】:

  • 两种方法都会发生完全相同的事情。
  • 再次查看您的代码,我会说您的线程的 run() 方法在每个循环上调用 checks() 方法并启动一个新的计时器,该计时器立即触发。
猜你喜欢
  • 1970-01-01
  • 2019-04-14
  • 2011-10-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-14
  • 1970-01-01
相关资源
最近更新 更多