PooledSjavac.java revision 3265:b7583d50f67d
1175702Smarius/* 2175702Smarius * Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved. 3175702Smarius * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4175702Smarius * 5175702Smarius * This code is free software; you can redistribute it and/or modify it 6175702Smarius * under the terms of the GNU General Public License version 2 only, as 7175702Smarius * published by the Free Software Foundation. Oracle designates this 8175702Smarius * particular file as subject to the "Classpath" exception as provided 9175702Smarius * by Oracle in the LICENSE file that accompanied this code. 10175702Smarius * 11175702Smarius * This code is distributed in the hope that it will be useful, but WITHOUT 12175702Smarius * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13175702Smarius * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14175702Smarius * version 2 for more details (a copy is included in the LICENSE file that 15175702Smarius * accompanied this code). 16175702Smarius * 17175702Smarius * You should have received a copy of the GNU General Public License version 18175702Smarius * 2 along with this work; if not, write to the Free Software Foundation, 19175702Smarius * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20175702Smarius * 21175702Smarius * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22175702Smarius * or visit www.oracle.com if you need additional information or have any 23175702Smarius * questions. 24175702Smarius */ 25175702Smarius 26175702Smariuspackage com.sun.tools.sjavac.comp; 27175702Smarius 28175702Smariusimport com.sun.tools.sjavac.Log; 29175702Smariusimport com.sun.tools.sjavac.server.Sjavac; 30175702Smarius 31175702Smariusimport java.util.Objects; 32175702Smariusimport java.util.concurrent.ExecutorService; 33175702Smariusimport java.util.concurrent.Executors; 34175702Smariusimport java.util.concurrent.TimeUnit; 35175702Smarius 36175702Smarius/** 37175702Smarius * An sjavac implementation that limits the number of concurrent calls by 38175702Smarius * wrapping invocations in Callables and delegating them to a FixedThreadPool. 39175702Smarius * 40175702Smarius * <p><b>This is NOT part of any supported API. 41175702Smarius * If you write code that depends on this, you do so at your own risk. 42175702Smarius * This code and its internal interfaces are subject to change or 43175702Smarius * deletion without notice.</b> 44175702Smarius */ 45175702Smariuspublic class PooledSjavac implements Sjavac { 46175702Smarius 47175702Smarius final Sjavac delegate; 48175702Smarius final ExecutorService pool; 49175702Smarius 50175702Smarius public PooledSjavac(Sjavac delegate, int poolsize) { 51175702Smarius Objects.requireNonNull(delegate); 52175702Smarius this.delegate = delegate; 53175702Smarius pool = Executors.newFixedThreadPool(poolsize); 54175702Smarius } 55175702Smarius 56175702Smarius @Override 57175702Smarius public int compile(String[] args) { 58175702Smarius Log log = Log.get(); 59175702Smarius try { 60175702Smarius return pool.submit(() -> { 61175702Smarius Log.setLogForCurrentThread(log); 62175702Smarius return delegate.compile(args); 63175702Smarius }).get(); 64175702Smarius } catch (Exception e) { 65175702Smarius e.printStackTrace(); 66175702Smarius throw new RuntimeException("Error during compile", e); 67175702Smarius } 68175702Smarius } 69175702Smarius 70175702Smarius @Override 71175702Smarius public void shutdown() { 72175702Smarius Log.debug("Shutting down PooledSjavac"); 73175702Smarius pool.shutdown(); // Disable new tasks from being submitted 74175702Smarius try { 75175702Smarius // Wait a while for existing tasks to terminate 76175702Smarius if (!pool.awaitTermination(60, TimeUnit.SECONDS)) { 77175702Smarius pool.shutdownNow(); // Cancel currently executing tasks 78175702Smarius // Wait a while for tasks to respond to being cancelled 79175702Smarius if (!pool.awaitTermination(60, TimeUnit.SECONDS)) 80175702Smarius Log.error("ThreadPool did not terminate"); 81175702Smarius } 82175702Smarius } catch (InterruptedException ie) { 83175702Smarius // (Re-)Cancel if current thread also interrupted 84175702Smarius pool.shutdownNow(); 85175702Smarius // Preserve interrupt status 86175702Smarius Thread.currentThread().interrupt(); 87175702Smarius } 88175702Smarius 89175702Smarius delegate.shutdown(); 90175702Smarius } 91175702Smarius 92175702Smarius} 93175702Smarius