本文整理汇总了Java中java.util.concurrent.ThreadPoolExecutor.shutdownNow方法的典型用法代码示例。如果您正苦于以下问题:Java ThreadPoolExecutor.shutdownNow方法的具体用法?Java ThreadPoolExecutor.shutdownNow怎么用?Java ThreadPoolExecutor.shutdownNow使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.concurrent.ThreadPoolExecutor
的用法示例。
在下文中一共展示了ThreadPoolExecutor.shutdownNow方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: 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();
}
}
示例2: testActiveThreadsCount
import java.util.concurrent.ThreadPoolExecutor; //导入方法依赖的package包/类
/**
* This is for testing the active number of threads that were used while
* doing a batch operation. It inserts one row per region via the batch
* operation, and then checks the number of active threads.
* For HBASE-3553
* @throws IOException
* @throws InterruptedException
* @throws NoSuchFieldException
* @throws SecurityException
*/
@Ignore ("Nice bug flakey... expected 5 but was 4..") @Test(timeout=300000)
public void testActiveThreadsCount() throws Exception {
try (Connection connection = ConnectionFactory.createConnection(UTIL.getConfiguration())) {
ThreadPoolExecutor executor = HTable.getDefaultExecutor(UTIL.getConfiguration());
try {
try (Table t = connection.getTable(TEST_TABLE, executor)) {
List<Put> puts = constructPutRequests(); // creates a Put for every region
t.batch(puts);
HashSet<ServerName> regionservers = new HashSet<ServerName>();
try (RegionLocator locator = connection.getRegionLocator(TEST_TABLE)) {
for (Row r : puts) {
HRegionLocation location = locator.getRegionLocation(r.getRow());
regionservers.add(location.getServerName());
}
}
assertEquals(regionservers.size(), executor.getLargestPoolSize());
}
} finally {
executor.shutdownNow();
}
}
}
示例3: test
import java.util.concurrent.ThreadPoolExecutor; //导入方法依赖的package包/类
@Test
public void test() {
int threads = 40;
ThreadPoolExecutor executor = new ThreadPoolExecutor(threads, threads, 1000, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>());
try {
int jobs = 1000;
int add = 1000;
latch = new CountDownLatch(jobs);
for (int i = 0; i < jobs; ++i) {
executor.execute(new IncReadDecValueAsync(add));
}
try {
latch.await();
} catch (InterruptedException e) {
throw new RuntimeException("Interrupted.", e);
}
log.info("Checking value, expecting 0 ...");
if (value != 0) {
testFailed("value == " + value + " != 0");
}
testOk();
} finally {
executor.shutdownNow();
}
}
示例4: test
import java.util.concurrent.ThreadPoolExecutor; //导入方法依赖的package包/类
@Test
public void test() {
int threads = 40;
ThreadPoolExecutor executor = new ThreadPoolExecutor(threads, threads, 1000, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>());
try {
int jobs = 1000;
int add = 1000;
latch = new CountDownLatch(jobs);
for (int i = 0; i < jobs; ++i) {
executor.execute(new PutGetRemoveAsync(i*add, i*add + add));
}
try {
latch.await();
} catch (InterruptedException e) {
throw new RuntimeException("Interrupted.", e);
}
log.info("Checking map, expecting 0 items...");
if (map.size() != 0) {
testFailed("map.size() == " + map.size() + " != 0");
}
if (failure) {
testFailed("Failure is true :(");
}
testOk();
} finally {
executor.shutdownNow();
}
}
示例5: test
import java.util.concurrent.ThreadPoolExecutor; //导入方法依赖的package包/类
@Test
public void test() {
int threads = 40;
ThreadPoolExecutor executor = new ThreadPoolExecutor(threads, threads, 1000, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>());
try {
int jobs = 1000;
int add = 1000;
latch = new CountDownLatch(jobs);
for (int i = 0; i < jobs; ++i) {
executor.execute(new IncValueAsync(0, add));
}
try {
latch.await();
} catch (InterruptedException e) {
throw new RuntimeException("Interrupted.", e);
}
log.info("Checking list, expecing all 0...");
for (int i = 0; i < add; ++i) {
if (list.get(i) != 0) {
testFailed("List[" + i + "] = " + list.get(i) + " != 0 !!! AtomicIntegerList has concurrency issues.");
}
}
testOk();
} finally {
executor.shutdownNow();
}
}
示例6: testAtomicInt
import java.util.concurrent.ThreadPoolExecutor; //导入方法依赖的package包/类
@Test
public void testAtomicInt() {
value = 0;
atomicValue = new AtomicInteger(0);
int threads = 40;
ThreadPoolExecutor executor = new ThreadPoolExecutor(threads, threads, 1000, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>());
try {
int total = 0;
int jobs = 1000;
int add = 1000;
latch = new CountDownLatch(jobs);
for (int i = 0; i < jobs; ++i) {
total += add;
executor.execute(new IncValueAtomicInt(add));
}
try {
latch.await();
} catch (InterruptedException e) {
throw new RuntimeException("Interrupted.", e);
}
log.info("Expected 0, reached " + atomicValue.get());
if (0 != atomicValue.get()) {
log.warning("atomicInteger.addAndGet(1) is not atomic operation !!!");
}
testOk();
} finally {
executor.shutdownNow();
}
}
示例7: test
import java.util.concurrent.ThreadPoolExecutor; //导入方法依赖的package包/类
@Test
public void test() {
int threads = 40;
ThreadPoolExecutor executor = new ThreadPoolExecutor(threads, threads, 1000, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>());
try {
int total = 0;
int jobs = 1000;
int add = 1000;
latch = new CountDownLatch(jobs);
for (int i = 0; i < jobs; ++i) {
executor.execute(new IncValueAsync(total, total+add));
total += add;
}
try {
latch.await();
} catch (InterruptedException e) {
throw new RuntimeException("Interrupted.", e);
}
log.info("Checking set ... set.size() == " + set.size());
if (set.size() != 0) {
testFailed("HashSet.add() / HashSet.remove() has concurrency issues!");
}
testOk();
} finally {
executor.shutdownNow();
}
}
示例8: test
import java.util.concurrent.ThreadPoolExecutor; //导入方法依赖的package包/类
@Test
public void test() {
int threads = 40;
ThreadPoolExecutor executor = new ThreadPoolExecutor(threads, threads, 1000, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>());
try {
int jobs = 1000;
int add = 1000;
latch = new CountDownLatch(jobs);
for (int i = 0; i < jobs; ++i) {
executor.execute(new IncValueAsync(0, add));
}
try {
latch.await();
} catch (InterruptedException e) {
throw new RuntimeException("Interrupted.", e);
}
log.info("Checking list, expecing all 0...");
for (int i = 0; i < add; ++i) {
if (list.get(i) != 0) {
testFailed("List[" + i + "] = " + list.get(i) + " != 0 !!! AtomicLongList has concurrency issues.");
}
}
testOk();
} finally {
executor.shutdownNow();
}
}
示例9: test
import java.util.concurrent.ThreadPoolExecutor; //导入方法依赖的package包/类
@Test
public void test() {
int threads = 40;
ThreadPoolExecutor executor = new ThreadPoolExecutor(threads, threads, 1000, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>());
try {
int jobs = 1000;
int add = 1000;
latch = new CountDownLatch(jobs);
for (int i = 0; i < add; ++i) {
map.put(i, i);
}
for (int i = 0; i < jobs; ++i) {
executor.execute(new GetValue(0, add));
}
try {
latch.await();
} catch (InterruptedException e) {
throw new RuntimeException("Interrupted.", e);
}
log.info("Checking map, expecting " + add + " items...");
if (map.size() != add) {
testFailed("map.size() == " + map.size() + " != " + add);
}
testOk();
} finally {
executor.shutdownNow();
}
}
示例10: test
import java.util.concurrent.ThreadPoolExecutor; //导入方法依赖的package包/类
@Test
public void test(){
ThreadFactory threadFactory = new NamedThreadFactory(null, ScalingThreadPoolExecutorTests.class.getSimpleName(),
false);
ThreadPoolExecutor executor = new ScalingThreadPoolExecutor(0, MAX_THREADS, 0, TimeUnit.SECONDS, threadFactory);
Phaser phaser = new Phaser(1);
List<Future<?>> futures = new ArrayList<>();
for(int i = 0; i < MAX_THREADS; i++){
Assert.assertEquals(executor.getActiveCount(), i);
Assert.assertEquals(executor.getPoolSize(), i);
Assert.assertEquals(executor.getQueue().size(), 0);
phaser.register();
futures.add(executor.submit(new WaitRunnable(phaser)));
phaser.arriveAndAwaitAdvance();
}
Assert.assertEquals(executor.getActiveCount(), MAX_THREADS);
Assert.assertEquals(executor.getPoolSize(), MAX_THREADS);
Assert.assertEquals(executor.getQueue().size(), 0);
futures.add(executor.submit(new WaitRunnable(phaser)));
Assert.assertEquals(executor.getActiveCount(), MAX_THREADS);
Assert.assertEquals(executor.getPoolSize(), MAX_THREADS);
Assert.assertEquals(executor.getQueue().size(), 1);
futures.add(executor.submit(new WaitRunnable(phaser)));
Assert.assertEquals(executor.getActiveCount(), MAX_THREADS);
Assert.assertEquals(executor.getPoolSize(), MAX_THREADS);
Assert.assertEquals(executor.getQueue().size(), 2);
phaser.arrive();
FutureTool.getAllVaried(futures);
Assert.assertEquals(executor.getActiveCount(), 0);
Assert.assertEquals(executor.getCompletedTaskCount(), MAX_THREADS + 2);
Assert.assertEquals(executor.getQueue().size(), 0);
executor.shutdownNow();
}
示例11: testShutdownNow
import java.util.concurrent.ThreadPoolExecutor; //导入方法依赖的package包/类
/**
* shutdownNow returns a list containing tasks that were not run,
* and those tasks are drained from the queue
*/
public void testShutdownNow() throws InterruptedException {
final int poolSize = 2;
final int count = 5;
final AtomicInteger ran = new AtomicInteger(0);
final ThreadPoolExecutor p =
new ThreadPoolExecutor(poolSize, poolSize,
LONG_DELAY_MS, MILLISECONDS,
new ArrayBlockingQueue<Runnable>(10));
final CountDownLatch threadsStarted = new CountDownLatch(poolSize);
Runnable waiter = new CheckedRunnable() { public void realRun() {
threadsStarted.countDown();
try {
MILLISECONDS.sleep(2 * LONG_DELAY_MS);
} catch (InterruptedException success) {}
ran.getAndIncrement();
}};
for (int i = 0; i < count; i++)
p.execute(waiter);
await(threadsStarted);
assertEquals(poolSize, p.getActiveCount());
assertEquals(0, p.getCompletedTaskCount());
final List<Runnable> queuedTasks;
try {
queuedTasks = p.shutdownNow();
} catch (SecurityException ok) {
return; // Allowed in case test doesn't have privs
}
assertTrue(p.isShutdown());
assertTrue(p.getQueue().isEmpty());
assertEquals(count - poolSize, queuedTasks.size());
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
assertTrue(p.isTerminated());
assertEquals(poolSize, ran.get());
assertEquals(poolSize, p.getCompletedTaskCount());
}
示例12: testShutdownNow
import java.util.concurrent.ThreadPoolExecutor; //导入方法依赖的package包/类
/**
* shutdownNow returns a list containing tasks that were not run,
* and those tasks are drained from the queue
*/
public void testShutdownNow() throws InterruptedException {
final int poolSize = 2;
final int count = 5;
final AtomicInteger ran = new AtomicInteger(0);
final ThreadPoolExecutor p =
new CustomTPE(poolSize, poolSize,
LONG_DELAY_MS, MILLISECONDS,
new ArrayBlockingQueue<Runnable>(10));
final CountDownLatch threadsStarted = new CountDownLatch(poolSize);
Runnable waiter = new CheckedRunnable() { public void realRun() {
threadsStarted.countDown();
try {
MILLISECONDS.sleep(2 * LONG_DELAY_MS);
} catch (InterruptedException success) {}
ran.getAndIncrement();
}};
for (int i = 0; i < count; i++)
p.execute(waiter);
await(threadsStarted);
assertEquals(poolSize, p.getActiveCount());
assertEquals(0, p.getCompletedTaskCount());
final List<Runnable> queuedTasks;
try {
queuedTasks = p.shutdownNow();
} catch (SecurityException ok) {
return; // Allowed in case test doesn't have privs
}
assertTrue(p.isShutdown());
assertTrue(p.getQueue().isEmpty());
assertEquals(count - poolSize, queuedTasks.size());
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
assertTrue(p.isTerminated());
assertEquals(poolSize, ran.get());
assertEquals(poolSize, p.getCompletedTaskCount());
}
示例13: createRegions
import java.util.concurrent.ThreadPoolExecutor; //导入方法依赖的package包/类
/**
* Create new set of regions on the specified file-system.
* NOTE: that you should add the regions to hbase:meta after this operation.
*
* @param conf {@link Configuration}
* @param rootDir Root directory for HBase instance
* @param tableDir table directory
* @param hTableDescriptor description of the table
* @param newRegions {@link HRegionInfo} that describes the regions to create
* @param task {@link RegionFillTask} custom code to populate region after creation
* @throws IOException
*/
public static List<HRegionInfo> createRegions(final Configuration conf, final Path rootDir,
final Path tableDir, final HTableDescriptor hTableDescriptor, final HRegionInfo[] newRegions,
final RegionFillTask task) throws IOException {
if (newRegions == null) return null;
int regionNumber = newRegions.length;
ThreadPoolExecutor exec = getRegionOpenAndInitThreadPool(conf,
"RegionOpenAndInitThread-" + hTableDescriptor.getTableName(), regionNumber);
try {
return createRegions(exec, conf, rootDir, tableDir, hTableDescriptor, newRegions, task);
} finally {
exec.shutdownNow();
}
}
示例14: waitFor
import java.util.concurrent.ThreadPoolExecutor; //导入方法依赖的package包/类
private void waitFor(ThreadPoolExecutor t, String name) {
boolean done = false;
while (!done) {
try {
done = t.awaitTermination(60, TimeUnit.SECONDS);
LOG.info("Waiting for " + name + " to finish...");
if (!done) {
t.shutdownNow();
}
} catch (InterruptedException ie) {
LOG.warn("Interrupted waiting for " + name + " to finish...");
}
}
}
示例15: test
import java.util.concurrent.ThreadPoolExecutor; //导入方法依赖的package包/类
@Test
public void test() {
int threads = 40;
ThreadPoolExecutor executor = new ThreadPoolExecutor(threads, threads, 1000, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>());
try {
int jobs = 1000;
int add = 1000;
latch = new CountDownLatch(jobs);
for (int i = 0; i < jobs; ++i) {
executor.execute(new IncReadDecValueAsync(0, add));
}
try {
latch.await();
} catch (InterruptedException e) {
throw new RuntimeException("Interrupted.", e);
}
log.info("Checking value, expecting 0 ...");
if (value != 0) {
testFailed("value == " + value + " != 0");
//log.warning("value == " + value + " != 0");
}
// if (value.get() != 0) {
// testFailed("value == " + value.get() + " != 0");
// }
if (failure) {
testFailed(errors.toString());
}
testOk();
} finally {
executor.shutdownNow();
}
}