PooledSjavac.java revision 3356:b99518745035
1/* 2 * Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26package com.sun.tools.sjavac.comp; 27 28import com.sun.tools.javac.main.Main.Result; 29import com.sun.tools.sjavac.Log; 30import com.sun.tools.sjavac.server.Sjavac; 31 32import java.util.Objects; 33import java.util.concurrent.ExecutorService; 34import java.util.concurrent.Executors; 35import java.util.concurrent.TimeUnit; 36 37/** 38 * An sjavac implementation that limits the number of concurrent calls by 39 * wrapping invocations in Callables and delegating them to a FixedThreadPool. 40 * 41 * <p><b>This is NOT part of any supported API. 42 * If you write code that depends on this, you do so at your own risk. 43 * This code and its internal interfaces are subject to change or 44 * deletion without notice.</b> 45 */ 46public class PooledSjavac implements Sjavac { 47 48 final Sjavac delegate; 49 final ExecutorService pool; 50 51 public PooledSjavac(Sjavac delegate, int poolsize) { 52 Objects.requireNonNull(delegate); 53 this.delegate = delegate; 54 pool = Executors.newFixedThreadPool(poolsize); 55 } 56 57 @Override 58 public Result compile(String[] args) { 59 Log log = Log.get(); 60 try { 61 return pool.submit(() -> { 62 Log.setLogForCurrentThread(log); 63 return delegate.compile(args); 64 }).get(); 65 } catch (Exception e) { 66 e.printStackTrace(); 67 throw new RuntimeException("Error during compile", e); 68 } 69 } 70 71 @Override 72 public void shutdown() { 73 Log.debug("Shutting down PooledSjavac"); 74 pool.shutdown(); // Disable new tasks from being submitted 75 try { 76 // Wait a while for existing tasks to terminate 77 if (!pool.awaitTermination(60, TimeUnit.SECONDS)) { 78 pool.shutdownNow(); // Cancel currently executing tasks 79 // Wait a while for tasks to respond to being cancelled 80 if (!pool.awaitTermination(60, TimeUnit.SECONDS)) 81 Log.error("ThreadPool did not terminate"); 82 } 83 } catch (InterruptedException ie) { 84 // (Re-)Cancel if current thread also interrupted 85 pool.shutdownNow(); 86 // Preserve interrupt status 87 Thread.currentThread().interrupt(); 88 } 89 90 delegate.shutdown(); 91 } 92 93} 94