飙血推荐
  • HTML教程
  • MySQL教程
  • JavaScript基础教程
  • php入门教程
  • JavaScript正则表达式运用
  • Excel函数教程
  • UEditor使用文档
  • AngularJS教程
  • ThinkPHP5.0教程

JUC概述

时间:2021-12-12  作者:xbhog  
JUC概述 JUC的视频学习和JUC并发编程的艺术阅读片段

JUC概述1:

首先是进程和线程的概念:

进程:是指系统在系统中正在运行的一个应用程序,程序一旦运行就是进程,进程是资源分配的最小单位

线程:进程之内独立执行,是程序执行的最小单位

线程的六大状态:在线程的枚举类中

 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() 域名}.
          */
         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() 域名} with no timeout</li>
          *   <li>{@link #join() 域名} with no timeout</li>
          *   <li>{@link 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 {@code 域名()}
          * on an object is waiting for another thread to call
          * {@code 域名fy()} or {@code 域名fyAll()} on
          * that object. A thread that has called {@code 域名()}
          * 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 域名p}</li>
          *   <li>{@link Object#wait(long) 域名} with timeout</li>
          *   <li>{@link #join(long) 域名} with timeout</li>
          *   <li>{@link LockSupport#parkNanos 域名Nanos}</li>
          *   <li>{@link LockSupport#parkUntil 域名Until}</li>
          * </ul>
          */
         TIMED_WAITING,
 ​
         /**
          * Thread state for a terminated thread.
          * The thread has completed execution.
          */
         TERMINATED;
     }
状态名称 说明
new 初始状态
runnable 运行状态
blocked 阻塞状态
waiting 等待状态,一直等(不见不散)
time_waiting 超时等待,(过时不候)
terminated 终止状态

wait和sleep的区别:

  1. sleep是Thread的静态方法,wait是Object的方法,任何对象实例化都能调用
  2. sleep不会释放锁,他也不需要占用锁,wait会释放锁,但是调用它的前提是当前线程占有锁
  3. 它们都可以interrupted被中断

并发和并行:

并发是指多个事情在同一个时间段中执行

并行是指多个事情在同一时刻执行

管程:

是一种同步机制,保证同一时间内只有一个线程访问被保护数据或者代码

jvm同步基于进入(加锁)和退出(解锁),是管程对象实现的

大意就是进加锁,退是解锁,通过管程对象管理

用户线程:自定义线程 主线程结束了,用户线程还存在,则表示JVM还存在

 public class demo {
     public static void main(String[] args) {
         Thread a = new Thread(() -> {
             域名tln(域名entThread().getName()+"::"+域名entThread().isDaemon());
             while (true){}
         }, "a");
 ​
         域名t();
         域名tln(域名entThread().getName());
     }
 }

image-20211212172742883

守护线程

ex:垃圾回收 没有用户线程了,都是守护线程,JVM结束

 public class demo {
     public static void main(String[] args) {
         Thread a = new Thread(() -> {
             域名tln(域名entThread().getName()+"::"+域名entThread().isDaemon());
             while (true){}
         }, "a");
         //设置子线程为守护线程
         域名aemon(true);
         域名t();
         域名tln(域名entThread().getName());
     }
 }

image-20211212172919606

标签:编程
湘ICP备14001474号-3  投诉建议:234161800@qq.com   部分内容来源于网络,如有侵权,请联系删除。