java - 如何让 For 循环在线程结束后继续?

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

使用的线程

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 循环的下一次迭代。

最佳答案

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

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);
    }
}

关于java - 如何让 For 循环在线程结束后继续?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44205953/

相关文章:

java - 使用巨大的 map (putIfAbsent)

java - 是否有一个好的 32 位哈希函数可用于幂等性?

javascript - 是否可以在 javascript 的一个循环中将结果数减少 7、3、2?

C++11 for 循环语法不能正常工作

Java 字符串对象创建

java - 防止 Swing GUI 在调用既访问 Swing 组件又耗时的方法时无响应

iphone - 核心数据和线程/Grand Central Dispatch

java - 我如何使用 Jython 线程,因为它们是 Java 线程?

python - 使用来自不同数据集的 for 循环进行绘图

java - 使用 JPA 和 Java 更新查询