三种锁(卖票、单例)练习

三种同步方式 实现 卖票

同步代码块

public class 同步代码块 {
	public static void main(String[] args) {
		Ticket ticket = new Ticket();
		Thread t = new Thread(ticket,"窗口一");
		Thread t2 = new Thread(ticket,"窗口二");
		Thread t3 = new Thread(ticket,"窗口三");
		Thread t4 = new Thread(ticket,"窗口四");
		t.start();
		t2.start();
		t3.start();
		t4.start();
	}
}

class Ticket implements Runnable{
	int ticket = 100;
	@Override
	public void run() {
		while(true) {
			synchronized (this) {
				if(ticket>0) {
					ticket--;
					System.out.println(Thread.currentThread().getName()+"卖了一张,还剩"+ticket+"张");
				}
			}
		}	
	}
}

同步方法

public class 同步方法 {
	public static void main(String[] args) {
		Ticket2 ticket2 = new Ticket2();
		Thread t = new Thread(ticket2,"窗口一");
		Thread t2 = new Thread(ticket2,"窗口二");
		Thread t3 = new Thread(ticket2,"窗口三");
		Thread t4 = new Thread(ticket2,"窗口四");
		t.start();
		t2.start();
		t3.start();
		t4.start();
	}

}
class Ticket2 implements Runnable{
	static int ticket2 = 1000;
	@Override
	public void run() {
//		for (int i = 0; i < ticket2; i++) {
//			a();
//		}
		while(true) {
			a();
			if(ticket2==0) {
				break;
			}
		}
		
	}
	
	public static synchronized void a() {
		if(ticket2>0) {
			ticket2--;
			System.out.println(Thread.currentThread().getName()+"卖了一张,还剩"+ticket2+"张");
		}
	}
}

Lock显式

public class Lock显式 {
	public static void main(String[] args) {
		Ticket3 ticket = new Ticket3();
		Thread t = new Thread(ticket,"窗口一");
		Thread t2 = new Thread(ticket,"窗口二");
		Thread t3 = new Thread(ticket,"窗口三");
		Thread t4 = new Thread(ticket,"窗口四");
		t.start();
		t2.start();
		t3.start();
		t4.start();
	}

}
class Ticket3 implements Runnable{
	int ticket3 = 100;
	
	Lock lock = new ReentrantLock();
	
	@Override
	public void run() {
		
		while(true) {
			try {
				lock.lock();
				if(ticket3>0) {
					ticket3--;
					System.out.println(Thread.currentThread().getName()+"卖出一张,还剩"+ticket3+"张");
				}else {
					break;
				}
				
			} finally {
				lock.unlock();
			}	
		}
	}
}

2. 三种同步方式 单例

同步代码块

懒汉式类

class LazyInstance {
	private LazyInstance() {
		
	}
	
	private static LazyInstance instance = null;
	
	public static LazyInstance getInstance() {
		if(instance==null) {
			synchronized (LazyInstance.class) {
				if(instance==null) {
					instance = new LazyInstance();
				}
			}
		}
		return instance;
	}
}
public class 同步锁代码块 {
	public static void main(String[] args) {
		MyThread m = new MyThread();
		MyThread m2 = new MyThread();
		MyThread m3 = new MyThread();
		MyThread m4 = new MyThread();
		
		m.start();
		m2.start();
		m3.start();
		m4.start();
	}

}

class MyThread extends Thread{
	@Override
	public void run() {
		LazyInstance instance = LazyInstance.getInstance();
		System.out.println(instance.hashCode());
	}
}

同步方法

public class 同步方法 {
	public static void main(String[] args) {
		MyThread2 m = new MyThread2();
		MyThread2 m2 = new MyThread2();
		MyThread2 m3 = new MyThread2();
		MyThread2 m4 = new MyThread2();
		
		m.start();
		m2.start();
		m3.start();
		m4.start();
	}

}
class MyThread2 extends Thread{
	@Override
	public void run() {
		a();
	}
	
	public synchronized void a() {
		LazyInstance instance = LazyInstance.getInstance();
		System.out.println(instance.hashCode());
	}
}

lock显式

public class Lock显式 {
	public static void main(String[] args) {
		MyThread3 m = new MyThread3();
		MyThread3 m2 = new MyThread3();
		MyThread3 m3 = new MyThread3();
		MyThread3 m4 = new MyThread3();
		
		m.start();
		m2.start();
		m3.start();
		m4.start();
	}

}
class MyThread3 extends Thread{
	Lock lock = new ReentrantLock();
	@Override
	public void run() {
		try {
			lock.lock();
			LazyInstance instance = LazyInstance.getInstance();
			System.out.println(instance.hashCode());
		} finally {
			lock.unlock();
		}
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值