Java 线程完成后,如何使For循环继续?

Java 线程完成后,如何使For循环继续?,java,multithreading,for-loop,java.util.scanner,thread-sleep,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;} } } 使用上述线

使用的线程

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;
            }
        }
    }
}
import java.util.Scanner;
公共课快速数学
{
公共静态void main(字符串[]args)
{
System.out.println(“你能解决多少问题?”);
扫描仪输入=新扫描仪(系统输入);
int total=in.nextInt();
MissedThread m=新的MissedThread();
int right=0;
int错误=0;
int=0;

对于(int i=0;i您不需要等待另一个线程的答案。这是使用单个线程实现的方式:

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);
    }
}
公共类快速数学{
公共静态void main(字符串[]args)引发IOException{
int答案;
System.out.println(“你能解决多少问题?”);
BufferedReader in=新的BufferedReader(新的InputStreamReader(System.in));
int total=Integer.valueOf(in.readLine());
int right=0;
int错误=0;
int=0;
对于(int i=0;i
您必须为此使用
线程吗?还有其他更容易实现的解决方案。扩展
可运行
,而不是
线程
。不要同步
运行
方法。研究如何处理
中断异常
,这是您在这里没有做的。摆脱冗余
返回
,并
continue
语句。主线程不等待计时线程,因此后者无效。线程不共享任何信息,因此主线程无法判断是否违反了时间约束。