简体   繁体   English

Java中的TimerTask

[英]TimerTask in Java

Currently my output is the equation and answer before the countdown. 目前,我的输出是倒数之前的方程式和答案。 I want the equation, countdown, and answer in that order. 我想要等式,倒计时和答案。 I have tried switching around parts, but I am not sure there is a smart way to do this. 我尝试过切换零件,但是我不确定是否有聪明的方法可以做到这一点。

Here's the code: 这是代码:

    import java.util.Timer;
    import java.util.TimerTask;
    public class S1p4 {

    public static void main(String[] args) {
        Timer timer = new Timer();
        Task task = new Task();
        timer.schedule(task, 1000, 1000);

        int num1 = (int) (Math.random()*10);

   int num2 = (int) (Math.random()*10);

        System.out.println(num1);

            System.out.println("+");

    System.out.println(num2);

    int addition = num1 + num2;

    System.out.println("=");

    System.out.println(addition);

    }
}

    class Task extends TimerTask 

{

    int i=4;

    @Override

    public void run() {

        i--;
        if(i==3)
            System.out.println("3 >>>");
        if(i==2){
            System.out.println("2 >>>");
        }
        if(i==1){
            System.out.println("1 >>>");
            cancel();    

            System.exit(0);
        }   
    }
}

You can first print out the equation. 您可以先打印出等式。 Next start your timer. 接下来启动计时器。 Then, in your main thread, use wait() to pause the tread. 然后,在您的主线程中,使用wait()暂停踩踏。 Then after your timer finishes it last iteration call notify() . 然后,在计时器完成计时后,最后一次迭代调用notify() finally have the answer be printed. 终于有了答案。

See also: http://docs.oracle.com/javase/tutorial/essential/concurrency/guardmeth.html 另请参阅: http : //docs.oracle.com/javase/tutorial/essential/concurrency/guardmeth.html

You could do something like this. 你可以做这样的事情。 Note that I added the result to the constructor of Task , and I added the System.out.println before exit. 请注意,我将结果添加到Task的构造函数中,并且在退出之前添加了System.out.println

import java.util.Timer;
import java.util.TimerTask;

public class S1p4 {

  public static void main(String[] args) {
    Timer timer = new Timer();
    int num1 = (int) (Math.random() * 10);
    int num2 = (int) (Math.random() * 10);
    int addition = (int) num1 + num2;
    System.out.println(num1);
    System.out.println("+");
    System.out.println(num2);
    // Add the result to the task.
    Task task = new Task(addition);
    timer.schedule(task, 1000, 1000);
  }
}

class Task extends TimerTask {
  // Store the result.
  private int result;

  // Construct a Task with the result.
  public Task(int result) {
    super();
    this.result = result;
  }

  // How many times to run.
  int i = 4;

  @Override
  public void run() {
    i--;
    if (i == 3) {
      System.out.println("3>>>");
    } else if (i == 2) {
      System.out.println("2>>>");
    } else {
      System.out.println("1>>>");
      cancel();
      // The timer is done print the result.
      System.out.println("The result was " + result);
      System.exit(0);
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM