简体   繁体   中英

How Do I Make this For Loop continue after the Thread finishes?

Thread used

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

Program that uses aforementioned Thread

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

So the purpose of the program is that if the user does not enter a number within 1 second (duration of the Thread.sleep), it will print a message and continue onto the next iteration. However instead if it's answered in time, it will just stop the program. And if it's not answered in time, it seems to get stuck and not move to the next iteration of the for-loop.

You don't need to wait for the answer from another thread. This is how it could be done using a single thread:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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