多线程基础知识(基础系列二)

一、线程的生命周期

当线程被创建并启动以后,它既不是一启动就进入了执行状态,也不是一直处于执行状态。
在线程的生命周期中,它要经过新建(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接口有返回值,可以抛出异常

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值