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() 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 {@code Object.wait()}
          * on an object is waiting for another thread to call
          * {@code Object.notify()} or {@code Object.notifyAll()} on
          * that object. A thread that has called {@code Thread.join()}
          * 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;
    }
状态名称 说明
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(() -> {
             System.out.println(Thread.currentThread().getName()+"::"+Thread.currentThread().isDaemon());
             while (true){}
        }, "a");
 ​
         a.start();
         System.out.println(Thread.currentThread().getName());
    }
 }

守护线程

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

 public class demo {
     public static void main(String[] args) {
         Thread a = new Thread(() -> {
             System.out.println(Thread.currentThread().getName()+"::"+Thread.currentThread().isDaemon());
             while (true){}
        }, "a");
         //设置子线程为守护线程
         a.setDaemon(true);
         a.start();
         System.out.println(Thread.currentThread().getName());
    }
 }

最新文章

  1. 到爱尔兰敲代码 / Come, Coding in Ireland
  2. JS常用属性
  3. could not get lock /var/lib/dpkg/lock
  4. 一图看懂host_only nat bridge拓扑结构
  5. NeHe OpenGL教程 第三十九课:物理模拟
  6. Android 获取控件相对于屏幕位置
  7. android 上下文菜单详解
  8. ntpd和ntpdate的区别
  9. 国内下载比较快的Maven仓库镜像
  10. 网络配置之基本网络配置(cenos6)
  11. elasticsearch+kibana+metricbeat安装部署方法
  12. HTML出现错位的问题
  13. XPosed 示例
  14. c++ string 转double
  15. C# Main函数中调用异步方法的2种实现
  16. kubernets网络模式
  17. MS16-032 漏洞复现
  18. Centos下yum安装Nginx报错 No package nginx available.
  19. CSS(一):CSS简介和基本语法
  20. [ActionScript 3.0] 记录几个ByteArray 十六进制 String等相互转换的方法

热门文章

  1. java框架面试高频问题(SpringMVC)
  2. ES6--ES12笔记整理(1)
  3. 基于ambari搭建hadoop生态圈大数据组件
  4. [noi1779]D
  5. [bzoj1105]石头花园
  6. Java 操作符小记
  7. Atcoder Grand Contest 020 F - Arcs on a Circle(DP+小技巧)
  8. 实现一个简单的类似不蒜子的PV统计器
  9. Bedtools genomecov 计算覆盖度
  10. 【机器学习与R语言】12- 如何评估模型的性能?