• 什么是JUC


    什么是JUC

        JUC指的是:Java里的三个包

    • java.util.concurrent
    • java.util.concurrent.atomic:原子性
    • java.util.concurrent.locks:lock锁

     

    回顾线程和进程

    进程

            程序执行的一次过程,一个进程包含一个或多个线程。进程是资源分配的单位

    线程

            可以指程序执行过程中,负责实现某个功能的单位。线程是CPU调度和执行的单位

    并发

            同一时刻,多个线程交替执行。(一个CPU交替执行线程)

    并行

            同一时刻,多个线程同时执行。(多个CPU同时执行多个线程)

        查询cpu核数

    1. public class Test1 {
    2. public static void main(String[] args) {
    3. //查询cpu核数
    4. //CPU 密集型,IO密集型
    5. System.out.println(Runtime.getRuntime().availableProcessors());
    6. }
    7. }

    并发编程的本质

            并发编程的本质是充分利用cpu资源。

        问题:Java真的可以开启线程吗

            不可以。Java创建Thread类调用start方法,底层是把线程放到一个组里面,然后调用一个本地方法start0;方法底层是C++;Java无法操作硬件

    Thread部分源码

    1. public synchronized void start() {
    2. /**
    3. * This method is not invoked for the main method thread or "system"
    4. * group threads created/set up by the VM. Any new functionality added
    5. * to this method in the future may have to also be added to the VM.
    6. *
    7. * A zero status value corresponds to state "NEW".
    8. */
    9. if (threadStatus != 0)
    10. throw new IllegalThreadStateException();
    11. /* Notify the group that this thread is about to be started
    12. * so that it can be added to the group's list of threads
    13. * and the group's unstarted count can be decremented. */
    14. group.add(this);
    15. boolean started = false;
    16. try {
    17. start0();
    18. started = true;
    19. } finally {
    20. try {
    21. if (!started) {
    22. group.threadStartFailed(this);
    23. }
    24. } catch (Throwable ignore) {
    25. /* do nothing. If start0 threw a Throwable then
    26. it will be passed up the call stack */
    27. }
    28. }
    29. }
    30. //本地方法,底层的C++ java无法直接操作硬件
    31. private native void start0();

    多线程回顾

    线程的几种状态

            新生状态、运行状态、阻塞状态、等待状态(死等)、超时等待状态(过期不候)、停止状态

        Thread.State的源码

    1. public enum State {
    2. /**
    3. * Thread state for a thread which has not yet started.
    4. */
    5. // 新生
    6. NEW,
    7. /**
    8. * Thread state for a runnable thread. A thread in the runnable
    9. * state is executing in the Java virtual machine but it may
    10. * be waiting for other resources from the operating system
    11. * such as processor.
    12. */
    13. // 运行
    14. RUNNABLE,
    15. /**
    16. * Thread state for a thread blocked waiting for a monitor lock.
    17. * A thread in the blocked state is waiting for a monitor lock
    18. * to enter a synchronized block/method or
    19. * reenter a synchronized block/method after calling
    20. * {@link Object#wait() Object.wait}.
    21. */
    22. // 阻塞
    23. BLOCKED,
    24. /**
    25. * Thread state for a waiting thread.
    26. * A thread is in the waiting state due to calling one of the
    27. * following methods:
    28. *
      • *
      • {@link Object#wait() Object.wait} with no timeout
    29. *
    30. {@link #join() Thread.join} with no timeout
  • *
  • {@link LockSupport#park() LockSupport.park}
  • *
  • *
  • *

    A thread in the waiting state is waiting for another thread to

  • * perform a particular action.
  • *
  • * For example, a thread that has called Object.wait()
  • * on an object is waiting for another thread to call
  • * Object.notify() or Object.notifyAll() on
  • * that object. A thread that has called 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:
  • *
    • *
    • {@link #sleep Thread.sleep}
    • *
    • {@link Object#wait(long) Object.wait} with timeout
    • *
    • {@link #join(long) Thread.join} with timeout
    • *
    • {@link LockSupport#parkNanos LockSupport.parkNanos}
    • *
    • {@link LockSupport#parkUntil LockSupport.parkUntil}
    • *
    • */
    • // 超时等待
    • TIMED_WAITING,
    • /**
    • * Thread state for a terminated thread.
    • * The thread has completed execution.
    • */
    • // 终止
    • TERMINATED;
    • }
    • sleep和wait的区别

      1. sleep是Thread类的本地方法;wait是Object类的方法。
      2. sleep不释放锁;wait释放锁。
      3. sleep可以使用在任何地方;wait必须在同步代码块中
      4. sleep不需要和synchronized关键字一起使用;wait必须和synchronized代码块一起使用。
      5. sleep不需要被唤醒(时间到了自动退出阻塞);wait需要被唤醒。
      6. sleep一般用于当前线程休眠,或者轮循暂停操作;wait则多用于多线程之间的通信。
      7. sleep和wait都需要捕获异常

    • 相关阅读:
      Tomcat Servlet内存马
      dll文件反编译源代码 C#反编译 dotpeek反编译dll文件后export
      【测试开发】几种常见的自动化测试框架
      Windows安装Visual Studio2019+OpenCV配置
      【iOS】多线程梳理
      内核APC&用户APC详解
      Java native关键字 实现
      WEB前端代码书写规范
      【day09】继承、super、this、抽象类
      【好书推荐】你想要的编码规范都在这里 | 《代码整洁之道》
    • 原文地址:https://blog.csdn.net/weixin_48426115/article/details/127894564