[英]java wait for swing.timer task to finish before executing the next task
因此標題確實說明了一切。 在執行方法中的下一個命令之前,我需要線程等待計時器完成。
碼:
import javax.swing.Timer;
public class doesntMatter
{
Timer timer;
ActionListener timerTask;
public doesntMatter
{
timerTask = new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
//taskSomethingIdk
}
}
}
private void whatever
{
timer = new Timer(1000, timerTask);
timer.setInitialDelay(0);
timer.start();
// here i need to wait for the timer to finish
if(timerhasfrigginfinished)
continueOrSomethingIdk
}
}
假設您的計時器重復執行,請在Timer的ActionListener中進行檢查,以查看其是否正在運行上一次重復,如果是, 則在其中調用continueOrSomethingIdk()
方法。
否則,您將必須建立自己的通知機制,即回叫,以便計時器可以通知所有偵聽器它已完成運行。
例如:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
@SuppressWarnings("serial")
public class WhenTimerDone extends JPanel {
private static final Color[] COLORS = {
Color.RED, Color.ORANGE,
Color.YELLOW, Color.GREEN,
Color.BLUE, Color.CYAN };
private static final String START = "Start";
private static final String DONE = "Done";
private static final int PREF_W = 400;
private static final int PREF_H = PREF_W;
public static final int TIMER_DELAY = 1000;
private JLabel statusLabel = new JLabel(START);
private StartAction startAction = new StartAction("Start!");
public WhenTimerDone() {
add(statusLabel);
add(new JButton(startAction));
}
@Override
public Dimension getPreferredSize() {
if (isPreferredSizeSet()) {
return super.getPreferredSize();
}
return new Dimension(PREF_W, PREF_H);
}
// this is the method called by the Timer's ActionListener when it is done
public void done() {
// reset all to baseline state
statusLabel.setText(DONE);
startAction.setEnabled(true);
setBackground(null);
}
// ActionListener for the start button
private class StartAction extends AbstractAction {
public StartAction(String name) {
super(name);
int mnemonic = (int) name.charAt(0);
putValue(MNEMONIC_KEY, mnemonic);
}
@Override
public void actionPerformed(ActionEvent e) {
// disables itself
setEnabled(false);
statusLabel.setText(START); // updates the status label
// create and start a timer
Timer timer = new Timer(TIMER_DELAY, new TimerListener());
timer.setInitialDelay(0);
timer.start();
}
}
// action listener for the timer
private class TimerListener implements ActionListener {
private int colorsIndex = 0;
@Override
public void actionPerformed(ActionEvent e) {
// simply loops through a colors array, changing background color
if (colorsIndex < COLORS.length) {
setBackground(COLORS[colorsIndex]);
colorsIndex++;
} else {
// when all colors shown -- stop the timer
((Timer) e.getSource()).stop();
// and call the done method -- ******* here's the key!
done(); // called when Timer is done!
}
}
}
private static void createAndShowGui() {
WhenTimerDone mainPanel = new WhenTimerDone();
JFrame frame = new JFrame("WhenTimerDone");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}
這里的關鍵字是notify / wait您的計時器線程應通知另一個應等待的線程,如下所示:
import javax.swing.Timer;
public class doesntMatter
{
Timer timer;
ActionListener timerTask;
public doesntMatter
{
timerTask = new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
synchronized(timer){
//taskSomethingIdk
//you would have to rig something so we can access a common object as the monitor/lock object
timer.notify()
}
}
}
}
private void whatever
{
timer = new Timer(1000, timerTask);
timer.setInitialDelay(0);
timer.start();
//Need to obtain a lock on the "monitor" object
//Lock will be released while it is waiting
synchronized(timer){
timer.wait()
//do whatever
}
}
}
這將使“主”線程等待,直到計時器線程調用notify方法,然后喚醒其中一個等待線程。
我也不認為“ ActionListener”擴展了“ TimerTask”。 因此,由於向您提供了一個ActionListener給Timer,該代碼示例可能無法編譯,該Timer希望使用TimerTask作為輸入。 也許我以某種方式不理解您的代碼應該做什么。
請參閱如何在Java中使用等待和通知? 欲獲得更多信息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.