本文整理匯總了Java中java.util.concurrent.ThreadPoolExecutor.shutdown方法的典型用法代碼示例。如果您正苦於以下問題:Java ThreadPoolExecutor.shutdown方法的具體用法?Java ThreadPoolExecutor.shutdown怎麽用?Java ThreadPoolExecutor.shutdown使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.util.concurrent.ThreadPoolExecutor
的用法示例。
在下文中一共展示了ThreadPoolExecutor.shutdown方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: parallelPixelFromGeo
import java.util.concurrent.ThreadPoolExecutor; //導入方法依賴的package包/類
/**
*
* @param coords
* @return
* @throws InterruptedException
* @throws ExecutionException
*/
public List<double[]> parallelPixelFromGeo(final Coordinate[] coords)
throws InterruptedException, ExecutionException {
int processors = Runtime.getRuntime().availableProcessors();
ThreadPoolExecutor executor = new ThreadPoolExecutor(2, processors, 2, TimeUnit.SECONDS,
new LinkedBlockingQueue<Runnable>());//(ThreadPoolExecutor)Executors.newFixedThreadPool(processors);
try {
final List<Future<double[]>> tasks = new ArrayList<Future<double[]>>();
for (int i = 0; i < coords.length; i++) {
tasks.add(executor.submit(new ParallelReverse(coords[i].x, coords[i].y)));
}
executor.shutdown();
final List<double[]> points = new ArrayList<double[]>();
for (Future<double[]> result : tasks) {
List<double[]> l = Arrays.asList(result.get());
points.addAll(l);
}
return points;
} catch (Exception e) {
if (!executor.isShutdown())
executor.shutdown();
throw e;
}
}
示例2: parallelGeoFromPixel
import java.util.concurrent.ThreadPoolExecutor; //導入方法依賴的package包/類
public List<double[]> parallelGeoFromPixel(final Coordinate[] coords)
throws InterruptedException, ExecutionException {
int processors = Runtime.getRuntime().availableProcessors();
ThreadPoolExecutor executor = new ThreadPoolExecutor(2, processors, 5000, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>());//(ThreadPoolExecutor)Executors.newFixedThreadPool(processors);
List<Callable<double[]>> tasks = new ArrayList<Callable<double[]>>();
for (final Coordinate c : coords) {
tasks.add(new ParallelForward(c.y,c.x));
}
List<Future<double[]>> results = executor.invokeAll(tasks);
executor.shutdown();
List<double[]> points = new ArrayList<double[]>();
for (Future<double[]> result : results) {
List<double[]> l = Arrays.asList(result.get());
points.addAll(l);
}
return points;
}
示例3: parallelBuffer
import java.util.concurrent.ThreadPoolExecutor; //導入方法依賴的package包/類
/**
*
* @param bufferedGeom
* @param bufferDistance
* @return
* @throws InterruptedException
* @throws ExecutionException
*/
private List<Geometry> parallelBuffer(List<Geometry> bufferedGeom,double bufferDistance)throws InterruptedException, ExecutionException {
int processors = Runtime.getRuntime().availableProcessors();
ThreadPoolExecutor executor = new ThreadPoolExecutor(2, processors, 5000, TimeUnit.MILLISECONDS,new LinkedBlockingQueue<Runnable>());
List<Callable<Geometry>> tasks = new ArrayList<Callable<Geometry>>();
for (int i=0;i<bufferedGeom.size();i++) {
tasks.add(new ParallelBuffer(bufferedGeom.get(i),bufferDistance));
}
List<Future<Geometry>> results = executor.invokeAll(tasks);
executor.shutdown();
List<Geometry> geoms = new ArrayList<Geometry>();
for (Future<Geometry> result : results) {
List<Geometry> l = Arrays.asList(result.get());
geoms.addAll(l);
}
return geoms;
}
示例4: testIsTerminated
import java.util.concurrent.ThreadPoolExecutor; //導入方法依賴的package包/類
/**
* isTerminated is false before termination, true after
*/
public void testIsTerminated() throws InterruptedException {
final ThreadPoolExecutor p =
new ThreadPoolExecutor(1, 1,
LONG_DELAY_MS, MILLISECONDS,
new ArrayBlockingQueue<Runnable>(10));
try (PoolCleaner cleaner = cleaner(p)) {
final CountDownLatch threadStarted = new CountDownLatch(1);
final CountDownLatch done = new CountDownLatch(1);
assertFalse(p.isTerminating());
p.execute(new CheckedRunnable() {
public void realRun() throws InterruptedException {
assertFalse(p.isTerminating());
threadStarted.countDown();
await(done);
}});
await(threadStarted);
assertFalse(p.isTerminating());
done.countDown();
try { p.shutdown(); } catch (SecurityException ok) { return; }
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
assertTrue(p.isTerminated());
assertFalse(p.isTerminating());
}
}
示例5: multiThreadUpload
import java.util.concurrent.ThreadPoolExecutor; //導入方法依賴的package包/類
private ThreadPoolExecutor multiThreadUpload(int threadNum, final int threadFileNum) {
ThreadPoolExecutor pool = (ThreadPoolExecutor) Executors.newFixedThreadPool(threadNum);
pool.prestartAllCoreThreads();
for (int i = 0; i < threadNum; ++i) {
final int threadId = i;
pool.submit(new Runnable() {
@Override
public void run() {
uploadAndDownloadPerform(threadId, threadFileNum);
}
});
}
pool.shutdown();
return pool;
}
示例6: testIsTerminated
import java.util.concurrent.ThreadPoolExecutor; //導入方法依賴的package包/類
/**
* isTerminated is false before termination, true after
*/
public void testIsTerminated() throws InterruptedException {
final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
try (PoolCleaner cleaner = cleaner(p)) {
final CountDownLatch threadStarted = new CountDownLatch(1);
final CountDownLatch done = new CountDownLatch(1);
assertFalse(p.isTerminated());
p.execute(new CheckedRunnable() {
public void realRun() throws InterruptedException {
assertFalse(p.isTerminated());
threadStarted.countDown();
await(done);
}});
await(threadStarted);
assertFalse(p.isTerminating());
done.countDown();
try { p.shutdown(); } catch (SecurityException ok) { return; }
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
assertTrue(p.isTerminated());
}
}
示例7: testIsTerminating
import java.util.concurrent.ThreadPoolExecutor; //導入方法依賴的package包/類
/**
* isTerminating is not true when running or when terminated
*/
public void testIsTerminating() throws InterruptedException {
final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
final CountDownLatch threadStarted = new CountDownLatch(1);
final CountDownLatch done = new CountDownLatch(1);
try (PoolCleaner cleaner = cleaner(p)) {
assertFalse(p.isTerminating());
p.execute(new CheckedRunnable() {
public void realRun() throws InterruptedException {
assertFalse(p.isTerminating());
threadStarted.countDown();
await(done);
}});
await(threadStarted);
assertFalse(p.isTerminating());
done.countDown();
try { p.shutdown(); } catch (SecurityException ok) { return; }
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
assertTrue(p.isTerminated());
assertFalse(p.isTerminating());
}
}
示例8: close
import java.util.concurrent.ThreadPoolExecutor; //導入方法依賴的package包/類
public static void close(ThreadPoolExecutor executor, Logger logger) {
executor.shutdown(); // Disable new tasks from being submitted
try {
// Wait a while for existing tasks to terminate
if (!executor.awaitTermination(1, TimeUnit.SECONDS)) {
executor.shutdownNow(); // Cancel currently executing tasks
// Wait a while for tasks to respond to being cancelled
if (!executor.awaitTermination(1, TimeUnit.SECONDS)) {
logger.error("Pool did not terminate");
}
}
} catch (InterruptedException ie) {
logger.warn("Executor interrupted while awaiting termination");
// (Re-)Cancel if current thread also interrupted
executor.shutdownNow();
// Preserve interrupt status
Thread.currentThread().interrupt();
}
}
示例9: testIsTerminated
import java.util.concurrent.ThreadPoolExecutor; //導入方法依賴的package包/類
/**
* isTerminated is false before termination, true after
*/
public void testIsTerminated() throws InterruptedException {
final CountDownLatch done = new CountDownLatch(1);
final ThreadPoolExecutor p = new CustomExecutor(1);
try (PoolCleaner cleaner = cleaner(p)) {
final CountDownLatch threadStarted = new CountDownLatch(1);
p.execute(new CheckedRunnable() {
public void realRun() throws InterruptedException {
assertFalse(p.isTerminated());
threadStarted.countDown();
await(done);
}});
await(threadStarted);
assertFalse(p.isTerminated());
assertFalse(p.isTerminating());
done.countDown();
try { p.shutdown(); } catch (SecurityException ok) { return; }
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
assertTrue(p.isTerminated());
}
}
示例10: realMain
import java.util.concurrent.ThreadPoolExecutor; //導入方法依賴的package包/類
private static void realMain(String[] args) throws Throwable {
final int n = 4;
final CyclicBarrier barrier = new CyclicBarrier(2*n+1);
final ThreadPoolExecutor pool
= new ThreadPoolExecutor(n, 2*n,
KEEPALIVE_MS, MILLISECONDS,
new SynchronousQueue<Runnable>());
final Runnable r = new Runnable() { public void run() {
try {
barrier.await();
barrier.await();
} catch (Throwable t) { unexpected(t); }}};
for (int i = 0; i < 2*n; i++)
pool.execute(r);
barrier.await();
checkPoolSizes(pool, 2*n, n, 2*n);
barrier.await();
long nap = KEEPALIVE_MS + (KEEPALIVE_MS >> 2);
for (long sleepyTime = 0L; pool.getPoolSize() > n; ) {
check((sleepyTime += nap) <= LONG_DELAY_MS);
Thread.sleep(nap);
}
checkPoolSizes(pool, n, n, 2*n);
Thread.sleep(nap);
checkPoolSizes(pool, n, n, 2*n);
pool.shutdown();
check(pool.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
}
示例11: test_Disconnect_Execute_Error
import java.util.concurrent.ThreadPoolExecutor; //導入方法依賴的package包/類
@Test(expected = ExecutionException.class)
public void test_Disconnect_Execute_Error() throws RemotingException{
handler = new ConnectionOrderedChannelHandler(new BizChannelHander(false), url);
ThreadPoolExecutor executor = (ThreadPoolExecutor)getField(handler, "connectionExecutor", 1);
executor.shutdown();
handler.disconnected(new MockedChannel());
}
示例12: mark
import java.util.concurrent.ThreadPoolExecutor; //導入方法依賴的package包/類
/**
* Marks all of the submissions.
* @param threads The thread-pool size to use in marking the assignments
*/
public void mark(final int threads)
throws Exception {
final ProgressBar pb = new ProgressBar("Marking", this.submissions.size());
pb.start();
final CountDownLatch latch = new CountDownLatch(this.submissions.size());
final ThreadPoolExecutor executor = new ThreadPoolExecutor(
threads,
threads,
0L,
TimeUnit.MILLISECONDS,
new ArrayBlockingQueue<>(threads),
new ThreadPoolExecutor.CallerRunsPolicy()
);
for (Submission s : this.submissions) {
executor.submit(() -> {
try {
s.results(this.markingResults(s.directory()));
pb.step();
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
latch.countDown();
}
});
}
executor.shutdown();
latch.await();
pb.stop();
}
示例13: test
import java.util.concurrent.ThreadPoolExecutor; //導入方法依賴的package包/類
void test(String[] args) throws Throwable {
final int n = 100;
final ThreadPoolExecutor pool =
new ThreadPoolExecutor(n, n, 1L, TimeUnit.NANOSECONDS,
new SynchronousQueue<Runnable>());
final CountDownLatch startingGate = new CountDownLatch(n);
final CountDownLatch finishLine = new CountDownLatch(n);
equal(pool.getCorePoolSize(), n);
equal(pool.getPoolSize(), 0);
for (int i = 0; i < n; i++)
pool.execute(new Runnable() { public void run() {
try {
startingGate.countDown();
startingGate.await();
equal(pool.getPoolSize(), n);
pool.setCorePoolSize(n);
pool.setCorePoolSize(1);
check(! Thread.interrupted());
equal(pool.getPoolSize(), n);
finishLine.countDown();
finishLine.await();
check(! Thread.interrupted());
} catch (Throwable t) { unexpected(t); }}});
finishLine.await();
pool.shutdown();
check(pool.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
}
示例14: testDiscardOldestOnShutdown
import java.util.concurrent.ThreadPoolExecutor; //導入方法依賴的package包/類
/**
* execute using DiscardOldestPolicy drops task on shutdown
*/
public void testDiscardOldestOnShutdown() {
final ThreadPoolExecutor p =
new CustomTPE(1, 1,
LONG_DELAY_MS, MILLISECONDS,
new ArrayBlockingQueue<Runnable>(1),
new ThreadPoolExecutor.DiscardOldestPolicy());
try { p.shutdown(); } catch (SecurityException ok) { return; }
try (PoolCleaner cleaner = cleaner(p)) {
TrackedNoOpRunnable r = new TrackedNoOpRunnable();
p.execute(r);
assertFalse(r.done);
}
}
示例15: removeVolume
import java.util.concurrent.ThreadPoolExecutor; //導入方法依賴的package包/類
/**
* Stops AsyncLazyPersistService for a volume.
* @param volume the root of the volume.
*/
synchronized void removeVolume(File volume) {
if (executors == null) {
throw new RuntimeException("AsyncDiskService is already shutdown");
}
ThreadPoolExecutor executor = executors.get(volume);
if (executor == null) {
throw new RuntimeException("Can not find volume " + volume
+ " to remove.");
} else {
executor.shutdown();
executors.remove(volume);
}
}