2015-05-06 196 views
2

我需要從定時間隔的4個單獨.csv數據文件中提取數據以同時輸出數字。我只能得到文件順序拉。我不確定線程​​是否會有所幫助,我從來沒有能夠讓它們正常運行。我的最終結果將在JFrame中顯示輸出。同時讀取.csv文件

import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.IOException; 
import java.util.Scanner; 


public class Inputtest { 
    public static void main(String[] args) throws IOException,  InterruptedException { 

     O21 a = new O21(); 
     O22 b = new O22(); 
     PH1 c = new PH1(); 
     PH2 d = new PH2(); 

     a.O21(); 
     b.O22(); 
     c.PH1(); 
     d.PH2(); 
    } 
} 

class O21 
{ 
    public void O21() throws FileNotFoundException, InterruptedException { 
     int e = 21;   //preset Level 

     Scanner O1 = new Scanner(new File("O21.txt")); 
     O1.useDelimiter(","); 
     while (O1.hasNext()) { 
      String a = O1.next(); 
      int aa = Integer.parseInt(a); 
      Thread.sleep(500);  // Time delay for output 

      if (a.trim().isEmpty()) {  continue;  } 
      if(aa > (e + 1)) { 
       System.out.println(a); 
       System.out.println("Alarm high"); 
       continue; 
      } 
      if(aa < (e-1)) { 
       System.out.println(a); 
       System.out.println("Alarm Low"); 
       continue; 
      } 
      System.out.println(a); 
     } 
    } 
} 

class O22 { 
    public void O22() throws FileNotFoundException, InterruptedException { 
     double f = 20;   //preset Level 
     Scanner O2 = new Scanner(new File("O22.txt")); 
     O2.useDelimiter(","); 
     while (O2.hasNext()) { 
      String b = O2.next(); 
      int bb = Integer.parseInt(b); 
      Thread.sleep(500);  // Time delay for output 

      if (b.trim().isEmpty()) {  continue;  } 

      if(bb > (f + 1)) { 
        System.out.println(b); 
        System.out.println("Alarm high"); 
        continue; 
      } 

      if (bb < (f - 1)) { 
       System.out.println(b); 
       System.out.println("Alarm Low"); 
       continue; 
      } 

      System.out.println(b); 
     } 
    } 
} 

class PH1 { 
    public void PH1() throws FileNotFoundException, InterruptedException { 
     double g = 7;   //preset Level 

     Scanner P1 = new Scanner(new File("PH1.txt")); 
     P1.useDelimiter(","); 
     while (P1.hasNext()) { 
      String c = P1.next(); 
      double cc = Double.parseDouble(c); 
      Thread.sleep(1000);  // Time delay for output 

      if (c.trim().isEmpty()) {  continue;  } 
      if (cc > (g + .5)) { 
       System.out.println(c); 
       System.out.println("Alarm high"); 
       continue; 
      } 

      if (cc < (g - .5)) { 
       System.out.println(c); 
       System.out.println("Alarm Low"); 
       continue; 
      } 
      System.out.println(c); 
     } 
    } 
} 

class PH2 { 
    public void PH2() throws FileNotFoundException, InterruptedException { 
     double h = 7;   //preset Level 

     Scanner P2 = new Scanner(new File("PH2.txt")); 
     P2.useDelimiter(","); 
     while (P2.hasNext()) { 
      String d = P2.next(); 
      double dd = Double.parseDouble(d); 
      Thread.sleep(1000);  // Time delay for output 
      if (d.trim().isEmpty()) {  continue;  } 
      if (dd > (h + .5)) { 
       System.out.println(d); 
       System.out.println("Alarm high"); 
       continue; 
      } 
      if (dd < (h - .5)) { 
       System.out.println(d); 
       System.out.println("Alarm Low"); 
       continue; 
      } 

      System.out.println(d); 
     } 
    } 
} 

回答

1

是的,線程將幫助你。很簡單,

class O21 implements Runnable 
{ 
    @Override public void run() // Used to be your constructor 
    { 
     .... 

而在你main方法:

... 
Thread a = new Thread(new O21()); 
a.start(); 
... 

,同樣爲O22PH1PH2現在將所有的併發執行。

您可以在您的Runnable s中添加構造函數以將參考傳遞給您的JFrame。我將把它作爲一個練習。另外,在SO和其他地方有很多關於線程編程的資源。

乾杯,