Search in sources :

Example 86 with ScheduledExecutorService

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();
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Test(org.junit.Test)

Example 87 with ScheduledExecutorService

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();
    }
}
Also used : Bitmap(android.graphics.Bitmap) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Canvas(android.graphics.Canvas) Handler(android.os.Handler) Activity(android.app.Activity) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) View(android.view.View)

Example 88 with ScheduledExecutorService

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;
}
Also used : GroupMember(org.apache.curator.framework.recipes.nodes.GroupMember) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService)

Example 89 with ScheduledExecutorService

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);
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) MyUI(ru.xpoft.vaadin.sample.session.MyUI) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Date(java.util.Date)

Example 90 with ScheduledExecutorService

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;
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) TimeUnit(java.util.concurrent.TimeUnit) ScheduledFuture(java.util.concurrent.ScheduledFuture)

Aggregations

ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)332 Test (org.junit.Test)99 ExecutorService (java.util.concurrent.ExecutorService)41 Test (org.testng.annotations.Test)35 CountDownLatch (java.util.concurrent.CountDownLatch)33 IOException (java.io.IOException)31 ArrayList (java.util.ArrayList)31 ScheduledThreadPoolExecutor (java.util.concurrent.ScheduledThreadPoolExecutor)31 HashMap (java.util.HashMap)30 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)28 Map (java.util.Map)26 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)25 List (java.util.List)19 ThreadFactory (java.util.concurrent.ThreadFactory)16 None (com.linkedin.common.util.None)15 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)15 UUID (java.util.UUID)15 DefaultStatisticsProvider (org.apache.jackrabbit.oak.stats.DefaultStatisticsProvider)15 URI (java.net.URI)14 CompletableFuture (java.util.concurrent.CompletableFuture)14