线程与进程

什么是JUC

image-20230228162454333

java.until工具包、包、分类

线程和进程

进程:一个程序。QQ.exe等等程序的集合

一个进程往往包含多个进程,至少包含一个

Java默认包含几个线程?2个 main GC

Java可以开启线程吗

public synchronized void start() {
    /**
     * This method is not invoked for the main method thread or "system"
     * group threads created/set up by the VM. Any new functionality added
     * to this method in the future may have to also be added to the VM.
     *
     * A zero status value corresponds to state "NEW".
     */
    if (threadStatus != 0)
        throw new IllegalThreadStateException();

    /* Notify the group that this thread is about to be started
     * so that it can be added to the group's list of threads
     * and the group's unstarted count can be decremented. */
    group.add(this);

    boolean started = false;
    try {
        start0();
        started = true;
    } finally {
        try {
            if (!started) {
                group.threadStartFailed(this);
            }
        } catch (Throwable ignore) {
            /* do nothing. If start0 threw a Throwable then
              it will be passed up the call stack */
        }
    }
}

private native void start0();

调用本地方法,底层的C++,Java无法直接操作硬件

并发,并行

并发编程:并发、并行

并发(多线程操作同一个资源)

  • CPU一核,模拟出来多条线程,天下武功,唯快不破,快速交替并行(多个人一起行走)
  • CPU多核,多个线程可以同时执行;线程池
package com.cz.demo1;

/**
 * @author 卓亦苇
 * @version 1.0
 * 2023/2/28 16:38
 */
public class Test {
    public static void main(String[] args) {
        //获取cpu核数
        System.out.println(Runtime.getRuntime().availableProcessors());
    }
}

并发编程的本质:充分利用CPU的资源

所有的公司都很看重!

企业,挣钱=>提高效率,裁员,找一个厉害的人顶替三个不怎么样的人;

人员(减)、技术成本(高)

线程的状态

public enum State {
    //线程新生
    NEW,

    //运行
    RUNNABLE,
    //阻塞
    BLOCKED,

    //等待
    WAITING,

    //超时等待
    TIMED_WAITING,

    //终止
    TERMINATED;
}

wait/sleep

  1. 来自不同的类

    wait => Object
    sleep => Thread

  2. 关于锁的释放
    wait 会释放锁,sleep睡觉了,抱着锁睡觉,不会释放!

  3. 使用范围不同
    wait:必须在同步代码块
    sleep可以再任何地方睡

  4. 是否需要捕获异常
    wait 不需要捕获异常
    sleep必须要捕获异常

Lock

传统Synchoronized

Lock锁

package com.cz.demo1;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author 卓亦苇
 * @version 1.0
 * 2023/2/28 22:54
 */
public class TestLock2 {
    public static void main(String[] args) {
        Ticket2 ticket2 = new Ticket2();

        new Thread(()->{for (int i = 0; i < 40; i++) {ticket2.sale();}},"A").start();
        new Thread(()->{for (int i = 0; i < 40; i++) {ticket2.sale();}},"B").start();
        new Thread(()->{for (int i = 0; i < 40; i++) {ticket2.sale();}},"C").start();

    }
}
//资源类 OOP
class Ticket2{
    //属性、方法
    private int num = 30;
    Lock lock = new ReentrantLock();
    //买票方式
    public void sale(){
        lock.lock();//加锁
        try {
            if(num>0){
                System.out.println(Thread.currentThread().getName()+"卖出了" +
                        num--+"票,剩余"+num);
            }
        }finally {
            lock.unlock();//解锁
        }
    }

}

image-20230228225338108

image-20230228225358807

synchronized与Lock锁的区别

  1. Synchronized内置的Java关键字,Lock 是一个Java类
  2. Synchronized 无法判断获取锁的状态,Lock 可以判断是否获取到了锁
  3. Synchronized 会自动释放锁,lock必须要手动释放锁!如果不释放锁,死锁
  4. synchronized 线程1(获得锁,阻塞)、线程2(等待,傻傻的等)) ; Lock锁就不一定会等待下去;
  5. Synchronized可重入锁,不可以中断的,非公平; Lock,可重入锁,可以判断锁,非公平(可以自己设置);
  6. synchronized适合锁少量的代码同步问题,Lock 适合锁大量的同步代码!
posted @   卓亦苇  阅读(11)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· AI与.NET技术实操系列(六):基于图像分类模型对图像进行分类
点击右上角即可分享
微信分享提示