如何制作一个循环或每 5 秒执行一次任务然后自行重置的东西?

Posted

技术标签:

【中文标题】如何制作一个循环或每 5 秒执行一次任务然后自行重置的东西?【英文标题】:How to make a loop or something that does a task every 5 seconds and then reset itself? 【发布时间】:2014-05-31 07:07:58 【问题描述】:

我需要让底部的 for 循环每 5 秒执行一次正文中的所有操作,但由于某种原因它无法正常工作!抱歉,如果这听起来很愚蠢,我对 java 很陌生!如果你能帮忙,那就太好了!谢谢! :)

package rBP;

import java.applet.Applet;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class Main extends Applet implements MouseListener 
public void init() 
    super.init();
    addMouseListener(this);


public long numClicks = 0; // 16385
public int autoFingerNum = 0;
public int faultyButtonNum = 0;
public int slickButtonNum = 0;
public int coolButtonNum = 0;
public int heatSinkNum = 0;
public int superButtonNum = 0;
public int aFCost = 30;
public int aFValue = 1;
public int fBCost = 75;
public int fBValue = 3;
public int slBCost = 300;
public int slBValue = 10;
public int cBCost = 1000;
public int cBValue = 25;
public int hSCost = 5000;
public int hSValue = 80;
public int suBCost = 10000;
public int suBValue = 200;

@Override
public void paint(Graphics g) 
    this.setSize(1010, 360);
    g.setColor(Color.cyan);
    g.fillRect(0, 0, 1010, 360); // background
    g.setColor(Color.black);
    g.fillOval(47, 47, 205, 205); // button outline
    g.setColor(Color.red);
    g.fillOval(50, 50, 200, 200); // button
    g.setColor(Color.yellow);
    g.fillRect(2, 5, 290, 30); // yellow boxes
    g.fillRect(300, 25, 100, 250);
    g.fillRect(410, 25, 100, 250);
    g.fillRect(520, 25, 100, 250);
    g.fillRect(630, 25, 100, 250);
    g.fillRect(740, 25, 100, 250);
    g.fillRect(850, 25, 100, 250);
    g.setColor(Color.black);
    g.drawLine(300, 100, 400, 100); // lines on yellow boxes
    g.drawLine(410, 100, 510, 100);
    g.drawLine(520, 100, 620, 100);
    g.drawLine(630, 100, 730, 100);
    g.drawLine(740, 100, 840, 100);
    g.drawLine(850, 100, 950, 100);
    g.drawLine(300, 190, 400, 190);
    g.drawLine(410, 190, 510, 190);
    g.drawLine(520, 190, 620, 190);
    g.drawLine(630, 190, 730, 190);
    g.drawLine(740, 190, 840, 190);
    g.drawLine(850, 190, 950, 190);
    g.fillRect(307, 197, 86, 71); // boxes around "buy" buttons
    g.fillRect(417, 197, 86, 71);
    g.fillRect(527, 197, 86, 71);
    g.fillRect(637, 197, 86, 71);
    g.fillRect(747, 197, 86, 71);
    g.fillRect(857, 197, 86, 71);
    g.setColor(Color.red);
    g.fillRect(310, 200, 80, 65); // red "buy" buttons
    g.fillRect(420, 200, 80, 65);
    g.fillRect(530, 200, 80, 65);
    g.fillRect(640, 200, 80, 65);
    g.fillRect(750, 200, 80, 65);
    g.fillRect(860, 200, 80, 65);
    g.setColor(Color.black);
    g.setFont(new Font("default", Font.BOLD, 12));
    g.drawString("Number of Points: " + numClicks, 5, 25); // num of points
    g.setFont(new Font("default", Font.BOLD, 20));
    g.drawString("DO", 135, 120);
    g.drawString("NOT", 127, 150);
    g.drawString("PRESS", 115, 180);
    g.setFont(new Font("default", Font.BOLD, 16));
    g.drawString("AutoFinger", 310, 50);
    g.drawString("FaultyButton", 411, 50);
    g.drawString("SlickButton", 524, 50);
    g.drawString("CoolButton", 635, 50);
    g.drawString("HeatSink", 754, 50);
    g.drawString("SuperButton", 851, 50);
    g.drawString("+1p/5s", 310, 75);
    g.drawString("+3p/5s", 434, 75);
    g.drawString("+10p/5s", 540, 75);
    g.drawString("+40p/5s", 648, 75);
    g.drawString("+100p/5s", 756, 75);
    g.drawString("+500p/5s", 866, 75);
    g.drawString("Buy: 30P", 315, 240);
    g.drawString("Buy: 75P", 425, 240);
    g.drawString("Buy: 300P", 530, 240);
    g.setFont(new Font("default", Font.BOLD, 15));
    g.drawString("Buy: 1000P", 642, 240);
    g.drawString("Buy: 5000P", 752, 240);
    g.setFont(new Font("default", Font.BOLD, 13));
    g.drawString("Buy: 10000P", 862, 240);
    g.setFont(new Font("default", Font.BOLD, 12));
    g.drawString("aFNumber: " + autoFingerNum, 305, 150);
    g.drawString("fBNumber: " + faultyButtonNum, 415, 150);
    g.drawString("sBNumber: " + slickButtonNum, 525, 150);
    g.drawString("cBNumber: " + coolButtonNum, 635, 150);
    g.drawString("hSNumber: " + heatSinkNum, 745, 150);
    g.drawString("sBNumber: " + superButtonNum, 855, 150);



@Override
public void mouseClicked(MouseEvent e) 

    if ((e.getX() > 50 && e.getX() < 250)
            && (e.getY() > 50 && e.getY() < 250)) 
        numClicks++;
        repaint();
    
    if ((e.getX() > 310 && e.getX() < 390)
            && (e.getY() > 200 && e.getY() < 265)
            && numClicks > (aFCost - 1)) 
        autoFingerNum++;
        numClicks -= aFCost;
        repaint();
    
    if ((e.getX() > 420 && e.getX() < 500)
            && (e.getY() > 200 && e.getY() < 265)
            && numClicks > (fBCost - 1)) 
        faultyButtonNum++;
        numClicks -= fBCost;
        repaint();
    
    if ((e.getX() > 530 && e.getX() < 610)
            && (e.getY() > 200 && e.getY() < 265)
            && numClicks > (slBCost - 1)) 
        slickButtonNum++;
        numClicks -= slBCost;
        repaint();
    
    if ((e.getX() > 640 && e.getX() < 720)
            && (e.getY() > 200 && e.getY() < 265)
            && numClicks > (cBCost - 1)) 
        coolButtonNum++;
        numClicks -= cBCost;
        repaint();
    
    if ((e.getX() > 750 && e.getX() < 830)
            && (e.getY() > 200 && e.getY() < 265)
            && numClicks > (hSCost - 1)) 
        heatSinkNum++;
        numClicks -= hSCost;
        repaint();
    
    if ((e.getX() > 860 && e.getX() < 940)
            && (e.getY() > 200 && e.getY() < 265)
            && numClicks > (suBCost - 1)) 
        superButtonNum++;
        numClicks -= suBCost;
        repaint();
    



@Override
public void mouseEntered(MouseEvent arg0) 


@Override
public void mouseExited(MouseEvent arg0) 


@Override
public void mousePressed(MouseEvent arg0) 


@Override
public void mouseReleased(MouseEvent arg0) 


public void output() 
    for (int i = 0; i >= 50; i++)      //<- I just set it at 50 for testing              purposes
                    i = 0;
        numClicks += autoFingerNum * aFValue;
        numClicks += faultyButtonNum * fBValue;
        numClicks += slickButtonNum * slBValue;
        numClicks += coolButtonNum * cBValue;
        numClicks += heatSinkNum * hSValue;
        numClicks += superButtonNum * suBValue;
    

【问题讨论】:

在单独的线程上运行要重复的任务。使用 Thread.sleep 等待 5 秒或您需要的任意时间。 你已经发布了很多代码,看起来只有 output` 方法在这里是相关的。也就是说,看看javax.swing.Timer 尝试将 i>=50 更改为 i 检查java的Sleep方法 【参考方案1】:

Timer + TimerTask 可能适合您的需求。这是一个代码示例:

TimerTask task = new MyTask();
Timer timer = new Timer();
timer.scheduleAtFixedRate(task, startTime, interval);

MyTask 在哪里:

public class MyTask extends TimerTask 
    @Override
    public void run() 
        // do work here
    

【讨论】:

【参考方案2】:

Thread.sleep 使当前线程暂停执行一段指定的时间。这是使处理器时间可用于应用程序的其他线程或可能在计算机系统上运行的其他应用程序的有效方法。 sleep方法也可以用于起搏,如下图

boolean continueExec = true; //you can use this to exit the loop

public void output() 

   while(this.continueExec )
        Thread.sleep(5000); //wait 5 seconds     
        numClicks += autoFingerNum * aFValue;
        numClicks += faultyButtonNum * fBValue;
        numClicks += slickButtonNum * slBValue;
        numClicks += coolButtonNum * cBValue;
        numClicks += heatSinkNum * hSValue;
        numClicks += superButtonNum * suBValue;
    

【讨论】:

【参考方案3】:

使用javax.swing.Timer。您可以安排它在任何给定的时间间隔或任何预定时间之后运行您的事件处理代码,并且此代码将在 Swing 线程中运行,您可以在其中安全地操作按钮、标签等。

如果你只是想定期改变绘画,定时器可以设置一些对你的绘画有影响的变量,然后调用repaint()

使用您自己的单独线程会更复杂,因为它不能接触任何 GUI 组件,除非使用 SwingUtils.invokeLater 或类似的东西。

【讨论】:

【参考方案4】:

使用线程执行output()的语句

Thread timer = new Thread()
        public void run()
            try
                while(true )

                    numClicks += autoFingerNum * aFValue;
                    numClicks += faultyButtonNum * fBValue;
                    numClicks += slickButtonNum * slBValue;
                    numClicks += coolButtonNum * cBValue;
                    numClicks += heatSinkNum * hSValue;
                    numClicks += superButtonNum * suBValue;
                    repaint();
                    sleep(5000);
                


            catch(InterruptedException e)
                e.printStackTrace();
            finally

            
        
    ;
    timer.start();

【讨论】:

以上是关于如何制作一个循环或每 5 秒执行一次任务然后自行重置的东西?的主要内容,如果未能解决你的问题,请参考以下文章

如何每 20 秒执行一次 bash 脚本? ,好用睡眠和循环?

C# 要想给一个死循环设置循环时间间隔,每5秒循环一次。

以固定的时间间隔执行for循环,例如。每5秒获取一次图[重复]

如何在 .NET 中每小时(或每小时的特定时间间隔)引发一个事件?

Adobe flash (AS2):如何制作一个永远重复的循环,并且每 5 秒复制一个影片剪辑?

如何让linux定时任务crontab按秒执行