JAVA高并发--多线程基础

本文介绍了Java中线程的基础知识,包括线程的定义、生命周期、创建方式以及线程API的使用,如Thread构造方法、start方法、sleep、yield、interrupt和join等。此外,还提到了线程安全、数据同步、线程间通信和线程池的相关概念。
摘要由CSDN通过智能技术生成

快速认识线程:

线程定义:

操作系统中对线程的定义是:线程(thread)是操作系统能够进行运算调度的最小单位。它被包含在进程之中,是进程中的实际运作单位。一条线程指的是进程中一个单一顺序的控制流,一个进程中可以并发多个线程,每条线程并行执行不同的任务。每个线程都有自己的局部变量表、程序计数器、以及生命周期。

线程生命周期:

通过上图我们可以知道线程的生命周期主要为五个阶段:

NEW:

NEW只是创建了一个线程对象,并不处于执行状态,因为没有调用start启动线程。New状态通过start方法进去RUNNABLE状态

RUNNABLE:

线程对象调用start方法后进去RUNNABLE状态,此时jvm真正创建了一个线程。意味着线程具有个执行资格,但需要等待cpu调度。

RUNNING:

一旦cpu通过轮询或则其他方式从执行队列中调度了线程,线程开始执行自己的逻辑代码。running状态可能发生一下状态转换:

  进入TERMINATED状态,比如调用了jdk不推荐的stop方法或者某个逻辑标识

  进入BLOCKED状态,比如调用sleep,或者wait方法而加入waitset

进行某个I/O阻塞,而进去BLOCKED状态

获取某个锁资源,从而加入到该锁的阻塞队列中而进入BLOCKED状态

由于CPU调度使线程放弃执行,进入RUNNABLE状态

线程主动调用yield方法,放弃cpu执行权,进入RANNABLE状态

BLOCKED:

线程阻塞状态,在此状态中可以切换到如下几种状态:

进入TERMINATED状态,比如调用了jdk不推荐的stop方法或意外死亡

线程阻塞结束,比如读取了阻塞数据进入到RUNNABLE状态

线程完成了指定事件的休眠,进入到RUNNABLE状态

wait中的线程被其他线程notify/notifyall唤醒,进入RUNNABLE状态

线程获取了某个锁资源,进入RUNNABLE状态

线程在阻塞过程中被打断,比如线程调用了interrupt,进入RUNNABLE状态

TERMINATED:

TERMINATED是一个线程的最终状态,在改状态中线程将不会切换到任何状态,进入TERMINATED状态,意未着线程的整个生命周期结束了。

创建创建:

我们认识了线程,了解了线程的生命周期,那在Java中该如何创建一个线程了,java提供了三种方式创建线程:

Thread:

Runnable:

Callable:

 

线程api:

Thread构造方法:

Thread start方法:

我们定义线程重写了run方法,但是启动的时候却调用了start方法,那么run和start方法有什么关系了?在调用start启动一个线程,该线程进入runnable状态,调用start后到底进行了那些操作?我们带着问题,一起寻找答案。

/**
     * Causes this thread to begin execution; the Java Virtual Machine
     * calls the <code>run</code> method of this thread.
     * <p>
     * The result is that two threads are running concurrently: the
     * current thread (which returns from the call to the
     * <code>start</code> method) and the other thread (which executes its
     * <code>run</code> method).
     * <p>
     * It is never legal to start a thread more than once.
     * In particular, a thread may not be restarted once it has completed
     * execution.
     *
     * @exception  IllegalThreadStateException  if the thread was already
     *               started.
     * @see        #run()
     * @see        #stop()
     */
    public synchronized void start() {
        /**
         * This method is not invoked for the main method thread or "system"
         * group threads created/set up by the VM. Any new functionality added
         * to this method in the future may have to also be added to the VM.
         *
         * A zero status value corresponds to state "NEW".
         */
        if (threadStatus != 0)
            throw new IllegalThreadStateException();

        /* Notify the group that this thread is about to be started
         * so that it can be added to the group's list of threads
         * and the group's unstarted count can be decremented. */
        group.add(this);

        boolean started = false;
        try {
            start0();
            started = true;
        } finally {
            try {
                if (!started) {
                    group.threadStartFailed(this);
                }
            } catch (Throwable ignore) {
                /* do nothing. If start0 threw a Throwable then
                  it will be passed up the call stack */
            }
        }
    }

Thread sleep方法:

Thread yeild方法:

Thread interrupt方法:

Thread join方法:

设置线程优先级:

获取当前线程:

如何正确关闭一个线程:

设置线程上下文类加载器:

线程安全与数据同步:

 

线程间通信:

 

Hook线程及线程异常:

 

线程池原理及自定义线程:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值