Callable

Thread Pool

package sandbox;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Main {

    public static void main(String[] args) {
        try {
            ExecutorService thPool
                    = new ThreadPoolExecutor(
                        0, Integer.MAX_VALUE,
                        30L, TimeUnit.SECONDS,
                        new SynchronousQueue());

            Future<String> future = thPool.submit(new Callable<String>() {

                @Override
                public String call() throws Exception {
                    return Thread.currentThread().getName();
                }
            });
            System.out.println("MAIN:" + Thread.currentThread().getName());
            System.out.println("SUB :" + future.get());

            thPool.shutdown();
        } catch (InterruptedException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ExecutionException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

Java#JavaSE


トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS   sitemap
Last-modified: 2011-01-16 (日) 14:29:02 (5071d)
Short-URL:
ISBN10
ISBN13
9784061426061