`
famoushz
  • 浏览: 2873644 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

jdk5多线程调度之 ExecutorService + Future使用

阅读更多
例子代码:
java 代码
 
  1. import java.util.concurrent.Callable;  
  2. import java.util.concurrent.ExecutionException;  
  3. import java.util.concurrent.ExecutorService;  
  4. import java.util.concurrent.Executors;  
  5. import java.util.concurrent.Future;  
  6. class TimeConsumingTask implements Callable {  
  7.  public Person call() throws Exception {  
  8.   ExecutorService executor = Executors.newSingleThreadExecutor();  
  9.   Future future = executor.submit(new Callable<String>(){  
  10.    public String call() throws Exception {  
  11.     return "内部线程代码";  
  12.    }  
  13.      
  14.   });  
  15.   String resultString = null;  
  16.   try {  
  17.    resultString = (String) future.get();  
  18.    System.out.printf("内部: %s\n", resultString);  
  19.   } catch (InterruptedException e) {  
  20.    // TODO Auto-generated catch block  
  21.    e.printStackTrace();  
  22.   } catch (ExecutionException e) {  
  23.    // TODO Auto-generated catch block  
  24.    e.printStackTrace();  
  25.   }  
  26.     
  27.   return new Person(1,"彭帅");  
  28.  }  
  29. }  
  30.    
  31.    
  32. import java.io.DataOutputStream;  
  33. import java.util.concurrent.ExecutionException;  
  34. import java.util.concurrent.ExecutorService;  
  35. import java.util.concurrent.Executors;  
  36. import java.util.concurrent.Future;  
  37.   
  38. public class PersonTest {  
  39.  public static void main(String[] args){  
  40.   ExecutorService executor = Executors.newSingleThreadExecutor();  
  41.   Future future = executor.submit(new TimeConsumingTask());  
  42.   Person resultPerson = null;  
  43.   try {  
  44.    resultPerson = (Person) future.get();  
  45.    resultPerson.move(Direction.DOWN);  
  46.   } catch (InterruptedException e) {  
  47.    // TODO Auto-generated catch block  
  48.    e.printStackTrace();  
  49.   } catch (ExecutionException e) {  
  50.    // TODO Auto-generated catch block  
  51.    e.printStackTrace();  
  52.   }  
  53.  }  
  54. }  
  55.    
  56. import java.util.Hashtable;  
  57. public class Person {  
  58.  private static Hashtable<Direction, String> hashValues = new Hashtable<Direction, String>();  
  59.  static{  
  60.   for(Direction d: Direction.values()){  
  61.    hashValues.put(d, d.name());  
  62.   }  
  63.  }  
  64.    
  65.  private long id;  
  66.  private String name;  
  67.  public Person(){  
  68.     
  69.  }  
  70.  public Person(long id, String name) {  
  71.   super();  
  72.   this.id = id;  
  73.   this.name = name;  
  74.  }  
  75.  public long getId() {  
  76.   return id;  
  77.  }  
  78.  public void setId(long id) {  
  79.   this.id = id;  
  80.  }  
  81.  public String getName() {  
  82.   return name;  
  83.  }  
  84.  public void setName(String name) {  
  85.   this.name = name;  
  86.  }  
  87.  public void move(Direction direct){  
  88.   System.out.printf(this.name + " towarding %s", hashValues.get(direct));  
  89.  }  
  90. }  
  91.   
  92. public enum Direction {  
  93.  UP, DOWN, LEFT, RIGHT;  
  94. }  



分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics