美文网首页JUC全家桶
JUC全家桶 | Java线程的两种创建方式和六种状态

JUC全家桶 | Java线程的两种创建方式和六种状态

作者: 码农StayUp | 来源:发表于2020-10-09 14:09 被阅读0次

本文源码:Gitee·点这里

线程的两种创建方式

1. 通过继承Thread类创建线程

将ThreadDemo1类继承Thread类,并重写run方法。
通过ThreadDemo1类实例调用start()方法,将会创建一个线程并启动它。

public class ThreadDemo1 extends Thread {

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName());
    }

    public static void main(String[] args) {

        new ThreadDemo1().start();

        ThreadDemo1 thread = new ThreadDemo1();
        thread.setName("myThread-1"); // 通过Thread类提供方法设置线程名称
        thread.start();

    }
}

2. 通过实现Runnable接口创建线程

将ThreadDemo2类实现Runnable接口,并实现run()方法。
通过ThreadDemo2类实例调用start()方法,将会创建一个线程并启动它。

public class ThreadDemo2 implements Runnable {
    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName());
    }

    public static void main(String[] args) {
        ThreadDemo2 threadDemo2 = new ThreadDemo2();
        new Thread(threadDemo2).start();
        new Thread(threadDemo2, "myThread").start();
    }
}

Runnable是一个函数式接口,可能通过Lambda表达式来创建线程,无需创建类

public class ThreadDemo3 {

    public static void main(String[] args) {
        new Thread(() -> System.out.println(Thread.currentThread().getName())).start();
        new Thread(() -> System.out.println(Thread.currentThread().getName()), "myThread").start();
    }
}

线程的六种状态

线程有哪几种状态呢?我们从源码中查找,可以看出线程有六种状态。Thread类中State枚举类源码:

   /**
     * A thread state.  A thread can be in one of the following states:
     * <ul>
     * <li>{@link #NEW}<br>
     *     A thread that has not yet started is in this state.
     *     </li>
     * <li>{@link #RUNNABLE}<br>
     *     A thread executing in the Java virtual machine is in this state.
     *     </li>
     * <li>{@link #BLOCKED}<br>
     *     A thread that is blocked waiting for a monitor lock
     *     is in this state.
     *     </li>
     * <li>{@link #WAITING}<br>
     *     A thread that is waiting indefinitely for another thread to
     *     perform a particular action is in this state.
     *     </li>
     * <li>{@link #TIMED_WAITING}<br>
     *     A thread that is waiting for another thread to perform an action
     *     for up to a specified waiting time is in this state.
     *     </li>
     * <li>{@link #TERMINATED}<br>
     *     A thread that has exited is in this state.
     *     </li>
     * </ul>
     *
     * <p>
     * A thread can be in only one state at a given point in time.
     * These states are virtual machine states which do not reflect
     * any operating system thread states.
     *
     * @since   1.5
     * @see #getState
     */
    public enum State {
        /**
         * Thread state for a thread which has not yet started.
         */
        NEW,

        /**
         * Thread state for a runnable thread.  A thread in the runnable
         * state is executing in the Java virtual machine but it may
         * be waiting for other resources from the operating system
         * such as processor.
         */
        RUNNABLE,

        /**
         * Thread state for a thread blocked waiting for a monitor lock.
         * A thread in the blocked state is waiting for a monitor lock
         * to enter a synchronized block/method or
         * reenter a synchronized block/method after calling
         * {@link Object#wait() Object.wait}.
         */
        BLOCKED,

        /**
         * Thread state for a waiting thread.
         * A thread is in the waiting state due to calling one of the
         * following methods:
         * <ul>
         *   <li>{@link Object#wait() Object.wait} with no timeout</li>
         *   <li>{@link #join() Thread.join} with no timeout</li>
         *   <li>{@link LockSupport#park() LockSupport.park}</li>
         * </ul>
         *
         * <p>A thread in the waiting state is waiting for another thread to
         * perform a particular action.
         *
         * For example, a thread that has called <tt>Object.wait()</tt>
         * on an object is waiting for another thread to call
         * <tt>Object.notify()</tt> or <tt>Object.notifyAll()</tt> on
         * that object. A thread that has called <tt>Thread.join()</tt>
         * is waiting for a specified thread to terminate.
         */
        WAITING,

        /**
         * Thread state for a waiting thread with a specified waiting time.
         * A thread is in the timed waiting state due to calling one of
         * the following methods with a specified positive waiting time:
         * <ul>
         *   <li>{@link #sleep Thread.sleep}</li>
         *   <li>{@link Object#wait(long) Object.wait} with timeout</li>
         *   <li>{@link #join(long) Thread.join} with timeout</li>
         *   <li>{@link LockSupport#parkNanos LockSupport.parkNanos}</li>
         *   <li>{@link LockSupport#parkUntil LockSupport.parkUntil}</li>
         * </ul>
         */
        TIMED_WAITING,

        /**
         * Thread state for a terminated thread.
         * The thread has completed execution.
         */
        TERMINATED;
    }

1. NEW(新建)

创建线程类实例后,线程尚未启动时,该线程为NEW状态

public class ThreadDemo4 {

    public static void main(String[] args) {

        Thread myThread = new Thread(() -> {
        }, "myThread");
        // 创建线程类实例时,线程进入NEW状态
        System.out.println(myThread.getState());
        
        myThread.start();
    }
}

2. RUNNABLE(就绪)

调用start()方法,该线程进入RUNNABLE状态。该状态表示线程等待JVM的线程调度器的调度。

public class ThreadDemo5 {
    
    public static void main(String[] args) {
        Thread myThread = new Thread(() -> {
        }, "myThread");
        myThread.start();
        // 调用start方法,该线程进入RUNNABLE状态
        System.out.println(myThread.getState());
    }
}

3. BLOCKED(阻塞)

当线程进入被加锁的方法或代码块时,若锁被其他线程占用,则该线程为BLOCKED状态。待线程取得锁后变回RUNNABLE状态

public class ThreadDemo6 {

    public static void main(String[] args) throws InterruptedException {
        ThreadDemo6 threadDemo6 = new ThreadDemo6();

        new Thread(() -> threadDemo6.lock(), "myThread1").start();
        TimeUnit.SECONDS.sleep(1);

        Thread myThread2 = new Thread(() -> threadDemo6.lock(), "myThread2");
        myThread2.start();

        TimeUnit.SECONDS.sleep(1);
        // 在获取锁之前,该线程进入BLOCKED状态
        System.out.println(myThread2.getState());

    }

    public synchronized void lock() {
        System.out.println(Thread.currentThread().getName() + "-->获取锁");
        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + "-->释放锁");
    }
}

4. WAITING(等待)

当程对象调用wait()方法后,该线程进入WAITING状态,它需要等待其他线程对象显式的唤醒,否则会处理无限期等待状态。当其他线程调用notify()notifyAll()时,则线程变回RUNNABLE状态。

处于该状态下的线程不会被分配CPU执行时间。

public class ThreadDemo7 {

    final Object lock = new Object();

    public static void main(String[] args) {
        ThreadDemo7 threadDemo7 = new ThreadDemo7();

        Thread myThread1 = new Thread(() -> threadDemo7.waiting(), "myThread1");
        myThread1.start();

        Thread myThread2 = new Thread(() -> threadDemo7.waiting(), "myThread2");
        myThread2.start();

        // 当程对象调用wait()方法后,该线程进入WAITING状态
        System.out.println(myThread1.getState());
    }

    public void waiting() {
        synchronized (lock) {
            Thread thread = Thread.currentThread();
            if (thread.getName().equals("myThread1")) {
                try {
                    lock.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            } else {
                lock.notifyAll();
            }
        }
    }
}

5. TIMED_WAITING(超时等待)

WAITING的区别是,增加了超时时间,当超过指定时间还未被显式唤醒,则自动唤醒。

处于该状态下的线程不会被分配CPU执行时间。

public class ThreadDemo8 {

    final Object lock = new Object();

    public static void main(String[] args) throws InterruptedException {
        ThreadDemo8 threadDemo8 = new ThreadDemo8();

        Thread myThread1 = new Thread(() -> threadDemo8.waiting(), "myThread1");
        myThread1.start();

        Thread myThread2 = new Thread(() -> threadDemo8.waiting(), "myThread2");
        myThread2.start();

        // 当程对象调用wait(1)方法后,该线程进入TIMED_WAITING状态
        System.out.println(myThread1.getState());
        TimeUnit.SECONDS.sleep(2);
        System.out.println(myThread1.getState());
    }

    public void waiting() {
        synchronized (lock) {
            Thread thread = Thread.currentThread();
            if (thread.getName().equals("myThread1")) {
                try {
                    lock.wait(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

6. TERMINATED(终止)

run()执行完后,则线程终止

public class ThreadDemo9 {

    public static void main(String[] args) throws InterruptedException {
        Thread myThread = new Thread(() -> {
            System.out.println("run方法结束");
        }, "myThread");
        myThread.start();
        TimeUnit.SECONDS.sleep(1);
        System.out.println(myThread.getState());
    }
}

相关文章

网友评论

    本文标题:JUC全家桶 | Java线程的两种创建方式和六种状态

    本文链接:https://www.haomeiwen.com/subject/mjelpktx.html