/java-threads-examples

Contains more than 20 simple examples with explanations using Thread API and Executors Framework.

Primary LanguageJava

Java Thread Simple Examples

This repository contains more than 20 simple examples with explanations on how to use Java Thread API and Executors Framework. Currently, the following subjects are covered:

  • Running tasks from Thread and Runnable
  • Strategies for returning values from Threads and Runnable tasks
  • wait(), notify(), interrupt(), join(), isAlive()...
  • Creating Thread Pools (FixedThreadPool, CachedThreadPool, SingleThreadPool, ScheduledThreadPool...)
  • Submiting tasks to ExecutorService
  • Returning values from tasks using Callable and Future
  • Naming threads and creating deamon threads in a thread pool implementing ThreadFactory interface
  • Terminating tasks using Thread API
  • Terminating tasks using Future
  • Handling exceptions in normal threads implementing UncaughtExceptionHandler interface
  • Handling exceptions in threads on a thread pool implementing UncaughtExceptionHandler interface
  • Managing tasks termination using CountDownLatch
  • Running scheduled tasks using Timer and TimerTask
  • Running scheduled tasks using ScheduledThreadPool

Of course there are much more than this to cover (such as Fork/Join Framework). Feel free to fork this project and contribute with more simple thread examples!