简体   繁体   English

如何从线程提交Callable到ExecutorService

[英]How to submit a Callable to an ExecutorService from a thread

I have an application which creates a new thread on a socket connection. 我有一个在套接字连接上创建新线程的应用程序。 I would like to submit a Callable from this thread to an ExecutorService. 我想将此线程中的Callable提交给ExecutorService。 The Callable needs to execute a program via a command line argument, so I don't want to do this via the connection thread. Callable需要通过命令行参数执行程序,因此我不想通过连接线程来执行此操作。

The problem is, I don't know how to submit the Callable to an ExecutorService which has a set thread count. 问题是,我不知道如何将Callable提交给具有固定线程数的ExecutorService。

I had considered doing this with a singleton and writing a submit method to submit my Callable to the ExecutorService instance but being unfamiliar with the api, I wasn't sure if this was sensible. 我曾考虑过以单例方式执行此操作,并编写了一个Submit方法将我的Callable提交给ExecutorService实例,但是由于不熟悉api,所以我不确定这是否明智。

Any help is greatly appreciated, Thanks. 非常感谢任何帮助,谢谢。

I would try 我会尝试

 static final ExecutorService service = Executors.newFixedThreadPool(4);

 Callable call = 
 service.submit(call);

Here is some code I find online about your problem : 这是一些我在网上找到的有关您的问题的代码:

public class CallableExample {

  public static class WordLengthCallable
        implements Callable {
    private String word;
    public WordLengthCallable(String word) {
      this.word = word;
    }
    public Integer call() {
      return Integer.valueOf(word.length());
    }
  }

  public static void main(String args[]) throws Exception {
    ExecutorService pool = Executors.newFixedThreadPool(3);
    Set<Future<Integer>> set = new HashSet<Future≶Integer>>();
    for (String word: args) {
      Callable<Integer> callable = new WordLengthCallable(word);
      Future<Integer> future = pool.submit(callable);
      set.add(future);
    }
    int sum = 0;
    for (Future<Integer> future : set) {
      sum += future.get();
    }
    System.out.printf("The sum of lengths is %s%n", sum);
    System.exit(sum);
  }
}

There is method submit(): 有方法Submit():

ExecutorService service = Executors.(get the one here you like most)();
Callable<Something> callable = (your Callable here);
Future<AnotherSomething> result = service.submit(callable);

Please note than when using executor service, you have no control over when the task actually starts. 请注意,使用执行程序服务时,您无法控制任务实际启动的时间。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从可调用接口返回未来对象(executorService.submit的内部工作) - How to return future object from callable interface (internal working of executorService.submit) 如何将可调用任务提交到ExecutorService超时 - How do get a Callable task submit to an ExecutorService timeout 提交带有回调的可调用任务时 ExecutorService 的工作原理 - How ExecutorService works when submit a callable task with callback ExecutorService.submit( <callable> )花更多的时间? - ExecutorService.submit(<callable>) taking more time? 对象成员是否对Callable&ExecutorService线程安全? - Are object members thread safe for Callable & ExecutorService 如何阻止Callable提交给ExecutorService? - How to stop a Callable submitted to ExecutorService? 如何终止从ExecutorService产生的线程 - How to terminate Thread spawned from ExecutorService 当ExecutorService中的任何线程/可运行/可调用在等待终止时失败时,如何捕获异常 - How to catch an exception when any Thread/Runnable/Callable in an ExecutorService fails while awaiting termination 对提交的引用不明确:<t> 提交(可调用<t> ) 中的 ExecutorService 和方法 submit(Runnable) 中的 ExecutorService 匹配</t></t> - reference to submit is ambiguous: <T>submit(Callable<T>) in ExecutorService and method submit(Runnable) in ExecutorService match 提交实现Callable子接口的任务 <T> 到ExecutorService - Submit task which implements a subinterface of Callable<T> to an ExecutorService
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM