JAVA-线程

前言

线程:程序执行的最小单元,进程中独立运行的子任务,其目的是为了充分利用CPU的空闲时间来处理其他任务

Java线程调度

抢占式调度

      指的是每条线程执行的时间、线程的切换都由操作系统控制。系统控制 指的是在系统某种允许机制下,可能每条线程都分配同样的执行时间片,也可能是某些线程执行的时间片较长,甚至某些线程得不到执行的时间片。在这种机制下 一个线程的堵塞不会导致整个进行堵塞。

协同式调度

     指某一线程执行完后主动通知系统切换到另一线程上执行,线程执行的时间有线程本身控制,线程的切换可以预知。有个致命弱点:如果一个线程编写有问题,可能导致整个系统崩溃

Java线程状态

java线程一共有6种状态,查看Thread 源码 State 枚举类

public enum State {
        /**
         * Thread state for a thread which has not yet started.
         * 初始状态 线程被创建出来还没有执行start()
         */
        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.
         * 运行状态 调用完starrt 方法之后等待cpu分配资源,可能立马执行可能等待释放cpu资源
         */
        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}.
         * 线程阻塞状态,被synchronized  修饰的方法或者代码块
         */
        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.
         * 无条件等待状态,当调用wait()/join()/LockSupport.park() 不加超时的方法之后的状态,
         * 如果没有被唤醒或者等待的线程没有结束 那么将一直等待且不会分配cpu资源和持有锁
         */
        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>
         *  有条件等待状态,当线程调用 
         *  sleep(long)/wait(long)/join(long)/LockSupport.parkNanos(long)/
         *  LockSupport.parkUntil(long),在指定时间内没有被唤醒或者等待线程没有结束,时间到会被 
         *  系统自定唤醒正常退出
         */
        TIMED_WAITING,

        /**
         * Thread state for a terminated thread.
         * The thread has completed execution.
         *  线程结束 ,当线程run执行完毕
         */
        TERMINATED;
    }

Java线程锁

全部转载至https://tech.meituan.com/2018/11/15/java-lock.html,更多内容请跳转查看,此处做笔记使用

 

Java线程常用操作

Thread.sleep(long): 主要作用是当前线程停止执行,把cpu让给其他线程执行,如果有锁,不会释放锁,等时间到后 恢复为可运行状态等待cpu分配资源

Object.wait(): 是当前线程阻塞,前提是必须先获得锁,一般配合synchronized代码块执行,当执行wait 方法的时候会释放当前的锁,然后让出CPU 进入等待状态,只有当notify/notifyAll被执行的时候,才会唤醒一个或多个正处于等待的线程,继续执行下去

Object.notify()/notifyAll(): notify 只唤醒一个等待线程。所以 如果有多个线程等待一个一个对象,这个方法只会唤醒其中的一个线程,由操作系统对多线程的管理决定。notifyall 会唤醒所有等待对象的线程,比如在生产者-消费者里面使用,每次都需要唤醒所有的消费者或者生产者。

Thread.join(): 表示一旦某个线程调用了join方法,那么久要一直运行到该程序执行,才会运行其他线程。

Thread.yield(): 是当前线程暂时释放cpu,不释放锁,使线程进入准备状态 ,只能让拥有相同优先级的线程竞争CPU资源(包括自己本身) 

 

很零碎 后续不断补充,仅做笔记使用

END

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

H风雨Y

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值