minte9
LearnRemember



Executor

ExecutorService allows you to run asynchron tasks without having to deal with threads directly.
 
/**
 * Creating a thread is an expensive operation ...
 * and it should be minimized. 
 * 
 * Having worker threads minimizes the overhead ...
 * because executor service has to create the thread pool only once.
 * 
 * In a fixed thread-pool, the executor service makes sure ...
 * that the pool always has the specified number of threads running.
 */

package com.minte9.threads.scheduler;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class Executor {
    public static void main(String[] args) {
         
        ExecutorService service = Executors.newFixedThreadPool(2);
            // fixed pool = 2 thread at the same time
        
        Runnable task1 = () -> { // loop
            while(true) {
                System.out.println(
                    "Task 1 / " + Thread.currentThread().getName()
                );
                sleep(2);
            }
        };
        Runnable task2 = () -> { // only once
            System.out.println(
                "Task 2 / " + Thread.currentThread().getName()
            );
            sleep(1);
        };
        Runnable task3 = () -> { // loop
            while(true) {
                System.out.println(
                    "Task 3 / " + Thread.currentThread().getName()
                );
                sleep(1);
            }
        };

        service.submit(task1);
        service.submit(task2);
        service.submit(task3);
        service.shutdown();

        /*
            Task 1 / pool-1-thread-1
            Task 2 / pool-1-thread-2 - Look Here
            Task 1 / pool-1-thread-1
            Task 3 / pool-1-thread-2
            Task 1 / pool-1-thread-1
            Task 3 / pool-1-thread-2
            ...
        */
    }

    public static void sleep(int seconds) {
        try {
            TimeUnit.SECONDS.sleep(seconds);
        } catch (InterruptedException ex) {}
    }
}

Scheduled

ScheduledExecutorService can run a task perioadically (and after an initial delay).
 
/**
 * ScheduledExecutorService can run a task perioadically ...
 * or after an initial delay. 
 */

package com.minte9.threads.scheduler;
import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class Scheduled {    
    public static void main(String[] args) 
                        throws InterruptedException {

        // Create a service with 1 thread
        ScheduledExecutorService service = 
            Executors.newScheduledThreadPool(1);

        // Schedule a task to run repeatedly
        service.scheduleAtFixedRate(() -> {
            System.out.println(new Date() + " - scheduled");
        }, 5L, 10L, TimeUnit.SECONDS); // 5s, 10s delay

        // Main thread
        while(true) {
            System.out.println(new Date() + " - main");
            TimeUnit.SECONDS.sleep(10); // 10S
        }    
        
        /*
            Tue Oct 05 17:54:13 EEST 2021 - main
            Tue Oct 05 17:54:18 EEST 2021 - scheduled
            Tue Oct 05 17:54:23 EEST 2021 - main
            Tue Oct 05 17:54:28 EEST 2021 - scheduled
            ...
        */
    }
}



  Last update: 381 days ago