在Java 线程中返回值的用法

有时在执行线程中需要在线程中返回一个值;常规中我们会用Runnable接口和Thread类设置一个变量;在run()中改变变量的值,再用一个get方法取得该值,但是run何时完成是未知的;我们需要一定的机制来保证。

在在Java se5有个Callable接口;我们可以用该接口来完成该功能;

代码如:

Java代码   收藏代码
  1. package com.threads.test;  
  2.   
  3. import java.util.concurrent.Callable;  
  4.   
  5. public class CallableThread implements Callable<String> {  
  6.     private String str;  
  7.     private int count=10;  
  8.     public CallableThread(String str){  
  9.         this.str=str;  
  10.     }  
  11.     //需要实现Callable的Call方法  
  12.     public String call() throws Exception {  
  13.         for(int i=0;i<this.count;i++){  
  14.             System.out.println(this.str+" "+i);  
  15.         }  
  16.         return this.str;  
  17.     }  
  18. }  



在call方法中执行在run()方法中一样的任务,不同的是call()有返回值。
call的返回类型应该和Callable<T>的泛型类型一致。
测试代码如下:

Java代码   收藏代码
  1. package com.threads.test;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.concurrent.ExecutionException;  
  5. import java.util.concurrent.ExecutorService;  
  6. import java.util.concurrent.Executors;  
  7. import java.util.concurrent.Future;  
  8.   
  9. public class CallableTest {   
  10.     public static void main(String[] args) {  
  11.         ExecutorService exs=Executors.newCachedThreadPool();  
  12.         ArrayList<Future<String>> al=new ArrayList<Future<String>>();  
  13.         for(int i=0;i<10;i++){  
  14.             al.add(exs.submit(new CallableThread("String "+i)));  
  15.         }  
  16.           
  17.         for(Future<String> fs:al){  
  18.             try {  
  19.                 System.out.println(fs.get());  
  20.             } catch (InterruptedException e) {  
  21.                 e.printStackTrace();  
  22.             } catch (ExecutionException e) {  
  23.                 e.printStackTrace();  
  24.             }  
  25.         }  
  26.     }  
  27.   
  28. }  


submit()方法返回一个Futrue对象,可以通过调用该对象的get方法取得返回值。
通过该方法就能很好的处理线程中返回值的问题。

 

 

http://icgemu.iteye.com/blog/467848

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
java实现线程返回值的方法有多种。其一种方法是通过继承Thread类或者实现Runnable接口,在子线程设置共享变量来获取返回值。这种方法的缺点是不能精确判断子线程是否执行完成。另一种方法是通过实现Callable接口,使用FutureTask启动子线程,然后使用FutureTask的get()方法获取返回值。这种方法可以精确地获取返回值。 以下是两种方法的示例代码: 方法一,通过设置共享变量获取返回值: ```java public class TestThread extends Thread { public String value = null; @Override public void run() { System.out.println("TestThread start...."); try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("TestThread end"); value = "TestThread"; } } public class Main { public static void main(String[] args) throws InterruptedException { TestThread thread = new TestThread(); thread.start(); while (thread.value == null) { Thread.sleep(100); } System.out.println(thread.value); } } ``` 方法二,使用Callable和FutureTask获取返回值: ```java import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.FutureTask; public class TestCallable implements Callable<String> { @Override public String call() throws Exception { System.out.println("TestCallable start...."); try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("TestCallable end"); return "TestCallable"; } } public class Main { public static void main(String[] args) throws ExecutionException, InterruptedException { FutureTask<String> futureTask = new FutureTask<>(new TestCallable()); Thread thread = new Thread(futureTask); thread.start(); String value = futureTask.get(); System.out.println(value); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值