2017-04-21 9 views
0

我正在開發一個項目,並且在停止該程序時遇到了一些困難。我使用的是線程而不是定時器,因爲我覺得這很容易處理。基本上,我現在面臨的問題是從主功能到靜態功能的時間。任何幫助,將不勝感激。如果我的問題不明確,我會在代碼的重要部分添加註釋。 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