本文整理汇总了Java中java.util.concurrent.Executors.defaultThreadFactory方法的典型用法代码示例。如果您正苦于以下问题:Java Executors.defaultThreadFactory方法的具体用法?Java Executors.defaultThreadFactory怎么用?Java Executors.defaultThreadFactory使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.concurrent.Executors
的用法示例。
在下文中一共展示了Executors.defaultThreadFactory方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testShutdownDelayedTasks
import java.util.concurrent.Executors; //导入方法依赖的package包/类
@Test
public void testShutdownDelayedTasks() throws InterruptedException {
ex = new ScheduledThreadPoolExecutorWithKeepAlive(50, 1, TimeUnit.SECONDS,
Executors.defaultThreadFactory());
ex.setExecuteExistingDelayedTasksAfterShutdownPolicy(false);
ex.schedule(new Runnable() {
public void run() {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
fail("interrupted");
}
}
}, 5000, TimeUnit.MILLISECONDS);
ex.shutdown();
long start = System.nanoTime();
assertTrue(ex.awaitTermination(30, TimeUnit.SECONDS));
long elapsed = System.nanoTime() - start;
assertTrue("Shutdown should not have waited. Waited " + TimeUnit.NANOSECONDS.toMillis(elapsed),
TimeUnit.SECONDS.toNanos(2) > elapsed);
}
示例2: testRepeatedExecution
import java.util.concurrent.Executors; //导入方法依赖的package包/类
@Test
public void testRepeatedExecution() throws InterruptedException {
ex = new ScheduledThreadPoolExecutorWithKeepAlive(50, 1, TimeUnit.SECONDS,
Executors.defaultThreadFactory());
final AtomicInteger counter = new AtomicInteger();
Runnable run = new Runnable() {
public void run() {
counter.incrementAndGet();
}
};
ScheduledFuture f = ex.scheduleAtFixedRate(run, 0, 1, TimeUnit.SECONDS);
Awaitility.await().atMost(30, TimeUnit.SECONDS)
.until(() -> assertEquals("Task was not executed repeatedly", true, counter.get() > 1));
Awaitility.await().atMost(30, TimeUnit.SECONDS)
.until(() -> assertEquals("The task could not be cancelled", true, f.cancel(true)));
Awaitility.await().atMost(30, TimeUnit.SECONDS)
.until(() -> assertEquals("Task was not cancelled within 30 sec", true, f.isCancelled()));
int oldValue = counter.get();
Thread.sleep(5000);
assertEquals("Task was not cancelled", oldValue, counter.get());
}
示例3: getExecutor
import java.util.concurrent.Executors; //导入方法依赖的package包/类
public XExecutor getExecutor() {
if (executor == null) {
synchronized (UploadThreadPool.class) {
if (executor == null) {
executor = new XExecutor(corePoolSize, MAX_IMUM_POOL_SIZE, KEEP_ALIVE_TIME, UNIT, //
new PriorityBlockingQueue<Runnable>(), //无限容量的缓冲队列
Executors.defaultThreadFactory(), //线程创建工厂
new ThreadPoolExecutor.AbortPolicy()); //继续超出上限的策略,阻止
}
}
}
return executor;
}
示例4: create
import java.util.concurrent.Executors; //导入方法依赖的package包/类
public ThreadPoolExecutor create() {
// RejectedExecutionHandler 拒绝策略
ThreadFactory threadFactory = Executors.defaultThreadFactory();
/*
* 参数1: 设置核心池大小
* 参数2: 设置线程池最大能接受多少线程
* 参数3: 当前线程数大于核心池大小、小于最大能接受线程时,超出核心池大小的线程数的生命周期
* 参数4: 设置生命周期时间单位,秒
* 参数5: 设置线程池缓存队列的排队策略为FIFO,并且指定缓存队列大小为2
* 参数6: 线程工厂
*/
return new ThreadPoolExecutor(2, 5, 10, TimeUnit.SECONDS,
new ArrayBlockingQueue<Runnable>(2), threadFactory);
}
示例5: TreadPool
import java.util.concurrent.Executors; //导入方法依赖的package包/类
private TreadPool(int corePoolSize, int maximumPoolSize, long keepAliveTime) {
exec = new ThreadPoolExecutor(
corePoolSize,// 核心线程数
maximumPoolSize,// 最大线程数
keepAliveTime,//闲置线程存活时间
TimeUnit.SECONDS, // 时间单位
new LinkedBlockingDeque<Runnable>(), // 线程队列
Executors.defaultThreadFactory(), // 线程工厂
new ThreadPoolExecutor.AbortPolicy() // 队列已满,而且当前线程数已经超过最大线程数时的异常处理策略
);
scheduleExec = Executors.newScheduledThreadPool(corePoolSize);
}
示例6: testConcurrentExecutionAndExpiration
import java.util.concurrent.Executors; //导入方法依赖的package包/类
@Category(FlakyTest.class) // GEODE-1138: time sensitive, thread sleeps, expiration
@Test
public void testConcurrentExecutionAndExpiration()
throws InterruptedException, ExecutionException {
ex = new ScheduledThreadPoolExecutorWithKeepAlive(50, 1, TimeUnit.SECONDS,
Executors.defaultThreadFactory());
Runnable waitForABit = new Runnable() {
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
fail("interrupted");
}
}
};
Future[] futures = new Future[50];
for (int i = 0; i < 50; i++) {
futures[i] = ex.submit(waitForABit);
}
long start = System.nanoTime();
for (int i = 0; i < 50; i++) {
futures[i].get();
}
long end = System.nanoTime();
assertTrue("Tasks executed in parallel", TimeUnit.NANOSECONDS.toSeconds(end - start) < 50);
assertEquals(50, ex.getLargestPoolSize());
// now make sure we expire back down.
Thread.sleep(5000);
assertEquals(1, ex.getPoolSize());
}
示例7: createPool
import java.util.concurrent.Executors; //导入方法依赖的package包/类
protected synchronized static void createPool() {
BlockingQueue<Runnable> workers = new LinkedBlockingQueue<>();
RejectedExecutionHandler handler = new ThreadPoolExecutor.AbortPolicy();
mDownloadPool = new DownloadPool(
corePoolSize,
maxPoolSize,
keepAliveTime,
TimeUnit.MILLISECONDS,
workers,
Executors.defaultThreadFactory(),
handler);
}
示例8: NewRelicMeterRegistry
import java.util.concurrent.Executors; //导入方法依赖的package包/类
public NewRelicMeterRegistry(NewRelicConfig config, Clock clock) {
this(config, clock, Executors.defaultThreadFactory());
}
示例9: QueuesExecutorService
import java.util.concurrent.Executors; //导入方法依赖的package包/类
protected QueuesExecutorService(int corePoolSize, int maximumPoolSize,
Queue<Runnable> bossQueue,IndexQueueGroupManager indexQM,KeyQueueGroupManager keyQM,WaitConditionStrategy waitConditionStrategy) {
this(corePoolSize, maximumPoolSize, DEFAULT_KEEP_ALIVE_SEC, TimeUnit.SECONDS,
Executors.defaultThreadFactory(),waitConditionStrategy,
bossQueue,indexQM,keyQM,new DirectExecutor());
}
示例10: SingleThreadTaskQueueExecutor_CountDownLatch
import java.util.concurrent.Executors; //导入方法依赖的package包/类
public SingleThreadTaskQueueExecutor_CountDownLatch(String queueName) {
this(queueName,Executors.defaultThreadFactory());
}
示例11: DefaultQueueGroupExecutor
import java.util.concurrent.Executors; //导入方法依赖的package包/类
protected DefaultQueueGroupExecutor(int corePoolSize, int maximumPoolSize,
Queue<Runnable> bossQueue,IndexQueueGroupManager indexQM,KeyQueueGroupManager keyQM) {
this(corePoolSize, maximumPoolSize, DEFAULT_KEEP_ALIVE_SEC, TimeUnit.SECONDS,
Executors.defaultThreadFactory(),DEFAULT_waitConditionStrategy,
bossQueue,indexQM,keyQM,new DirectExecutor());
}
示例12: main
import java.util.concurrent.Executors; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {
if (args.length < 1) {
System.err.println("Missing required project argument");
System.err.printf(
"Usage: java %s gcp-project-id [verbose]\n", SheepCounterExample.class.getName());
return;
}
String project = args[0];
// Turn up the logging verbosity
if (args.length > 1) {
Logger log = LogManager.getLogManager().getLogger("");
log.setLevel(Level.ALL);
for (Handler h : log.getHandlers()) {
h.setLevel(Level.ALL);
}
}
// Create a sample resource. In this case, a GCE Instance.
// See https://cloud.google.com/monitoring/api/resources for a list of resource types.
MonitoredResource monitoredResource =
new MonitoredResource()
.setType("gce_instance")
.setLabels(
ImmutableMap.of(
"instance_id", "test-instance",
"zone", "us-central1-f"));
// Set up the Metrics infrastructure.
MetricWriter stackdriverWriter =
new StackdriverWriter(
createAuthorizedMonitoringClient(),
project,
monitoredResource,
STACKDRIVER_MAX_QPS,
STACKDRIVER_MAX_POINTS_PER_REQUEST);
final MetricReporter reporter =
new MetricReporter(
stackdriverWriter, METRICS_REPORTING_INTERVAL, Executors.defaultThreadFactory());
reporter.startAsync().awaitRunning();
// Set up a handler to stop sleeping on SIGINT.
Runtime.getRuntime()
.addShutdownHook(
new Thread(
() -> {
reporter.stopAsync().awaitTerminated();
// Allow the LogManager to cleanup the loggers.
DelayedShutdownLogManager.resetFinally();
}));
System.err.println("Send SIGINT (Ctrl+C) to stop sleeping.");
while (true) {
// Count some Googley sheep.
int colorIndex = new Random().nextInt(SHEEP_COLORS.size());
int speciesIndex = new Random().nextInt(SHEEP_SPECIES.size());
sheepCounter.incrementBy(1, SHEEP_COLORS.get(colorIndex), SHEEP_SPECIES.get(speciesIndex));
sheepFluffiness.record(
new Random().nextDouble() * 200,
SHEEP_COLORS.get(colorIndex),
SHEEP_SPECIES.get(speciesIndex));
isSleeping.set(true);
logger.info("zzz...");
Thread.sleep(5000);
}
}
示例13: AbstractTaskQueuesExecutor
import java.util.concurrent.Executors; //导入方法依赖的package包/类
public AbstractTaskQueuesExecutor() {
this(Executors.defaultThreadFactory());
}
示例14: ThreadPoolTaskQueuesExecutor
import java.util.concurrent.Executors; //导入方法依赖的package包/类
public ThreadPoolTaskQueuesExecutor(int corePoolSize, int maximumPoolSize,
WaitConditionStrategy waitConditionStrategy) {
this(corePoolSize, maximumPoolSize, DEFAULT_KEEP_ALIVE, TimeUnit.SECONDS, Executors.defaultThreadFactory(),waitConditionStrategy);
}
示例15: build
import java.util.concurrent.Executors; //导入方法依赖的package包/类
private static ThreadFactory build(ThreadFactoryBuilder builder) {
final String nameFormat = builder.nameFormat;
final Boolean daemon = builder.daemon;
final Integer priority = builder.priority;
final UncaughtExceptionHandler uncaughtExceptionHandler = builder.uncaughtExceptionHandler;
final ThreadFactory backingThreadFactory =
(builder.backingThreadFactory != null)
? builder.backingThreadFactory
: Executors.defaultThreadFactory();
final AtomicLong count = (nameFormat != null) ? new AtomicLong(0) : null;
return new ThreadFactory() {
@Override
public Thread newThread(Runnable runnable) {
Thread thread = backingThreadFactory.newThread(runnable);
if (nameFormat != null) {
thread.setName(format(nameFormat, count.getAndIncrement()));
}
if (daemon != null) {
thread.setDaemon(daemon);
}
if (priority != null) {
thread.setPriority(priority);
}
if (uncaughtExceptionHandler != null) {
thread.setUncaughtExceptionHandler(uncaughtExceptionHandler);
}
return thread;
}
};
}