Threads

  • Its a light weight process within a process, which executes a sequence of instructions concurrently in an execution context.

  • It requires fewer resources.

  • Its a component of a process, and a process has atleast one thread.

  • They share the process memory and resources to accomplish a task.

  • As they share resources, synchronization on read and write operations are important to avoid problems related to memory-inconsistencies, atomicity and visibility, thread-interference.

  • java.lang.Thread is the main class that help to create and control the thread while executing.

States

  • Threads has following states,

    • NEW : Not yet started

    • RUNNABLE : Scheduled for execution or started execution in JVM.

    • BLOCKED : When a thread is blocked by some other thread as it is waiting for monitor lock. This happens when a thread tries to enter a critical code section but is being blocked as other thread has monitor lock.

    • WAITING : Thread enters this state when waiting for monitor or awaiting other threads execution completion. Calling wait(), join() and park() will take a thread to this state.

    • TIMED_WAITING : This takes thread to waiting state for specified time. Calling wait(time), sleep(time), join(time), parkNanos() and parkUntil() will take a thread to this state.

    • TERMINATED : End of thread. By calling exit() or unhandled exception or interrupted by calling interrupt().

References

Last updated