Prince_ZaZa
Go big or go home
posts - 33,comments - 0,views - 7039

Synchronized

public class Test01 {
public static void main(String[] args) {
Ticket ticket = new Ticket();
new Thread(() -> {
for (int i = 0; i < 30; i++) {
ticket.sale();
}
}, "A").start();
new Thread(() -> {
for (int i = 0; i < 30; i++) {
ticket.sale();
}
}, "A").start();
new Thread(() -> {
for (int i = 0; i < 30; i++) {
ticket.sale();
}
}, "A").start();
}
//资源类 属性、方法
//OOP的思想
static class Ticket {
private int num = 30;
public synchronized void sale() {
if (num > 0) {
System.out.println(Thread.currentThread().getName() + "卖出了" + (num--) + "票,剩余" + num + "票");
}
}
}
}

Lock

public class Test03 {
public static void main(String[] args) {
Ticket ticket = new Ticket();
new Thread(() -> {for (int i = 0; i < 30; i++) {ticket.sale();}}, "A").start();
new Thread(() -> {for (int i = 0; i < 30; i++) {ticket.sale();}}, "B").start();
}
//资源类 属性、方法
//OOP的思想
static class Ticket {
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 + "票");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
}
}

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

Lock和Synchronized方式实现传统的消费者与生产者模式

public class Test02 {
public static void main(String[] args) {
Data data=new Data();
new Thread(()->{
for (int i=0;i<10;i++){
try {
data.increment();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},"A").start();
new Thread(()->{
for (int i=0;i<10;i++){
try {
data.decrement();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},"B").start();
new Thread(()->{
for (int i=0;i<10;i++){
try {
data.increment();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},"C").start();
new Thread(()->{
for (int i=0;i<10;i++){
try {
data.decrement();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},"D").start();
}
static class Data{
int num=0;
public synchronized void increment() throws InterruptedException {
while(num!=0){
//不等与0的时候就等待
this.wait();
}
//否则
num++;
System.out.println(Thread.currentThread().getName()+"=>"+num);
this.notifyAll();
}
public synchronized void decrement() throws InterruptedException {
while(num==0){
this.wait();
}
num--;
System.out.println(Thread.currentThread().getName()+"=>"+num);
this.notifyAll();
}
}
}

以上是Synchronized方式的,Lock方式与其模式相似,只需将wait改为await,将notifyAll改为signal。
另外,Lock方式可以使用condition监视器实现精准通知,每个监视器可以只监视一个线程。

posted on   Prince_ZaZa  阅读(19)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5

点击右上角即可分享
微信分享提示