欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

多线程基础知识

程序员文章站 2022-05-05 23:13:26
...

一、线程的生命周期

当线程被创建并启动以后,它既不是一启动就进入了执行状态,也不是一直处于执行状态。
在线程的生命周期中,它要经过新建(New)、就绪或可运行(Runnable)、运行(Running)、阻塞(Blocked)和死亡(Dead) 5 种状态。尤其是当线程启动以后,它不可能一直"霸占"着 CPU 独自运行,所以 CPU 需要在多条线程之间切换,于是线程状态也会多次在运行、阻塞之间切换。
多线程基础知识我们可以通过输入 Thread.State,鼠标点击进入查看5种状态

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;
    }

二、线程的三种创建方式

线程的创建有三种方式,分别为继承Thread类、实现Runnable接口、实现Callable接口
第一种是继承Thread类

public class Demo1 {
    public static void main(String[] args) {
        MyThread myThread = new MyThread ("thread_name");
        myThread.start ();
    }
}

class MyThread extends Thread {
    // 给线程起个名字
    public MyThread(String name) {
        super (name);
    }

    @Override
    public void run() {
        System.out.println ("继承Thread,线程名字是=>" + Thread.currentThread ().getName ());
        System.out.println ("继承Thread,线程名字是=>" + this.getName ());
    }
}

在main函数里面创建了一个MyThread实例,然后通过start方法启动线程,这里的start方法调用后线程并没有马上执行,而是进入了就绪状态,会等待获取到CPU的资源后才会真正的处于运行状态。

第二种是实现Runnable接口,重写run方法

public class Demo2 {
    public static void main(String[] args) {
        // 创建资源类
        RunnableTask task = new RunnableTask ();
        // 启动两个线程
        new Thread (task, "a").start ();
        new Thread (task, "b").start ();
    }
}

class RunnableTask implements Runnable {

    @Override
    public void run() {
        System.out.println ("实现了Runnable接口的线程类,线程名字=>" + Thread.currentThread ().getName ());
    }
}

两个线程共用一个task资源类,通过给两个线程传入名字参数,进行区分
第三种是实现Callable接口

public class Demo3 {
    public static void main(String[] args) {

        // 创建一个异步任务
        FutureTask<Integer> task = new FutureTask<> (new CallableTask ());
        // 启动线程
        new Thread (task).start ();

        try {
            // 使用get方法会阻塞,直到任务执行结束返回结果
            Integer integer = task.get ();
            System.out.println (integer);
        } catch (InterruptedException e) {
            e.printStackTrace ();
        } catch (ExecutionException e) {
            e.printStackTrace ();
        }

    }
}

class CallableTask implements Callable<Integer> {

    @Override
    public Integer call() throws Exception {
        return 123;
    }
}

我们首先创建了一个FutureTask对象,传入一个CallableTask实例对象,然后使用创建的FutureTask对象作为任务去启动一个线程,最后通过get方法去获取返回结果

总结

1、使用继承的方式可以直接使用this,this代表的就是当前对象,但是java不支持多继承,所以继承了Thread类就不能继承其他的,而且任务和运行机制没有解耦
2、实现Runnable接口没有返回值,不能抛出异常,只能在内部捕获
3、实现Callable接口有返回值,可以抛出异常