控制台应用程序有两个线程

时间:2016-08-02 05:44:03

标签: java concurrency java.util.scanner

我只想在输入1

时打印learning...
  package a;

    import java.util.Scanner;

    class main extends Thread {
        static String n;

        Scanner reader = new Scanner(System.in);
        public void run() {
            n = reader.nextLine();
        }

        public static void main(String args[]) throws InterruptedException {
            (new Thread(new main())).start();
            n="5";
            System.out.println("1 = ON\n0 = OFF");
            while (n.equals("1")) {
                System.out.println("Learning..");
            }
        }
    }

4 个答案:

答案 0 :(得分:1)

您可能有兴趣阅读Producer-Consumer模式。您可以在这里查看http://javarevisited.blogspot.fr/2012/02/producer-consumer-design-pattern-with.html并尝试使用类似

的内容
class main extends Thread {

// a thread-safe queue for decoupling reading and writing threads avoiding
// synchronization issues. The capacity of the queue is 1 to avoid reading (producing) a
// command without having handled (consumed) the previous before
private static final BlockingQueue<String> sharedQueue = new LinkedBlockingQueue<>(1);

Scanner reader = new Scanner(System.in);

public void run() {
    while (true) {
        String s = reader.nextLine();
        try {
            //if the queue is empty, adds the element, 
            //otherwise blocks waiting for the current element to be handled by main thread
            sharedQueue.put(s);
        } catch (InterruptedException e) {
            e.printStackTrace();
            Thread.currentThread().interrupt();
        }
    }
}

public static void main(String args[]) throws InterruptedException {
    (new Thread(new main())).start();

    System.out.println("1 = ON\n0 = OFF");
    while (true) {
        //will block till an element is available, then removes and handles it
        final String s = sharedQueue.take();
        if ("1".equals(s)) {
            System.out.println("Learning..");
        }
    }
}

}

答案 1 :(得分:0)

使用可以使用下面给出的代码。

class main extends Thread {

    static String n;

    Scanner reader = new Scanner(System.in);

    public void run() {
        while (true) {
            n = reader.nextLine();
            if (Integer.parseInt(n) == 0) {
                break;
            }
        }
    }

    public static void main(String args[]) throws InterruptedException {
        (new Thread(new main())).start();

        System.out.println("1 = ON\n0 = OFF");
        while (n == null) {

        }

        while (n.equals("1")) {
            System.out.println("Learning..");
        }

        System.out.println("DONE");
    }
}

答案 2 :(得分:0)

如果您试图停止启动,最好保持两个线程用于打印,另一个用于输入。尝试使用吹码。它对我来说很好。


  public class ThreadsStop {

    static String n="";

    class Printer extends Thread{

        @Override
        public void run() {

            while(!n.equals(null)){

                try {
                    Thread.sleep(1000);

                    if(n.trim().equals("1"))
                        System.out.println("Learning..");

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }

        }

    }

    class Starter extends Thread{

        @Override
        public void run() {

            Scanner reader = new Scanner(System.in);

            while(true){
                System.out.println("1 = ON \n 0 = OFF");
                n= reader.nextLine();
            }
        }

    }

    public static void main(String[] args) {

        new ThreadsStop().start();

    }

    private void start() {

        new Starter().start();
        new Printer().start();

    }

}

答案 3 :(得分:0)

尝试以下程序,它会接受您的输入并打印出来。

class main extends Thread {
    static String n;

    Scanner reader = new Scanner(System.in);
    public void run() {
        System.out.println("Enter n value ");
        n = reader.nextLine();
    }

    public static void main(String args[]) throws InterruptedException {
        (new Thread(new main())).start();
        n="5";
        System.out.println("1 = ON\n0 = OFF");
        while (n.equals("5")) {
            //System.out.println("Learning..");
        }
        System.out.println(n);
    }
}

在提供主要方法执行的输入之前,您的代码没有接受输入的原因,这意味着程序执行完成。我对你的代码做了一些修改。现在您的代码将接受您的输入。