2012-12-05 15 views
0

我正在處理一個文件,在java中,我正在計算該文件的每一行中特定文件的出現次數我在哪裏啓動每行 的線程作爲下面Java:在線程處理期間無法獲取列表大小

input = new BufferedReader(new FileReader(new File(finaName))); 
    String line = null; 
    while ((line = input.readLine()) != null) { 
FileThread t = new FileThread(line); 
t.start(); 
    } 

FileThread st = new FileThread(); 
System.out.println("Size"+st.list.size()); 

顯示FileThread.java

public class FileThread extends Thread 
List<String> list = new ArrayList<String>(); 

public FileThread(){} 
public FileThread(String line){} 
private String line = null; 
    public void run() { 
    String[] tokens = line.split("\\|", LIST.length); 
      String Symbol = null; 
      try { 
      Symbol = tokens[4]; 
        list.add(symbol); 

      } catch (Throwable t1) { 
       t1.printStackTrace(); 
      } 


     } 

的問題是,我總是得到大小爲0。 請參閱樣本輸出

BIM!一個 BIM!乙 BIM!c請 BIM!d Size0

+0

Java線程不是輕量級的..要小心你多少你 – jozefg

+0

我分裂的字符是| – Pawan

+0

請給出您的確切代碼。你在你的問題中編寫的內容甚至不會編譯('line'不會在'FileThread'中記住,而是在'run()'中使用,而'LIST'沒有聲明。 –

回答

0

我已經在你的代碼的一些變化,可以幫助您

public class FileThread extends Thread { 
    private List<String> list = new ArrayList<String>(); 
    private boolean inThread = false; 
    ....... 
    public int getListSize() { 
     if(!inThread) 
      return list != null ? list.size() : 0 ; 
     else 
      return 0; 
    } 

    public FileThread(String line) { 
    } 

    public void run() { 
     inThread = true; 
      // You logic of the thread 
     inThread = false; 
    } 

    public static void main(String args[]) throws IOException { 
    .............. 

    .............. 

    .............. 
     System.out.println("Size" + st.getListSize()); 
    } 

} 
+0

不能在返回列表中編譯!= null? list.size()? 0; – Pawan

+0

在代碼中做了一些更新。現在你可以檢查 –

+0

謝謝但是它的大小是0 – Pawan