Example usage for java.util.concurrent ExecutorService execute

List of usage examples for java.util.concurrent ExecutorService execute

Introduction

In this page you can find the example usage for java.util.concurrent ExecutorService execute.

Prototype

void execute(Runnable command);

Source Link

Document

Executes the given command at some time in the future.

Usage

From source file:Main.java

public static void main(String[] args) {
    Runnable badTask = () -> {
        throw new RuntimeException("Throwing exception  from  task execution...");
    };/*from w  w w  .  ja  va2 s  .c  o  m*/

    ExecutorService exec = Executors.newSingleThreadExecutor();
    exec.execute(badTask);
    exec.shutdown();
}

From source file:SimpExec.java

public static void main(String args[]) {
    CountDownLatch cdl = new CountDownLatch(5);
    CountDownLatch cdl2 = new CountDownLatch(5);
    CountDownLatch cdl3 = new CountDownLatch(5);
    CountDownLatch cdl4 = new CountDownLatch(5);
    ExecutorService es = Executors.newFixedThreadPool(2);

    es.execute(new MyThread(cdl, "A"));
    es.execute(new MyThread(cdl2, "B"));
    es.execute(new MyThread(cdl3, "C"));
    es.execute(new MyThread(cdl4, "D"));

    try {//from w w w  . j av a 2s . c o  m
        cdl.await();
        cdl2.await();
        cdl3.await();
        cdl4.await();
    } catch (InterruptedException exc) {
        System.out.println(exc);
    }

    es.shutdown();
}

From source file:Main.java

public static void main(String[] args) {
    PrintTask task1 = new PrintTask("thread1");
    PrintTask task2 = new PrintTask("thread2");
    PrintTask task3 = new PrintTask("thread3");

    System.out.println("Starting threads");

    ExecutorService threadExecutor = Executors.newCachedThreadPool();

    threadExecutor.execute(task1); // start task1
    threadExecutor.execute(task2); // start task2
    threadExecutor.execute(task3); // start task3

    threadExecutor.shutdown(); // shutdown worker threads

    System.out.println("Threads started, main ends\n");
}

From source file:Main.java

public static void main(String[] args) {
    ExecutorService pool = Executors.newCachedThreadPool();
    Main app = new Main();
    for (int i = 0; i < 1000; i++) {
        pool.execute(app.new Adding());
    }//from w  w w  .jav a 2 s  .c o  m
    pool.shutdown();
    while (!pool.isTerminated()) {
        System.out.println(" Is it done? : " + pool.isTerminated());
    }
    System.out.println(" Is it done? : " + pool.isTerminated());
    System.out.println("Sum is " + app.sum);
}

From source file:MainClass.java

public static void main(String[] args) {
    PrintTask task1 = new PrintTask("thread1");
    PrintTask task2 = new PrintTask("thread2");
    PrintTask task3 = new PrintTask("thread3");

    System.out.println("Starting threads");

    ExecutorService threadExecutor = Executors.newCachedThreadPool();

    // start threads and place in runnable state
    threadExecutor.execute(task1); // start task1
    threadExecutor.execute(task2); // start task2
    threadExecutor.execute(task3); // start task3

    threadExecutor.shutdown(); // shutdown worker threads

    System.out.println("Threads started, main ends\n");
}

From source file:koper.demo.performance.SendDataEventMsgPerf.java

/**
 * DataEventMsg ?/*  w w w .j av  a2 s.  c o  m*/
 * @param args ?:???
 *             ?:?????? sleep 
 */
public static void main(String[] args) throws Exception {
    ApplicationContext context = new ClassPathXmlApplicationContext("kafka/context-data-message.xml",
            "kafka/context-data-producer.xml");

    Order order = new Order();
    order.setId(100);
    order.setOrderNo("order_no");
    order.setCreatedTime("oroder_created_time");

    OrderService orderService = (OrderService) context.getBean("orderServiceImpl");

    int threadNum = 1;
    long sleepMs = 1000L;
    if (args.length >= 1) {
        threadNum = Integer.parseInt(args[0]);
        sleepMs = Long.parseLong(args[1]);
    }
    final long finalSleepMs = sleepMs;
    ExecutorService executorService = Executors.newFixedThreadPool(threadNum);
    for (int i = 0; i < threadNum; ++i) {
        executorService.execute(() -> {
            while (true) {
                orderService.insertOrder(order);
                try {
                    Thread.sleep(finalSleepMs);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
}

From source file:Main.java

public static void main(String[] args) {
    Map<String, String> map = new ConcurrentHashMap<String, String>();
    for (int i = 0; i < MAP_SIZE; i++) {
        map.put("key:" + i, UUID.randomUUID().toString());
    }/*from  w w w  .  ja  va2 s  . c om*/
    ExecutorService executor = Executors.newCachedThreadPool();
    Accessor a1 = new Accessor(map);
    Accessor a2 = new Accessor(map);
    Mutator m = new Mutator(map);
    executor.execute(a1);
    executor.execute(m);
    executor.execute(a2);
}

From source file:Main.java

public static void main(String[] args) throws IOException {
    ExecutorService service = Executors.newFixedThreadPool(4);
    BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in));
    String line;/*  w ww .ja  v  a 2s . c  o  m*/
    while ((line = buffer.readLine()) != null) {
        service.execute(new Worker(line));
    }
}

From source file:se.symsoft.codecamp.SmsTerminatorService.java

public static void main(String[] args) throws IOException, InterruptedException {

    ExecutorService executor = Executors.newFixedThreadPool(1);
    SmsTerminatorService service = new SmsTerminatorService();
    executor.execute(() -> {
        try {//w  w  w.  j a  v a 2 s  .  co  m
            service.start();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    });

}

From source file:com.puzzle.module.send.sign.SignXml.java

public static void main(String[] args) throws Exception {
    ClientApi api = ClientApi.getSingletonClientApi();
    String xml = FileUtils.readFileToString(new File("D:\\work\\myeclipse2014\\workspace\\Ukey\\sign.xml"),
            "UTF-8");
    //            
    ExecutorService s = Executors.newFixedThreadPool(3);

    for (int i = 0; i < 1; i++) {
        s.execute(new signTest(xml));
    }/*w w w.ja  v  a2  s . c o m*/

    //        String signData = Base64.encodeBase64String(signedData);
    //        FileUtils.writeStringToFile(new File("D:\\sign.txt"), signData, "UTF-8");
    //        System.out.println(signData);
}