0

我正在用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());

文件线程.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();
            }


        }

问题是我总是将 Size 设为 0 。请查看示例输出

BIM!A BIM!B BIM!C BIM!D 尺寸0

4

1 回答 1

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());
    }

}
于 2012-12-05T06:16:59.717 回答