【Java】コールバック関数を実装する ~Future インターフェースを使って、実装編~

はじめに

 * Future インターフェースについては、以下の関連記事を参照のこと。
http://blogs.yahoo.co.jp/dk521123/34213161.html

サンプル

Main.java

import java.util.Date;

public class Main {

   public static void main(String[] args) throws InterruptedException {

      try {
         System.out.println("Main start " + new Date());
         long now = System.currentTimeMillis();
         while (now + 10000 > System.currentTimeMillis()) {
            SampleClient client = new SampleClient();
            client.start();
            Thread.sleep(2000);
         }
      } catch (Exception ex) {
         ex.printStackTrace();
      }
      System.out.println("Main Done : " + new Date());
   }
}

SampleClient.java

import java.util.Date;
import java.util.Random;

public class SampleClient extends Thread {
   public SampleClient() {
   }

   public void run() {
      try {
         System.out.println("Request Start : " + new Date());
         SampleProxy sampleProxy = new SampleProxy();
         SampleTask sampleTask = new SampleTask((new Random().nextInt(10)));
         SampleResult result = sampleProxy.execute(sampleTask);
         System.out.println("Request Done ID : " + result.getId() + " Name " + result.getName() + " " + new Date());
      } catch (Exception ex) {
         ex.printStackTrace();
      }
   }
}

SampleProxy.java

import java.util.Date;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

public class SampleProxy {
   private ExecutorService executorService;
   public SampleProxy() {
      this.executorService = Executors.newSingleThreadExecutor();
   }
   
   public SampleResult execute(SampleTask task) throws InterruptedException, ExecutionException {
      try {
         System.out.println("execute Start " + new Date());
         Future<SampleResult> sampleResult = this.executorService.submit(task);
         return sampleResult.get();
      } finally {
         executorService.shutdown();
         executorService.awaitTermination(1, TimeUnit.MINUTES);
         System.out.println("execute Done " + new Date());
      }
   }
}

SampleResult.java

public class SampleResult {
   private String id;
   private String name;
   public SampleResult(String id, String name) {
      System.out.println("SampleResult ID : " + id + " Name " + name);
      this.id = id;
      this.name = name;
   }
   
   public String getId() {
      return this.id;
   }
   public String getName() {
      return this.name;
   }
}

SampleTask.java

import java.util.Date;
import java.util.UUID;
import java.util.concurrent.Callable;

public class SampleTask implements Callable<SampleResult> {
   private Integer sleepTime;

   public SampleTask(Integer sleepTime) {
      this.sleepTime = sleepTime * 1000;
   }

   @Override
   public SampleResult call() throws Exception {
      System.out.println("Sleep Time : " + this.sleepTime + " " + new Date());
      Thread.sleep(sleepTime);
      
      return new SampleResult(UUID.randomUUID().toString(), "TaskResult");
   }
}

出力結果

Main start Wed Dec 03 00:38:26 JST 2014
Request Start : Wed Dec 03 00:38:26 JST 2014
execute Start Wed Dec 03 00:38:26 JST 2014
Sleep Time : 7000 Wed Dec 03 00:38:26 JST 2014
Request Start : Wed Dec 03 00:38:28 JST 2014
execute Start Wed Dec 03 00:38:28 JST 2014
Sleep Time : 0 Wed Dec 03 00:38:28 JST 2014
SampleResult ID : 4ba399e6-18ad-4412-9b6e-d791c9bbff84 Name TaskResult
execute Done Wed Dec 03 00:38:28 JST 2014
Request Done ID : 4ba399e6-18ad-4412-9b6e-d791c9bbff84 Name TaskResult Wed Dec 03 00:38:28 JST 2014
Request Start : Wed Dec 03 00:38:30 JST 2014
execute Start Wed Dec 03 00:38:30 JST 2014
Sleep Time : 4000 Wed Dec 03 00:38:30 JST 2014
Request Start : Wed Dec 03 00:38:32 JST 2014
execute Start Wed Dec 03 00:38:32 JST 2014
Sleep Time : 4000 Wed Dec 03 00:38:32 JST 2014
SampleResult ID : 42360653-a9e1-437b-a1dd-3311eddc4052 Name TaskResult
execute Done Wed Dec 03 00:38:33 JST 2014
Request Done ID : 42360653-a9e1-437b-a1dd-3311eddc4052 Name TaskResult Wed Dec 03 00:38:33 JST 2014
SampleResult ID : 760f32be-0cf5-4469-aad2-d0243924248b Name TaskResult
execute Done Wed Dec 03 00:38:34 JST 2014
Request Done ID : 760f32be-0cf5-4469-aad2-d0243924248b Name TaskResult Wed Dec 03 00:38:34 JST 2014
Request Start : Wed Dec 03 00:38:34 JST 2014
execute Start Wed Dec 03 00:38:34 JST 2014
Sleep Time : 7000 Wed Dec 03 00:38:34 JST 2014
SampleResult ID : 5644df9f-351f-4fc3-9648-b22a9d7d91c6 Name TaskResult
Main Done : Wed Dec 03 00:38:36 JST 2014
execute Done Wed Dec 03 00:38:36 JST 2014
Request Done ID : 5644df9f-351f-4fc3-9648-b22a9d7d91c6 Name TaskResult Wed Dec 03 00:38:36 JST 2014
SampleResult ID : 3306f9e9-1609-4e9f-88ee-068d0ef78f93 Name TaskResult
execute Done Wed Dec 03 00:38:41 JST 2014
Request Done ID : 3306f9e9-1609-4e9f-88ee-068d0ef78f93 Name TaskResult Wed Dec 03 00:38:41 JST 2014

関連記事

コールバック関数を実装する ~自作インターフェースで使って、実装編~

http://blogs.yahoo.co.jp/dk521123/34242678.html