JUC并发编程(一)

因为内容太多了,所以将其拆分为以下内容

参考

https://www.bilibili.com/video/BV1B7411L7tE

多线程进阶=>JUC并发编程

JDK下的

  • java.util.concurrent
  • java.util.concurrent.atomic
  • java.util.concurrent.locks
  • java.util.fuction

回顾多线程

1
2
3
4
5
6
public class Test1 {
public static void main(String[] args) {
//获得CPU核数
System.out.println(Runtime.getRuntime().availableProcessors());
}
}

线程状态

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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;
}

中文对应

Enum Constant and Description
BLOCKED 一个线程的线程状态阻塞等待监视器锁定。
NEW 线程尚未启动的线程状态。
RUNNABLE 可运行线程的线程状态。
TERMINATED 终止线程的线程状态。
TIMED_WAITING 具有指定等待时间的等待线程的线程状态。
WAITING 等待线程的线程状态

Lock锁

传统的synchronized

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
public class Test1 {
public static void main(String[] args) {
Ticket ticket=new Ticket();

new Thread(()->{
for (int i = 0; i < 40; i++) {
ticket.sale();
}
},"A").start();
new Thread(()->{
for (int i = 0; i < 40; i++) {
ticket.sale();
}
},"B").start();
new Thread(()->{
for (int i = 0; i < 40; i++) {
ticket.sale();
}
},"C").start();
}
}
class Ticket{
private int number=30;

public synchronized void sale(){
if(number>0){
number--;
System.out.println(Thread.currentThread().getName()+"当前票数"+number);
}
}
}

Lock接口

  • 随着这种增加的灵活性,额外的责任。 没有块结构化锁定会删除使用synchronized方法和语句发生的锁的自动释放。 在大多数情况下,应使用以下惯用语:

    Lock l = ...; l.lock(); try { // access the resource protected by this lock } finally { l.unlock(); }

    当在不同范围内发生锁定和解锁时,必须注意确保在锁定时执行的所有代码由try-finally或try-catch保护,以确保在必要时释放锁定。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
public class Test2 {
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();
}
}

class Ticket2 {
private int number = 30;
//1.new ReentrantLock();
Lock lock = new ReentrantLock();

public void sale() {
//2.lock();
lock.lock();
try {
if (number > 0) {
number--;
System.out.println(Thread.currentThread().getName() + "当前票数" + number);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
//3.unlock();
lock.unlock();
}
}
}

synchronized 与 lock 区别

  1. Synchronized 内置的Jva关键字, Lock 是一个Java类
  2. Synchronized 无法判断获取锁的状态,Lock可以判断是否获取到了锁
  3. Synchronized 会自动释放锁,lock必须要手动释放锁!如果不释放锁,死锁
  4. Synchronized 线程1(获得锁,阳塞).线程2 (等待,使使的等) ;Lock锁就不一定会等待下去;
  5. Synchronized可重入锁 ,不可以中断的,非公平;Lock,可重入锁,可以判断锁,非公平(可以自己设置) ;
  6. Synchronied 适合锁少量的代码同步问题 ,Lock适合锁大量的同步代码!

锁是什么,如何判断锁的是谁?

因为内容太多了,所以将其拆分为以下内容