The Thread and The Executor Framework

  • New — Whenever a new thread is created, it is in this state. The execution of this thread is not yet started.
  • Runnable — In this state, a thread has either started its execution or it is ready to be executed. The execution of a thread is handled by the thread scheduler
  • Blocked or Waiting — A thread moves into this state when it is waiting for some operation to complete. For example — I/O Operation or trying to access a section which is currently locked by some other thread
  • Terminated — A thread moves into this state when execution gets over normally or abnormally.
  • Extending Thread class
  • Implementing Runnable interface
  • Executor — Unlike thread class which combines both task (run method) and its execution (start method), the Executor is simply a core interface that is used to specify the task.
  • ExecutorService — It is an extension of the Executor interface and provides a facility for returning a Future object and terminate or shut down the thread pool. Once the shut down is called on the thread pool, it will not accept any new task but it will complete any pending task.
  • Executors — It is a utility class that provides static methods to create different types of thread pools. The object returned by any of the static methods is the implementation of the ExecutorService interface.
  • newFixedThreadPool — It creates a fixed thread pool. The number of threads will depend on the integer value you provide to this method.
  • newCachedThreadPool — It creates a new thread if none available otherwise it reuses the available ones from the thread pool.
  • newScheduledThreadPool — Like newFixedThreadPool, it takes an integer value to create that many numbers of threads in a thread pool. Additional to that it allows you to execute a scheduled task.
  • newSingleThreadExecutor — It consists of a single thread to execute a task.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store