Java geek

Related reading:

Java Concurrent programming (1) Knowledge map Java concurrent programming (2) Atomic Java concurrent programming (3) Visibility Java concurrent programming (4) Sequential Java concurrent programming (5) Introduction to Creating threads Java concurrent programming (6) Synchronized usage Introduction to Concurrent Programming in Java (7) Easy to understand wait and Notify and application scenarios Introduction to Concurrent programming in Java (9) Introduction to Concurrent programming in Java with deadlocks and deadlocks (10) Introduction to Concurrent programming in Java with lock optimization (11) Flow limiting scenarios and Spring flow limiter implementation Introduction to Concurrent Programming in Java (12) Producer and Consumer pattern – Introduction to concurrent programming in Java (13) Read/write lock and cache template (14) CountDownLatch Application Scenario Java Concurrent programming (15) CyclicBarrier application scenario Introduction to Concurrent programming in Java (16) seconds to understand the difference between the thread pool Java Concurrent programming introduction (17) one picture master common classes and interfaces for threads Java concurrent programming introduction (18) Again on thread safety Java concurrent programming introduction (19) Asynchronous task scheduling tool CompleteFeature Java Concurrent programming introduction (20) Common locking scenarios and locking tools


Thread lifecycle 1

The thread lifecycle is as follows:


The verification code is as follows:

public class LifeCycleDemo {

    public static void main(String[] args) {
        Thread t0 = new Thread(new Runnable() {
            public void run(a) { Counter.increase(); }}); Thread t1 =new Thread(new Runnable() {
            public void run(a) { Counter.increase(); }});// The state is NEW after creation
        printState(t0);
        printState(t1);

        t0.start();
        t1.start();

        // The startup state is RUNNABLE
        printState(t0);
        printState(t1);

        sleep(1000);

        Sleep state changes to TIMED_WAITING, t1 waits to obtain the lock, and its state is BLOCKED
        printState(t0);
        printState(t1);

        sleep(4000);

        // The state of the thread is TERMINATED
        printState(t0);
        printState(t1);

        System.out.println("count: " + Counter.getCount());
    }

    private static void printState(Thread t) {
        System.out.println(t.getName() + "" + t.getState());
    }

    private static void sleep(long millis) {
        try {
            Thread.sleep(millis);
        } catch(InterruptedException e) { e.printStackTrace(); }}}class Counter {
    private static int count;

    public synchronized static void increase(a) {
        count = count +1;

        try {
            // Simulate concurrent contention
            Thread.sleep(2000);
        } catch(InterruptedException ie) { ie.printStackTrace(); }}public static int getCount(a) {
        returncount; }}Copy the code

Print logs:

Thread-0 NEW
Thread-1 NEW
Thread-0 RUNNABLE
Thread-1 RUNNABLE
Thread-0 BLOCKED
Thread-1 TIMED_WAITING
Thread-0 TERMINATED
Thread-1 TERMINATED
count: 2
Copy the code

Thread life cycle 2

The verification code is as follows:

public class LifeCycleDemo2 {
    public static void main(String[] args) {
        Thread t0 = new Thread(new ChildThread(Thread.currentThread()));

        t0.start();
        // The state of the child thread is RUNNABLE after it starts
        printState(t0);

        try {
            // The main thread is in WAITING state after the child thread joins
            t0.join();

            // The main thread status is RUNNABLE
            printState(Thread.currentThread());

            // The child thread is TERMINATED and in state TERMINATED
            printState(t0);

        } catch(InterruptedException e) { e.printStackTrace(); }}private static void printState(Thread t) {
        System.out.println(t.getName() + ""+ t.getState()); }}class ChildThread implements Runnable {

    Thread parentThread;

    public ChildThread(Thread parentThread) {
        this.parentThread = parentThread;
    }
    public void run(a) {
        try {
            Thread.sleep(2000);
            // This is printed after the child thread joins, and the main thread is WAITING
            System.out.println(parentThread.getName() + "" + parentThread.getState());
        } catch(InterruptedException e) { e.printStackTrace(); }}}Copy the code

Output log:

Thread-0 RUNNABLE
main WAITING
main RUNNABLE
Thread-0 TERMINATED
Copy the code

Other scenarios do not list the code separately.

Thread life cycle 3


end.


<– Read the mark, left like!