use of java.util.concurrent.ScheduledExecutorService in project dropwizard by dropwizard.
the class LifecycleEnvironmentTest method scheduledExecutorServiceBuildsDaemonThreads.
@Test
public void scheduledExecutorServiceBuildsDaemonThreads() throws ExecutionException, InterruptedException {
final ScheduledExecutorService executorService = environment.scheduledExecutorService("daemon-%d", true).build();
final Future<Boolean> isDaemon = executorService.submit(() -> Thread.currentThread().isDaemon());
assertThat(isDaemon.get()).isTrue();
}
use of java.util.concurrent.ScheduledExecutorService in project UltimateAndroid by cymcsg.
the class IntentUtils method startPreviewActivity.
/**
* start screen capture after "delay" milliseconds, so the previous activity's
* state recover to normal state, such as button click, list item click,wait
* them to normal state so we can make a good screen capture
*
* @param context
* @param intent
* @param delay time in milliseconds
*/
public static void startPreviewActivity(final Context context, final Intent intent, long delay) {
final Handler mainThread = new Handler(Looper.getMainLooper());
final Runnable postAction = new Runnable() {
@Override
public void run() {
context.startActivity(intent);
}
};
/** process screen capture on background thread */
Runnable action = new Runnable() {
@Override
public void run() {
/**
* activity's root layout id, you can change the android.R.id.content to your root
* layout id
*/
final View contentView = ((Activity) context).findViewById(android.R.id.content);
ByteArrayOutputStream baos = null;
Bitmap bitmap = null;
try {
bitmap = Bitmap.createBitmap(contentView.getWidth(), contentView.getHeight(), Bitmap.Config.ARGB_8888);
contentView.draw(new Canvas(bitmap));
baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 70, baos);
intent.putExtra(KEY_PREVIEW_IMAGE, baos.toByteArray());
} finally {
try {
/** no need to close, actually do nothing */
if (null != baos)
baos.close();
} catch (IOException e) {
e.printStackTrace();
}
if (null != bitmap && !bitmap.isRecycled()) {
bitmap.recycle();
bitmap = null;
}
}
mainThread.post(postAction);
}
};
if (delay > 0) {
ScheduledExecutorService worker = Executors.newSingleThreadScheduledExecutor();
worker.schedule(action, delay, TimeUnit.MILLISECONDS);
} else {
action.run();
}
}
use of java.util.concurrent.ScheduledExecutorService in project zipkin by openzipkin.
the class ZooKeeperCollectorSampler method storeRateGroup.
static GroupMember storeRateGroup(CuratorFramework client, Builder builder, Closer closer, AtomicInteger spanCount, AtomicInteger storeRate) {
String storeRatePath = ensureExists(client, builder.basePath + "/storeRates");
GroupMember storeRateGroup = closer.register(new GroupMember(client, storeRatePath, builder.id));
log.debug("{} is to join the group {}", builder.id, storeRatePath);
storeRateGroup.start();
ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
closer.register(executor::shutdown);
ScheduledFuture<?> future = executor.scheduleAtFixedRate(() -> {
int oldValue = storeRate.get();
int newValue = (int) (1.0 * spanCount.getAndSet(0) * 60 / builder.updateFrequency);
log.debug("Store rates was: {} now {}", oldValue, newValue);
if (oldValue != newValue) {
storeRate.set(newValue);
storeRateGroup.setThisData(Integer.valueOf(newValue).toString().getBytes(UTF_8));
}
}, 0, builder.updateFrequency, TimeUnit.SECONDS);
closer.register(() -> future.cancel(true));
return storeRateGroup;
}
use of java.util.concurrent.ScheduledExecutorService in project vaadin-samples by xpoft.
the class MainView method attach.
@Override
public void attach() {
super.attach();
final UI ui = this.getUI();
final AtomicInteger pushCounts = new AtomicInteger(0);
final Runnable beeper = new Runnable() {
public void run() {
ui.access(new Runnable() {
public void run() {
Date date = java.util.Calendar.getInstance().getTime();
String text = date.toString() + ". Count: " + pushCounts.incrementAndGet();
System.out.println(text);
pushLabel.setValue(text);
}
});
}
};
final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
scheduler.scheduleAtFixedRate(beeper, 5, 5, TimeUnit.SECONDS);
}
use of java.util.concurrent.ScheduledExecutorService in project OpenAM by OpenRock.
the class ThreadMonitorTest method mockNoOpScheduledExecutor.
private ScheduledExecutorService mockNoOpScheduledExecutor() {
ScheduledExecutorService r = mock(ScheduledExecutorService.class);
given(r.scheduleAtFixedRate(any(Runnable.class), anyLong(), anyLong(), any(TimeUnit.class))).willReturn(mock(ScheduledFuture.class));
return r;
}
Aggregations