JAVA学习笔记25——JUC
Posted 今日伊始
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JAVA学习笔记25——JUC相关的知识,希望对你有一定的参考价值。
JUC
Lock锁
Lock接口
ReentrantLock:可重入锁
ReentrantReadWriteLock.ReadLock:读锁
ReentrantReadWriteLock.WriteLock:写锁
public ReentrantLock() {
sync = new NonfairSync();
}
/**
* Creates an instance of {@code ReentrantLock} with the
* given fairness policy.
*
* @param fair {@code true} if this lock should use a fair ordering policy
*/
public ReentrantLock(boolean fair) {
sync = fair ? new FairSync() : new NonfairSync();
}
// 默认非公平锁
public class SaleTicketDemo02 {
public static void main(String[] args) {
// 并发,多线程操作同一个资源类
Ticket2 ticket = new Ticket2();
// lambda表达式(参数)->{代码}
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 Ticket2 {
private int number = 30;
Lock lock = new ReentrantLock();
public synchronized void sale() {
lock.lock();
try {
if (number > 0) {
System.out.println(Thread.currentThread().getName() + "卖出了第" + (number--) + "张票,剩余:" + number);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
}
Synchronized锁
public class SaleTicketDemo01 {
public static void main(String[] args) {
// 并发,多线程操作同一个资源类
Ticket ticket = new Ticket();
// lambda表达式(参数)->{代码}
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) {
System.out.println(Thread.currentThread().getName() + "卖出了第" + (number--) + "张票,剩余:" + number);
}
}
}
Synchronized锁与Lock锁区别
1.Synchronized是内置关键字,Lock是一个Java类
2.Synchronized无法判断获取锁的状态,Lock可以判断是否获取到了锁
3.Synchronized会自动释放锁,Lock需要手动释放锁
4.Synchronized线程一获得锁阻塞后,线程二会一直等待;Lock锁不一定会等待下去
5.Synchronized是可重入锁,不可以中断,非公平;Lock,可重入锁,可以判断锁,非公平
6.Synchronized适合少量的代码同步问题,Lock适合锁大量的同步代码
生产者消费者问题
Synchronized
public class Test1 {
public static void main(String[] args) {
Resource resource = new Resource();
new Thread(()->{
for (int i = 0;i < 10;i++) {
try {
resource.increment();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},"A").start();
new Thread(()->{
for (int i = 0;i < 10;i++) {
try {
resource.decrement();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},"B").start();
new Thread(()->{
for (int i = 0;i < 10;i++) {
try {
resource.increment();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},"C").start();
new Thread(()->{
for (int i = 0;i < 10;i++) {
try {
resource.decrement();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},"D").start();
}
}
class Resource {
private int num = 0;
public synchronized void increment() throws InterruptedException {
while (num != 0) {
// 等待
this.wait();
}
num++;
System.out.println(Thread.currentThread().getName()+"=>"+num);
// 通知线程+1完毕
this.notifyAll();
}
public synchronized void decrement() throws InterruptedException {
while (num == 0) {
// 等待
this.wait();
}
num--;
System.out.println(Thread.currentThread().getName()+"=>"+num);
// 通知线程-1完毕
this.notifyAll();
}
}
Lock
public class Test2 {
public static void main(String[] args) {
Resource resource = new Resource();
new Thread(()->{
for (int i = 0;i < 10;i++) {
try {
resource.increment();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},"A").start();
new Thread(()->{
for (int i = 0;i < 10;i++) {
try {
resource.decrement();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},"B").start();
new Thread(()->{
for (int i = 0;i < 10;i++) {
try {
resource.increment();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},"C").start();
new Thread(()->{
for (int i = 0;i < 10;i++) {
try {
resource.decrement();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},"D").start();
}
}
class Resource2 {
private int num = 0;
Lock lock = new ReentrantLock();
Condition condition = lock.newCondition();
public synchronized void increment() throws InterruptedException {
lock.lock();
try {
while (num != 0) {
// 等待
condition.await();
}
num++;
System.out.println(Thread.currentThread().getName()+"=>"+num);
// 通知线程+1完毕
condition.signalAll();
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
public synchronized void decrement() throws InterruptedException {
lock.lock();
try {
while (num == 0) {
// 等待
condition.await();
}
num--;
System.out.println(Thread.currentThread().getName()+"=>"+num);
// 通知线程-1完毕
condition.signalAll();
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
}
Condition精准的通知唤醒线程
// A执行完调用B,B执行完调用C,C执行完调用A
public class Test3 {
public static void main(String[] args) {
Resource3 resource3 = new Resource3();
new Thread(()->{
for (int i = 0;i < 10;i++) {
resource3.printA();
}
},"A").start();
new Thread(()->{
for (int i = 0;i < 10;i++) {
resource3.printB();
}
},"B").start();
new Thread(()->{
for (int i = 0;i < 10;i++) {
resource3.printC();
}
},"C").start();
}
}
class Resource3 {
private Lock lock = new ReentrantLock();
private Condition condition1 = lock.newCondition();
private Condition condition2 = lock.newCondition();
private Condition condition3 = lock.newCondition();
private int number = 1;
public void printA() {
lock.lock();
try {
while (number != 1) {
condition1.await();
}
System.out.println(Thread.currentThread().getName()+"=>AAA");
number = 2;
condition2.signal();
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
public void printB() {
lock.lock();
try {
while (number != 2) {
condition2.await();
}
System.out.println(Thread.currentThread().getName()+"=>BBB");
number = 3;
condition3.signal();
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
public void printC() {
lock.lock();
try {
while (number != 3) {
condition3.await();
尚硅谷JUC高并发编程学习笔记JUC简介与Lock接口
Java多线程之JUC包:ReentrantReadWriteLock源码学习笔记
[原创]java WEB学习笔记61:Struts2学习之路--通用标签 property,uri,param,set,push,if-else,itertor,sort,date,a标签等(代码片段