【问题标题】:How Do I Make this For Loop continue after the Thread finishes?线程完成后如何使这个 For 循环继续?
【发布时间】:2017-05-26 16:11:03
【问题描述】:

使用的线程

public class MissedThread extends Thread
{
    public synchronized void run()
    {
        try
        {
            Thread.sleep(1000);
            System.out.println("Too slow");
        }catch(InterruptedException e){return;}
    }
}

使用上述线程的程序

import java.util.Scanner;
public class FastMath
{
    public static void main(String[] args)
    {
        System.out.println("How many questions can you solve?");
        Scanner in = new Scanner(System.in);
        int total = in.nextInt();
        MissedThread m = new MissedThread();
        int right = 0;
        int wrong = 0;
        int missed = 0;

        for(int i = 0;i<total;i++)
        {
            int n1 = (int)(Math.random()*12)+1;
            int n2 = (int)(Math.random()*12)+1;
            System.out.print(n1+" * "+n2+" = ");
            m.start();
            int answer = in.nextInt();
            if(answer==n1*n2)
            {
                right++;
                continue;
            }
            if(answer!=n1*n2)
            {
                wrong++;
                continue;
            }
        }
    }
}

所以程序的目的是,如果用户在 1 秒(Thread.sleep 的持续时间)内没有输入数字,它将打印一条消息并继续下一次迭代。但是,如果它得到及时回答,它只会停止程序。如果没有及时回答,它似乎会卡住,不会移动到 for 循环的下一次迭代。

【问题讨论】:

  • 您必须为此使用Thread 吗?还有其他可能更容易实施的解决方案。
  • 扩展Runnable,而不是Thread。不要同步run 方法。研究如何处理InterruptedException,这里不做。摆脱多余的returncontinue 语句。您的主线程不会等待计时线程,因此后者无效。线程不共享信息,因此主线程无法判断是否违反了时间约束。

标签: java multithreading for-loop java.util.scanner thread-sleep


【解决方案1】:

您无需等待另一个线程的答复。这就是使用单线程完成的方式:

public class FastMath {

    public static void main(String[] args) throws IOException {
        int answer;

        System.out.println("How many questions can you solve?");

        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

        int total = Integer.valueOf(in.readLine());

        int right = 0;
        int wrong = 0;
        int missed = 0;

        for (int i = 0; i < total; i++) {
            int n1 = (int) (Math.random() * 12) + 1;
            int n2 = (int) (Math.random() * 12) + 1;
            System.out.print(n1 + " * " + n2 + " = ");

            long startTime = System.currentTimeMillis();
            while ((System.currentTimeMillis() - startTime) < 3 * 1000
                    && !in.ready()) {
            }

            if (in.ready()) {
                answer = Integer.valueOf(in.readLine());

                if (answer == n1 * n2)
                    right++;
                else
                    wrong++;
            } else {
                missed++;
                System.out.println("Time's up!");
            }
        }
        System.out.printf("Results:\n\tCorrect answers: %d\n\nWrong answers:%d\n\tMissed answers:%d\n", right, wrong, missed);
    }
}

【讨论】:

    猜你喜欢
    • 2021-12-27
    • 2017-07-20
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    • 2018-04-25
    • 1970-01-01
    • 2017-09-07
    • 2017-12-14
    相关资源
    最近更新 更多