0

我正在尝试制作从文件中读取条目的程序,在数组中收集 10 个 etnries,然后以该数组作为参数提交一个线程。在这一点上,一切在逻辑上似乎都是正确的,但我在运行时收到绝对不同的结果:

代码:

public class headScanner {

    public static void main(String args[]){

        Scanner our_file                 = null;
        ArrayList<String> our_urls       = new ArrayList<String>();
        List<Future<String>> futures     = new ArrayList<Future<String>>(10);
        ArrayList<String> urls_buffer    = new ArrayList<String>();

        try {
        our_file = new Scanner (new File ("list.txt"));
        }
        catch(IOException e){
            System.out.println("[-] Cant open the file!");
            System.exit(0);
        }

        System.out.println("[!] Creaing pool");
        int max_threads     = 50;
        int urls_per_thread = 10;

        ExecutorService threadPool  = Executors.newFixedThreadPool(max_threads);
        ArrayList<String> buffer = new ArrayList<String>();
        String url;
        while(our_file.hasNext()){

            url = our_file.next();
            if (url.length()>0){
            buffer.add(url);
            }

            if(buffer.size()==urls_per_thread){

                System.out.println("[~] Buffer is full, spawn thread...");
                GoGo child = new GoGo();
                child.some = buffer;
                threadPool.submit(child);

                buffer.clear();

            }
        }

        threadPool.shutdown();
        try {
        threadPool.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
        }
        catch (InterruptedException e ) {
        }

        for(Future<String> after: futures){
                try {
                    String result = after.get();
                    System.out.println(result);
                    }
                catch (InterruptedException e) {
                    System.out.println(e);
                    } catch (ExecutionException e) {
                    System.out.println(e);
                    } finally {
                    }


        }
    }
}


class GoGo implements Runnable{

ArrayList<String> some = new ArrayList<String>();

public void GoGo(ArrayList<String> received_array){
    this.some = received_array;
}

public void run(){
    System.out.println("I am thread");
    System.out.println("Size of array received is:" + this.some.size());
    for(String element: this.some){
        System.out.println(element);
    }
}
}

当我运行它时,我得到不同的结果,无法理解为什么:

anybody@anymachine ~/java $ javac headScanner.java 
anybody@anymachine ~/java $ java headScanner 
[!] Creaing pool
[~] Buffer is full, spawn thread...
I am thread
Size of array received is:3
http://www.top-technik.com/
[~] Buffer is full, spawn thread...
I am thread
Size of array received is:0
anybody@anymachine ~/java $ java headScanner 
[!] Creaing pool
[~] Buffer is full, spawn thread...
I am thread
Size of array received is:4
http://www.top-technik.com/
[~] Buffer is full, spawn thread...
I am thread
Size of array received is:0
anybody@anymachine ~/java $ java headScanner 
[!] Creaing pool
[~] Buffer is full, spawn thread...
I am thread
Size of array received is:3
[~] Buffer is full, spawn thread...
I am thread
Size of array received is:0

抱歉愚蠢的问题,我是否正确地将参数传递给可运行类?我试过了:

 threadPool.submit(new MyClass(myarray));

这给了我错误:

headScanner.java:49: error: constructor GoGo in class GoGo cannot be applied to given types;
                    threadPool.submit(new GoGo(buffer));
                                      ^
  required: no arguments
  found: ArrayList<String>
  reason: actual and formal argument lists differ in length
1 error

可能是传递参数的更正确方法,但我无法搜索任何内容。感谢您的任何帮助

4

1 回答 1

0

没有构造函数GoGo接受ArrayList<String>. 像这样定义一个构造函数GoGo

class GoGo implements Runnable{

ArrayList<String> some;

public GoGo(ArrayList<String> received_array){
    this.some = received_array;
}
..
}
于 2013-10-25T13:30:23.007 回答