Refine search
CountDownLatch latch = new CountDownLatch(totalNumberOfTasks); ExecutorService taskExecutor = Executors.newFixedThreadPool(4); while(...) { taskExecutor.execute(new MyTask()); } try { latch.await(); } catch (InterruptedException E) { // handle }
private final ExecutorService executor = Executors.newCachedThreadPool(); private final Random random = new Random(); this.map.remove("key" + random.nextInt(MAP_SIZE)); this.map.put("key" + random.nextInt(MAP_SIZE), UUID.randomUUID().toString()); System.out.println(Thread.currentThread().getName() + ": " + i); Mutator m = new Mutator(this.map); executor.execute(a1); executor.execute(m); executor.execute(a2);
private static final Random rand = new Random(); private Queue<JLabel> labels = new LinkedList<JLabel>(); private JPanel panel = new JPanel(new GridLayout(0, 1)); public void actionPerformed(ActionEvent e) { startButton.setEnabled(false); CountDownLatch latch = new CountDownLatch(N); ExecutorService executor = Executors.newFixedThreadPool(N); for (JLabel label : labels) { label.setBackground(Color.white); executor.execute(new Counter(label, latch)); latch.await(); return null; int latency = rand.nextInt(42) + 10; for (int i = 1; i <= 100; i++) { publish(i); protected void done() { label.setBackground(Color.green); latch.countDown();
ExecutorService taskExecutor = Executors.newFixedThreadPool(4); while(...) { taskExecutor.execute(new MyTask()); } taskExecutor.shutdown(); try { taskExecutor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS); } catch (InterruptedException e) { ... }
import java.util.concurrent.*; class ThreadIdTest { public static void main(String[] args) { final int numThreads = 5; ExecutorService exec = Executors.newFixedThreadPool(numThreads); for (int i=0; i<10; i++) { exec.execute(new Runnable() { public void run() { long threadId = Thread.currentThread().getId(); System.out.println("I am thread " + threadId + " of " + numThreads); } }); } exec.shutdown(); } }
@Test public void testSendInAnotherThread() throws Exception { simpleChannel.send(new GenericMessage<String>("test")); Executor otherThreadExecutor = Executors.newSingleThreadExecutor(); final CountDownLatch latch = new CountDownLatch(1); otherThreadExecutor.execute(() -> { simpleChannel.send(new GenericMessage<String>("crap")); latch.countDown(); }); assertTrue(latch.await(10, TimeUnit.SECONDS)); assertEquals("test", simpleChannel.receive(10).getPayload()); // Message sent on another thread is not collected here assertEquals(null, simpleChannel.receive(10)); }
public void start(Stage primaryStage) { TableView<TestTask> table = new TableView<TestTask>(); Random rng = new Random(); for (int i = 0; i < 20; i++) { table.getItems().add( new TestTask(rng.nextInt(3000) + 2000, rng.nextInt(30) + 20)); primaryStage.show(); ExecutorService executor = Executors.newFixedThreadPool(table.getItems().size(), new ThreadFactory() { @Override public Thread newThread(Runnable r) { executor.execute(task);
final AtomicInteger counter = new AtomicInteger(); final AtomicInteger expected = new AtomicInteger(); final Random r = new Random(System.currentTimeMillis()); final ListUncaughtExceptionHandler eh = new ListUncaughtExceptionHandler(); final Executor executor = Executors.newSingleThreadExecutor(new ThreadFactory() { @Override public Thread newThread(Runnable r) { switch (r.nextInt(4)) { case 0: expected.incrementAndGet(); requests.add(newRequest(i + 1, counter, r.nextInt(20))); executor.execute(requests); break; case 1: executor.execute(requests); Thread.sleep(50);
private final Random random = new Random(); private final ExecutorService exec = Executors.newSingleThreadExecutor(); exec.shutdownNow(); exec.execute(swimTask); exec.execute(bikeTask); exec.execute(runTask); random.nextDouble() * (max - min) + min );
@Test public void testThreadOverload() throws Exception { final Store<ByteArray, byte[], byte[]> store = getStore(); int numOps = 100; final CountDownLatch latch = new CountDownLatch(numOps); Executor exec = Executors.newCachedThreadPool(); for(int i = 0; i < numOps; i++) { exec.execute(new Runnable() { public void run() { store.put(TestUtils.toByteArray(TestUtils.randomString("abcdefghijklmnopqrs", 10)), new Versioned<byte[]>(TestUtils.randomBytes(8)), null); latch.countDown(); } }); } latch.await(); }
ExecutorService es = Executors.newCachedThreadPool(); for(int i=0;i<5;i++) es.execute(new Runnable() { /* your task */ }); es.shutdown(); boolean finshed = es.awaitTermination(1, TimeUnit.MINUTES); // all tasks have finished or the time has been reached.
final SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd"); ExecutorService ex = Executors.newFixedThreadPool(1000); for (;;) { ex.execute(new Runnable() { public void run() { try { f.format(new Date(new Random().nextLong())); } catch (Exception e) { e.printStackTrace(); System.exit(1); } }; }); }
final OneThreadCache c = new OneThreadCache(); final ConcurrentCache cache = new ConcurrentCache(c); final Executor executor = Executors.newFixedThreadPool(20); final Random random = new Random(System.currentTimeMillis()); for (int i = 0; i < 100; i++) { executor.execute(new Runnable() { @Override public void run() {
ExecutorService e = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()); // Do work using something like either e.execute(new Runnable() { public void run() { // do one task } });
/* Get an executor service that will run a maximum of 5 threads at a time: */ ExecutorService exec = Executors.newFixedThreadPool(5); /* For all the 100 tasks to be done altogether... */ for (int i = 0; i < 100; i++) { /* ...execute the task to run concurrently as a runnable: */ exec.execute(new Runnable() { public void run() { /* do the work to be done in its own thread */ System.out.println("Running in: " + Thread.currentThread()); } }); } /* Tell the executor that after these 100 steps above, we will be done: */ exec.shutdown(); try { /* The tasks are now running concurrently. We wait until all work is done, * with a timeout of 50 seconds: */ boolean b = exec.awaitTermination(50, TimeUnit.SECONDS); /* If the execution timed out, false is returned: */ System.out.println("All done: " + b); } catch (InterruptedException e) { e.printStackTrace(); }
@Override public void onFailure(Call<T> call, final Throwable t) { callbackExecutor.execute(new Runnable() { @Override public void run() { callback.onFailure(ExecutorCallbackCall.this, t); } }); } });
ExecutorService exec = Executors.newFixedThreadPool(4, new ThreadFactory() { public Thread newThread(Runnable r) { Thread t = Executors.defaultThreadFactory().newThread(r); t.setDaemon(true); return t; } }); exec.execute(YourTaskNowWillBeDaemon);
int nThreads = 100; ExecutorService ex = Executors.newFixedThreadPool(nThreads); for (int i = 0; i < nThreads; i++) { ex.execute(new Walker()); } Thread.sleep(5000); ex.shutdownNow();