2014-09-25 87 views
0

我有一個線程正在工作,但現在我添加了一些代碼,它不再運行。我嘗試了沒有代碼,它的工作原理。有人可以看看代碼,並確保我沒有做錯什麼?Java線程未運行

public class TimeBellServer extends Thread{ 
    static XBellDB[] xbellDB; 
    static InterviewsDB[] interviewsDB; 
    static boolean isPaused = false; 
    static boolean bellsToday = false; 

    public TimedBellServer() { 

    } 

    public void run() { 
     try { 
      xbellDB = new DBRefresh().refreshDB(); 
      System.out.println("Cody BAE");  
      //!this.isInterrupted() && 
      while(!TimedBellServer.isPaused) { 
       SimpleDateFormat format = new SimpleDateFormat("HH:mm"); 
       Date current = new Date(); 
       String formatCurrent = format.format(current); 
       String date = formatCurrent + ":00"; 
       boolean anyRung = false; 

       System.out.println("--TimedBellServer: The current system time is: " + date); 

       if(bellsToday) { 
        for(int i = 0; i != xbellDB.length; i++) { 
         if(xbellDB[i].time.equals(date)) { 
          System.out.println("--TimedBellServer: Ringing bell \"" + xbellDB[i].name + "\""); 
          TCPConnection.isPaused = true; 

          GPIO.initiate(18); 
          Thread.sleep(100); 
          GPIO.bellOn(); 
          Thread.sleep(8000); 
          GPIO.bellOff(); 
          Thread.sleep(100); 
          GPIO.unExport(); 

          TCPConnection.isPaused = false; 
         } 
        } 
       } 

       if(ParentInterviews.parentInterviewsToday) { 
        if(interviewsDB[0].value.equals(date)) { 
         ParentInterviews.isPaused = false; 
         isPaused = true; 
         TCPConnection.isPaused = true; 

         System.out.println("--TimedBellServer: Parent Interviews First Block has started"); 
        } 

        if(interviewsDB[1].value.equals(date)) { 
         ParentInterviews.isPaused = true; 
         TCPConnection.isPaused = false; 

         System.out.println("--TimedBellServer: Parent Interviews break has started"); 

        } 

        if(interviewsDB[2].value.equals(date)) { 
         ParentInterviews.isPaused = false; 
         TCPConnection.isPaused = true; 

         System.out.println("--TimedBellServer: Parent Interviews Second Block has started"); 
        } 

        if(interviewsDB[3].value.equals(date)) { 
         ParentInterviews.isPaused = true; 
         TCPConnection.isPaused = false; 
         isPaused = false; 

         System.out.println("--TimedBellServer: Parent Interviews Second Block has ended"); 
        } 
       } 

       if(date.equals("00:01")) { 
        ParentInterviews.parentInterviewsToday = false; 
       } 

       if(anyRung == false) {System.out.println("--TimedBellServer: No bells rung");} 
       Thread.sleep(60000); 
      } 
     } catch (Exception e) { 

     } 
    } 

    public static void refresh(XBellDB[] db) { 
     xbellDB = db; 
    } 

    public static void refreshInterviews(InterviewsDB[] db) { 
     interviewsDB = db; 
    } 

我用它來運行代碼:

Thread tbs = new Thread(new TimedBellServer()); 
tbs.start(); 

感謝

+2

我不會在這個上下文中使用'static'(至少要小心),因爲當你改變這個值的時候,所有的線程都會得到相同的值 – MadProgrammer 2014-09-25 05:11:45

+5

考慮提供一個[可運行的例子](https:// stackoverflow.com/help/mcve),它演示了你的問題。這會減少混淆和更好的反應 – MadProgrammer 2014-09-25 05:12:25

+0

代碼的哪一部分導致問題? – Thilo 2014-09-25 05:12:55

回答

0

雖然問題沒有提供足夠的相應的信息,我要在這裏斗膽的猜測。該問題沒有提供任何有關導致代碼無法運行的代碼片段的信息。

但是,因爲你沒有看到在所有的任何輸出(println語句的結果),並因爲在run方法的第二個說法是println,並且我相信t.start()是相當多保證調用run方法,我認爲該行

xbellDB = new DBRefresh().refreshDB(); 

是造成一個例外。由於你的catch塊捕捉所有Exceptions併吞下它們,沒有任何記錄,我認爲你無法看到任何堆棧跟蹤或任何其他相關信息。嘗試添加類似

System.out.println(e.getMessage()) 

run方法的主要catch塊內,看看是否有幫助。

+0

我發現80端口上的另一個Web服務器與XAMPP一起運行,而我不知道,Google Chrome顯示了XAMPP,但Java顯示了另一個Web服務器。謝謝! – cheese5505 2014-09-29 08:34:53