目录
4、双检锁/双重校验锁(DCL,即 double-checked locking)
1、什么是JUC
JUC指的是:Java里的三个包
java.util.concurrent
java.util.concurrent.atomic:原子性
java.util.concurrent.locks:lock锁
2、线程和进程
1.进程
程序执行的一次过程,一个进程包含一个或多个线程。进程是资源分配的单位。
2.线程
可以指程序执行过程中,负责实现某个功能的单位。线程是CPU调度和执行的单位。
3.并发
同一时刻,多个线程交替执行。(一个CPU交替执行线程)
4.并行
同一时刻,多个线程同时执行。(多个CPU同时执行多个线程)
# 获取cpu的核数(cpu密集型;io密集型)
System.out.println(Runtime.getRuntime().availableProcessors());
并发编程的本质
并发编程的本质是充分利用cpu资源。
Java 真的可以开启线程吗? 答案是:开不了的
Java创建Thread类调用start方法,底层是把线程放到一个组里面,然后调用一个本地方法start0;方法底层是C++;Java无法操作硬件。
3、多线程回顾
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 <tt>Object.wait()</tt>
* on an object is waiting for another thread to call
* <tt>Object.notify()</tt> or <tt>Object.notifyAll()</tt> on
* that object. A thread that has called <tt>Thread.join()</tt>
* 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;
}
2.sleep和wait的区别
- sleep是Thread类的本地方法;wait是Object类的方法。
- sleep不释放锁;wait释放锁。
- sleep不需要和synchronized关键字一起使用;wait必须和synchronized代码块一起使用。
- sleep不需要被唤醒(时间到了自动退出阻塞);wait需要被唤醒。
- sleep一般用于当前线程休眠,或者轮循暂停操作;wait则多用于多线程之间的通信。
4、Lock锁
1、传统 Synchronized锁
public class SaleTicketTDemo01 {
/*
* 真正的多线程开发,公司中的开发,降低耦合性
* 线程就是一个单独的资源类,没有任何附属的操作!
* 1、 属性、方法
*/
public static void main(String[] args) {
//并发:多个线程同时操作一个资源类,把资源类丢入线程
Ticket ticket = new Ticket();
// @FunctionalInterface 函数式接口,jdk1.8 lambada表达式
new Thread(() -> {
for (int i = 1; i < 50; i++) {
ticket.sale();
}
}, "A").start();
new Thread(() -> {
for (int i = 1; i < 50; i++) {
ticket.sale();
}
}, "B").start();
new Thread(() -> {
for (int i = 1; i < 50; i++) {
ticket.sale();
}
}, "C").start();
}
}
//资源类 OOP
class Ticket {
//属性、方法
private int number = 50;
// 卖票的方式
// synchronized 本质: 队列,锁
public synchronized void sale() {
if (number > 0) {
System.out.println(Thread.currentThread().getName() + "卖出了" +
(50-(--number)) + "张票,剩余:" + number + "张票");
}
}
}
2.公平锁和非公平锁(锁的底层)
- 公平锁:十分公平,线程执行顺序按照先来后到顺序
- 非公平锁:十分不公平:可以插队 (默认锁)
将上面的卖票例子用lock锁 替换synchronized:
public class SaleTicketTDemo02 {
public static void main(String[] args) {
//并发:多个线程同时操作一个资源类,把资源类丢入线程
Ticket2 ticket = new Ticket2();
// @FunctionalInterface 函数式接口,jdk1.8 lambada表达式
new Thread(() -> {
for (int i = 1; i < 50; i++) {
ticket.sale();