2014-09-25 75 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