2016-08-02 28 views
1

控制檯應用程序,我只是想打印learning...只要我在生產者 - 消費者模式讀了進入1有兩個線程

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.."); 
      } 
     } 
    } 
+0

你期望'while(n.equals(「1」))'在n =「5」'時循環嗎?您可能還想閱讀多線程和數據競賽。 –

+0

嗯,當我沒有定義它時,它給我一個錯誤。 – nooby

+0

我只是想能夠關閉它,這就是全部。 – nooby

回答

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

    } 

} 
0

您可能會感興趣。你可以在這裏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.."); 
     } 
    } 
} 

}

+0

這似乎是一個不錯的解決方案 - 請考慮添加一些解釋,說明爲什麼這個代碼應該工作 - 您還可以在代碼本身中添加註釋 - 並提供解釋,以幫助其他社區瞭解您所做的解決方案/回答問題。 – ishmaelMakitla

+0

但有一件事,輸入1時沒有循環,只做了一次 – nooby

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

之所以你的代碼沒有輸入,在提供輸入你的主要方法執行,這意味着程序執行完成。我對你的代碼做了一些修改。現在你的代碼將會接受你的輸入。