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() 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());     } }


__EOF__

  • 本文作者: xbhog
  • 本文链接: https://www.cnblogs.com/xbhog/p/15679944.html
  • 关于博主: 评论和私信会在第一时间回复。或者直接私信我。
  • 版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
  • 声援博主: 如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。
  • 免责声明:本网信息来自于互联网,目的在于传递更多信息,并不代表本网赞同其观点。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,并请自行核实相关内容。本站不承担此类作品侵权行为的直接责任及连带责任。如若本网有任何内容侵犯您的权益,请及时联系我们,本站将会在24小时内处理完毕。
    相关文章
    返回顶部