2017-04-21 1 views
0

プロジェクトで作業していて、プログラムを停止するのが難しかったです。タイマーではなくスレッドを使用しています。なぜなら、作業が簡単だったからです。基本的には、私が今問題にしているのは、main関数から静的関数への時間です。どんな助けもありがとう。私の質問がはっきりしない場合は、コードの重要な部分をコメントに含めました。 TIA静的クラスのスレッドを使用して停止条件を実装する方法

import java.util.Random; 
import java.util.Scanner; 

import javax.swing.JOptionPane; 

public class InLineCustomers { 
    @SuppressWarnings("static-access") 
    public static void main (String args[]){ 
     try{ 
      final long NANOSEC_PER_SEC = 1000l*1000*1000; 

      long startTime = System.nanoTime(); 
      long time = (System.nanoTime()-startTime); 
      final long genTime=3*60*NANOSEC_PER_SEC; 

      while (time<genTime){ //Program runs for 3 minutes 
       customerGenerator(); 

       Random r = new Random();  
       int timeValue=r.nextInt(10);  


       Thread.currentThread().sleep(timeValue * 1000); 
      } 
     } 
     catch (InterruptedException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 


    public static void customerGenerator(){ 
     ...code here 
     if(selection.equalsIgnoreCase("C")){  
      /**This doesn't working because the customerGenerator is in it's own static class 
      * Would the program be more difficult to read if I had everything in the main method? 
      * That's what I'm trying to avoid 
      * 
      * time=genTime; 
       JOptionPane.showMessageDialog(null,"The restaurant is no longer accepting any customers."); 
      */ 

      stop(); //This isn't working because it created a different timer 

     } 

    } 
    public static void stop(){ 
     final long NANOSEC_PER_SEC = 1000l*1000*1000; 
     long startTime = System.nanoTime();    
     long time = (System.nanoTime()-startTime); 
     final long genTime=3*60*NANOSEC_PER_SEC; 
     time=genTime; 
     JOptionPane.showMessageDialog(null,"The restaurant is no longer accepting any customers."); 
    } 

} 
+1

あなたはどこでもスレッドを作成しているわけではなく、単にメインスレッドを使用しています。ここには「メインクラス」または「静的クラス」はなく、静的メイン関数と静的customerGenerator関数は同じクラスに属しています。いくつかの基本的なスレッディングの概念以上に欠けているようです。 – Tibrogargan

+0

ありがとうございましたが、私の質問には全く答えられませんでした。私は質問を編集します。 – DontPrayForMe

答えて

0

は、これはおそらく、最も関連がスレッドにデータを渡すjava.util.concurrent.atomic変数の使用であり、あなたがスレッドと通信可能性があるいくつかの方法を示しています。

public static void main(String[] args) { 

    final java.util.concurrent.atomic.AtomicLong timer = new java.util.concurrent.atomic.AtomicLong((long)(Math.random() * 1000)); 

    Thread thread = new Thread(new Runnable() { 
     @Override 
     public void run() { 
      try { 
       long timeValue = timer.get(); 
       do { 
        System.out.println("Customer does something"); 
        Thread.currentThread().sleep(timeValue); 
        timeValue = timer.get(); 
       } while (0 != timeValue); 
      } 
      catch(InterruptedException interrupt) { 
       // whatever; 
      } 
      System.out.println("Parent says we're all done"); 
     } 
    }); 
    thread.start(); 

    try { 
     Thread.currentThread().sleep((long)(Math.random() * 10 * 1000)); 
     boolean do_it_the_easy_way = true; 
     if (do_it_the_easy_way) { 
      thread.interrupt(); 
     } else { 
      timer.set(0); 
     } 
    } 
    catch(InterruptedException interrupt) { 
     System.out.println("Program halted externally"); 
     return; 
    } 
} 
+0

これは実際にスレッドを実際に作成する方法です。 – DontPrayForMe

+0

これは**スレッドを作成する方法です**。通常、匿名の内部クラスは(顧客ロジックのような)些細なものには使用しません。 – Tibrogargan

関連する問題