- 背景
- 什么是JUC
- 进程和线程回顾
- Lock锁
- 生产者和消费者
- 8锁现象
- 集合类不安全
- Callable
- CountDownLatch, CyclicBarrier, Semaphore
- 读写锁
- 阻塞队列
- 线程池
- 四大函数式接口
- Stream流式计算
- 分支合并
- 异步回调
- JMM
- volatile
- 深入单例模式
- 深入理解CAS
- 原子引用
背景
本笔记来自于狂神说JUC笔记。
B站link: https://www.bilibili.com/video/BV1B7411L7tE
什么是JUC
java.util.concurrent
java.util.concurrent.atomic
java.util.concurrent.locks
java.util 工具包,包,分类
Runnable
没有返回值,效率相比于 Callable 相对较低
进程和线程回顾
线程,进程,如果不能用一句话说出来的技术,不扎实
进程:一个程序,QQ.exe, Music.exe 程序的集合
一个进程往往可以额包含多个线程,至少包含一个线程
Java 默认有几个线程? 2个。 main 和 GC
线程:开了一个进程 Typora,写字,自动保存(线程负责的)
对于Java而言: Thread,Runnable, Callable
Java真的可以开启线程吗? 开不了
thread code
AtomicInteger x = new AtomicInteger();
new Thread(()->{ for (int i = 0; i < 40; ++i) System.out.println(x.getAndIncrement());}, "B").start();
thread start0()
/**
* Causes this thread to begin execution; the Java Virtual Machine
* calls the <code>run</code> method of this thread.
* <p>
* The result is that two threads are running concurrently: the
* current thread (which returns from the call to the
* <code>start</code> method) and the other thread (which executes its
* <code>run</code> method).
* <p>
* It is never legal to start a thread more than once.
* In particular, a thread may not be restarted once it has completed
* execution.
*
* @exception IllegalThreadStateException if the thread was already
* started.
* @see #run()
* @see #stop()
*/
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();
并发,并行
并发编程:并发,并行
并发(多线程操作同一个资源)
- CPU一核,模拟出来多条线程,天下武功,唯快不破,快速交替
并行(多个人一起行走)
- CPU多核,多个线程可以同时执行;线程池
getAvailableProcessors
private static void getAvailableProcessors() {
//获取CPU的核数
System.out.println("available processors: " + Runtime.getRuntime().availableProcessors());
}
并发编程的本质:充分利用CPU的资源
所有的公司都很看重!
企业,挣钱 => 提高效率,裁员,找一个厉害的人顶替三个不怎么样的人
人员(减),技术成本(高)
线程有几个状态 - 6个
thread state
/**
* A thread state. A thread can be in one of the following states:
* <ul>
* <li>{@link #NEW}<br>
* A thread that has not yet started is in this state.
* </li>
* <li>{@link #RUNNABLE}<br>
* A thread executing in the Java virtual machine is in this state.
* </li>
* <li>{@link #BLOCKED}<br>
* A thread that is blocked waiting for a monitor lock
* is in this state.
* </li>
* <li>{@link #WAITING}<br>
* A thread that is waiting indefinitely for another thread to
* perform a particular action is in this state.
* </li>
* <li>{@link #TIMED_WAITING}<br>
* A thread that is waiting for another thread to perform an action
* for up to a specified waiting time is in this state.
* </li>
* <li>{@link #TERMINATED}<br>
* A thread that has exited is in this state.
* </li>
* </ul>
*
* <p>
* A thread can be in only one state at a given point in time.
* These states are virtual machine states which do not reflect
* any operating system thread states.
*
* @since 1.5
* @see #getState
*/
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;
}
wait/sleep 区别
1. 来自不同的类
wait => Object
sleep => Thread
2. 关于锁的释放
wait 会释放锁,sleep睡觉了,抱着锁睡觉,不会释放锁
3. 使用的范围是不同的
wait 必须在同步代码块中
sleep 可以在任何地方睡
4. 是否需要捕获异常
wait 不需要捕获异常
sleep 必须要捕获异常