前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >自制悲观锁

自制悲观锁

作者头像
马克java社区
修改2021-10-11 10:24:04
4750
修改2021-10-11 10:24:04
举报
文章被收录于专栏:java大数据

i)自制悲观锁:

例 2.2.1.1

package com;

public class Ticket_Pess_MarkToWin {

private int number=4;

private int lock=0;

public synchronized int getLock()

{

System.out.println("最后还剩"+number);

}else{

System.out.println("不够了,买几张失败");

}

}

}

package com;

class MulThreMarkToWin extends Thread {

Ticket_Pess_MarkToWin ticPes_MarkToWin;

public MulThreMarkToWin(Ticket_Pess_MarkToWin ticPes_MarkToWin) {

System.out.println("没拿到锁,没干成");

}

}

}

class MulThreMarkToWinBatch extends Thread {

Ticket_Pess_MarkToWin ticPes_MarkToWin;

public MulThreMarkToWinBatch(Ticket_Pess_MarkToWin ticPes_MarkToWin) {

this.ticPes_MarkToWin=ticPes_MarkToWin;

}

public void run() {

}

}

public class TestConcurBuy_MarkToWin {

public static void main(String[] args) {

Ticket_Pess_MarkToWin ticPes_MarkToWin=new Ticket_Pess_MarkToWin();

Thread t1 = new MulThreMarkToWin(ticPes_MarkToWin);

Thread t2 = new MulThreMarkToWinBatch(ticPes_MarkToWin);

Thread t3 = new MulThreMarkToWin(ticPes_MarkToWin);

t1.start();

t2.start();

t3.start();

}

}

更多请看:https://blog.csdn.net/qq_44638460/article/details/104135510

本文系转载,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文系转载前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档