Home > Blockchain >  How can I implement a thread that gets user input?
How can I implement a thread that gets user input?

Time:04-12

I've implemented a thread that get user input by keyboard. However, when user sets input my program returns an IllegalThreadStateException error, at line 23.

import java.util.Scanner;

public class Main {

    static MyThread myThread = new MyThread();
    static public boolean answered = false;

    public static void main(String[] args) {

        String s = "";
        while (!s.equalsIgnoreCase("q")) {

            int seconds = 0, average = 5;
            if (seconds > average) {
                myThread.stop();
                String phrase = choosePhrase(seconds, average);
                System.out.println(phrase);
                Scanner keyboard = new Scanner(System.in);
                s = keyboard.nextLine();
            } else {
                long createdMillis = System.currentTimeMillis();
                myThread.start();
                while (!answered && seconds < average) {
                    long nowMillis = System.currentTimeMillis();
                    seconds = (int) ((nowMillis - createdMillis) / 1000);
                }
            }
        }
    }

    private static String choosePhrase(int seconds, int average) {
        if (seconds > average   10) {
            return "¿D?";
        } else if (seconds > average   5) {
            return "¿E?";
        } else {
            return "¿F?";
        }
    }
}

class MyThread extends Thread {

    static String[] questions = {"¿A?", "¿B?", "¿C?"};

    public void run() {
        System.out.println("MyThread running");
        Double d = Math.random() * 100;
        int n = (int) (Math.ceil(d) % 3);
        String question = questions[n];

        System.out.println(question);

        Scanner keyboard = new Scanner(System.in);
        String s = keyboard.nextLine();
        stop();
    }
}

Why am I getting this exception? How do I make the thread gets and exits correctly to main function?

CodePudding user response:

To make a thread exits proprely you have to use myThread.interrupt();

CodePudding user response:

I've solved the issue with your suggestions:

import javax.swing.JOptionPane;

public class Main {

    public static void main(String[] args) {
        Latency latency = new Latency();
        latency.start();
    }
}

class Latency {

    int seconds = 0, average = 5;
    MyThread myThread;

    ObjectToPass o = new ObjectToPass();

    public void start() {
        
        String s = "";
        Thread t = null;
        while (!s.equalsIgnoreCase("q")) {

            // The program has just begun, or user has answered a question,
            // or term has expired.
            seconds = 0;
            if (myThread != null) {
                myThread.shutdown();
                myThread = null;
            }

            if (o.answered) {
                o.answered = false;
                myThread = new MyThread(o, new String[]{"¿A?", "¿B?", "¿C?"});

            } else {
                myThread = new MyThread(o, new String[]{"¿D?", "¿E?", "¿F?"});
            }

            t = new Thread(myThread);
            t.start();

            long createdMillis = System.currentTimeMillis();
            while (!this.o.getPlay() && seconds < average) {
                long nowMillis = System.currentTimeMillis();
                seconds = (int) ((nowMillis - createdMillis) / 1000);
            }
        }
    }
}

class ObjectToPass {

    boolean answered = true;

    public synchronized boolean getPlay() {
        return answered;
    }

    public synchronized void setPlay(boolean answered) {
        this.answered = answered;
    }
}

class MyThread implements Runnable {

    ObjectToPass o;
    static String[] questions;

    public MyThread(ObjectToPass o, String[] questions) {
        this.o = o;
        this.questions = questions;
    }

    public void run() {
//        System.out.println("MyThread running");
        Double d = Math.random() * 100;
        int n = (int) (Math.ceil(d) % 3);
        String question = questions[n];

        System.out.println(question);

//        Scanner keyboard = new Scanner(System.in);
//        String s = keyboard.nextLine();
        Object[] options = {"Yes", "No"};
        int oo = JOptionPane.showOptionDialog(null,
                question, "Question", JOptionPane.YES_OPTION,
                JOptionPane.NO_OPTION, null, options, options[0]);

        this.o.setPlay(true);
    }

    public void shutdown() {
        Thread.currentThread().interrupt();
        return;
    }
}
  • Related