自制悲观锁
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