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_Threadpool_Threadpoolexecutor - Fatal编程技术网

Java多线程程序的并行执行

Java多线程程序的并行执行,java,multithreading,threadpool,threadpoolexecutor,Java,Multithreading,Threadpool,Threadpoolexecutor,我想使用多线程方法编写一个程序,其中每个线程执行一些IO操作,每个IO操作需要100毫秒才能完成。我的问题是,我想将我的问题设计成这样一种方式,即只要一个线程调用IO操作并进入等待状态,其余线程就应该开始执行 以下是我的初步实验: public class TestThread { public static void main(String args[]) throws InterruptedException { List<String> list = n

我想使用多线程方法编写一个程序,其中每个线程执行一些IO操作,每个IO操作需要100毫秒才能完成。我的问题是,我想将我的问题设计成这样一种方式,即只要一个线程调用IO操作并进入等待状态,其余线程就应该开始执行

以下是我的初步实验:

public class TestThread {
    public static void main(String args[]) throws InterruptedException {
        List<String> list = new ArrayList<>();
        ExecutorService executorService = Executors.newFixedThreadPool(10);
for(int i = 0; i < 50; i++) {
            Future<String> future = executorService.submit(() -> {
                System.out.println(Thread.currentThread().getId());
                return getValue();
               //here instead of calling getValue(), thread will call some IO operation
//each IO call is indepndent of each other
            });
            list.add(future.get());
        }
       }
    static String getValue() throws InterruptedException {
        if(Thread.currentThread().getId() == 11)
        Thread.sleep(10000);
        return "some value for the thread " + Thread.currentThread().getId();
    }
}
以下是观察到的情况: 线程12等待线程11完成其执行。这里我想要的是,一旦11号线程进入睡眠状态,在实际问题中,它将进入IO等待,12号线程应该开始执行。 请帮我设计这个问题

线程12等待线程11完成其执行

这可能是因为您正在将每个任务添加到一个循环中,您可能认为这将以异步方式触发并忘记,但是,通过调用future.get和循环结束,您实际上阻止了成功任务的启动

future.get被阻塞,因此线程11阻止了线程12的启动

我建议采用这种设计,以避免阻碍自己


我建议,如果IO调用彼此独立,那么为什么要鼓励它们彼此等待?为什么他们会产生任何类型的组合列表?

什么?请尝试重申您的问题。您有一个固定的线程池。当然要等。如果线程池小于线程数,它将阻塞。
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.ListeningExecutorService;
import com.google.common.util.concurrent.MoreExecutors;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;

public class TestThread {

    public static List<Integer> execute(ExecutorService executorService, Callable<Integer> callable, int calls)
        throws ExecutionException, InterruptedException {
        ListeningExecutorService listeningExecutorService = MoreExecutors.listeningDecorator(executorService);

        List<ListenableFuture<Integer>> futures = new ArrayList<>();

        for (int i = 0; i < calls; i++) {
            futures.add(listeningExecutorService.submit(callable));
        }

        return Futures.allAsList(futures).get();
    }
}