How to access a Runnable object by Thread?

前端 未结 11 921
深忆病人
深忆病人 2020-12-08 10:21

Possible duplicate: need-help-returning-object-in-thread-run-method

Hello. I have a class implementing runnable and I have a List, storing Threads instantiated with

11条回答
  •  死守一世寂寞
    2020-12-08 11:10

    The concurrency library supports this well. Note: If your task throws an Exception, the Future will hold this and throw a wrapping exception when you call get()

    ExecutorService executor = Executors.newSingleThreadedExecutor();
    
    Future future = executor.submit(new Callable() { 
       public String call() { 
          return "world"; 
       } 
    }); 
    
    String result = future.get(); 
    

提交回复
热议问题