Coder Social home page Coder Social logo

java-mod-5-thread-states-lifecycle's Introduction

Thread States and Lifecycle

Learning Goals

  • Explain the various thread states.
  • Explain a simplified thread lifecycle.

Introduction

A thread goes through different states from its creation to termination. These state changes can be caused by the programmer’s code or by the operating system’s events. We’ll first take a look at the different states a thread can be in and then look at a simplified lifecycle.

Thread States

Thread states are represented by the Thread.State enum and a current thread’s state can be examined using the getState instance method on a thread.

There are six possible values of thread state:

  • NEW: a new thread instance has just been created but not started yet.
  • RUNNABLE: the thread is being run by the JVM but it’s waiting for OS resources.
  • BLOCKED: a thread is said to be blocked if it’s waiting for a monitor lock (discussed later).
  • WAITING: the thread is waiting for another thread indefinitely (join without a timeout).
  • TIMED_WAITING: the thread is waiting for another thread for the specified amount of time (Thread.sleep, join with a timeout defined).
  • TERMINATED: a thread is said to have terminated when the run method has finished execution or an uncaught exception is thrown. Once terminated, a thread cannot go back to a runnable state.

Let’s look at some of these states in an example.

class Example {
    public static void main(String[] args) throws InterruptedException {
        Thread t = new Thread(() -> {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                System.out.println(e.getMessage());
            }
            System.out.println("hi from the thread!");
        });

        System.out.println(t.getState());
        t.start();
        System.out.println(t.getState());
        System.out.println(t.getState());
        System.out.println(t.getState());
        t.join();
        System.out.println(t.getState());
    }
}
NEW
RUNNABLE
RUNNABLE
TIMED_WAITING
hi from the thread!
TERMINATED

Thread Lifecycle

The states in the Thread.Stateare from the JVM’s point of view. The actual states are more complex. For example, the RUNNABLE state can describe a thread that is ready to run or is already running. Here’s a simplified diagram of a thread lifecycle:

Thread lifecycle

When a thread is initialized, it is in the NEW state. When the thread is started it goes into the RUNNING state waiting for the thread scheduler to allocate it resources for executing its instructions. The thread switches between a “ready to run” state and a “running’.

The waiting state means that a thread’s execution is paused while it waits for another thread or an I/O operation. A thread must be moved from this state to a “ready to run” state to continue execution.

Conclusion

We’ve learned about the various states of a thread and their lifecycle. As we learn how to synchronize data and create safe, concurrent programs, this idea about different thread states will be helpful in understanding more advanced concepts.

java-mod-5-thread-states-lifecycle's People

Contributors

alveem avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.