Skip to content

Java Examples --- Threads, Executor Framework,Fork Join Framework,CompletableFuture

Notifications You must be signed in to change notification settings

serdaralkancode/java-thread-vs-executor

Repository files navigation

java-thread-vs-executor

JDK VERSION : JDK 11 - AMAZON CORRETTO


Examples

  • Thread

    Thread class and Runnable Interface

    Thread sleep & join & priority

    Thread Synchronization

    Thread Deadlock

    Atomic

    Thread wait & notify

    CountDownLatch

    CyclicBarrier

    example code

  • Executor Thread Pooling

    Thread Pooling

    Single Thread Pool

    Fixed Thread Pool

    Cached Thread Pool

    Custom Thread Pool

    BlockingQueue & RejectedExecutionHandler & ThreadFactory & Core and Max Pool size & KeepAlive

    example code

  • Callable & Future

    Callable & Runnable Object

    Future Object

    example code

  • CompletableFuture

    runAsync & supplyAsync method

    thenApply & thenAccept & thenRun & thenCompose

    join & get

    allOf & anyOf

    CompletableFuture with Executor

    Exception with exceptionally method

    Exception with handle method

    example code

  • Fork/Join Framework

    RecursiveAction

    RecursiveTask

    example code

    Resources : Rules of ThreadPoolExecutor pool size

Here are Sun's rules for thread creation in simple terms:

If the number of threads is less than the corePoolSize, create a new Thread to run a new task.

If the number of threads is equal (or greater than) the corePoolSize, put the task into the queue.

If the queue is full, and the number of threads is less than the maxPoolSize, create a new thread to run tasks in.

If the queue is full, and the number of threads is greater than or equal to maxPoolSize, reject the task.

The long and the short of it is that new threads are only created when the queue fills up, so if you're using an unbounded queue then the number of threads will not exceed corePoolSize.