Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么我的自行车承运人是空的?_Java_Multithreading_Concurrency_Java.util.concurrent_Cyclicbarrier - Fatal编程技术网

Java 为什么我的自行车承运人是空的?

Java 为什么我的自行车承运人是空的?,java,multithreading,concurrency,java.util.concurrent,cyclicbarrier,Java,Multithreading,Concurrency,Java.util.concurrent,Cyclicbarrier,我正在学习ciclycbarrier,我正在尝试创建一个小应用程序。我的应用程序的构造函数如下所示: public FileDownloader(String line, int maxDownload){ this.position = 0; this.line = line; this.maxDownload = maxDownload; this.urls = new ArrayList<String>(); this.ths = new

我正在学习ciclycbarrier,我正在尝试创建一个小应用程序。我的应用程序的构造函数如下所示:

public FileDownloader(String line, int maxDownload){
    this.position = 0;
    this.line = line;
    this.maxDownload = maxDownload;
    this.urls = new ArrayList<String>();
    this.ths = new ArrayList<Thread>();
    this.exm = new Semaphore(1);
    this.GenerateURLS();
    final CyclicBarrier cb = new CyclicBarrier(this.maxDownload, new Runnable(){
        @Override
        public void run(){


            System.out.println("All download are finished");
            //Mergear cuando se termina
            //Borrar cuando se termina
        }

    });

    for(int i=0; i<this.maxDownload;i++){
        ths.add(new Thread(new Task(this.cb),"Hilo"+i));
    }
    for(Thread th: ths){
        th.start();
    }

}
class Task implements Runnable{
    private CyclicBarrier barrier;
    public static int position;
    public Task(CyclicBarrier cb){
        this.barrier = cb;

    }

    public void run(){
        try {

            FileDownloader.DownloadManager();
            this.barrier.await();
        } catch (InterruptedException | BrokenBarrierException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.out.println(e.getStackTrace());
        }
    }
}
但问题是,当方法DownloadFile(在我的任务运行中)结束时,该执行cb.await了,我出现了下一个错误:

Exception in thread "Hilo1" java.lang.NullPointerException
    at Prac2.Task.run(FileDownloader.java:23)
    at java.lang.Thread.run(Thread.java:745)
通过调试,我可以看到任务中的cyclicbarrier(barrier)总是空的,但cb不是空的


有什么问题吗?

因为您正在
文件下载程序
构造函数中创建一个本地
cb
变量,但您正在将未初始化的
this.cb
传递给
任务
构造函数。

仔细查看您的代码

创建局部变量cb

 final CyclicBarrier cb = new CyclicBarrier(this.maxDownload, new Runnable(){
    @Override
    public void run(){
        System.out.println("All download are finished");
        //Mergear cuando se termina
        //Borrar cuando se termina
    }

});
但在这里您可以访问类级别变量

for(int i=0; i<this.maxDownload;i++){
    ths.add(new Thread(new Task(this.cb),"Hilo"+i));
}
注意

this.cb