利用线程锁实现窗口卖票

版权声明: https://blog.csdn.net/m0_37682436/article/details/80661426

synchronized关键字  

实现Runnable接口,加锁可以用

    Object obj = new Object();

    this

    lock

因为操作的是同一个对象 win1

package com.thread;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Thick1 {
	public static void main(String[] args) {
		Window1 win1 = new Window1();
		Thread t1 = new Thread(win1);
		Thread t3 = new Thread(win1);
		Thread t2 = new Thread(win1);

		t1.setName("窗口1");
		t2.setName("窗口2");
		t3.setName("窗口3");

		t1.start();
		t2.start();
		t3.start();
	}
}

class Window1 implements Runnable {
	private static int ticket = 100;
	// 创建锁对象
	Object obj = new Object();

	@Override
	public void run() {
		while (true) {
			synchronized (obj) {
				if (ticket > 0) {
					try {
						Thread.sleep(10);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					System.out.println(Thread.currentThread().getName() + "售票: " + ticket--);
				} else {
					break;
				}
			}

		}

	}
}
private Lock lock = new ReentrantLock();
虽然我们可以理解同步代码块和同步方法的锁对象问题,但是我们并没有直接看到在哪里加上了锁,在哪里释放了锁, 为了更清晰的表达如何加锁和释放锁,JDK5以后提供了一个新的锁对象Lock
  Lock:
      void lock():获取锁
     void unlock():释放锁
 ReentrantLock是Lock的实现类
package com.thread;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

class SellTicket implements Runnable {

	private int tickets = 100;

	// 定义锁对象
	private Lock lock = new ReentrantLock();

	@Override
	public void run() {
		while (true) {
			try {
				// 加锁
				lock.lock();
				if (tickets > 0) {
					try {
						Thread.sleep(100);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					System.out.println(Thread.currentThread().getName() + "正在出售第" + (tickets--) + "张票");
				}
			} finally {
				// 释放锁
				lock.unlock();
			}
		}
	}

}

/*
 * 虽然我们可以理解同步代码块和同步方法的锁对象问题,但是我们并没有直接看到在哪里加上了锁,在哪里释放了锁,
 * 为了更清晰的表达如何加锁和释放锁,JDK5以后提供了一个新的锁对象Lock
 * 
 * Lock: void lock():获取锁 void unlock():释放锁 ReentrantLock是Lock的实现类
 */
public class SellTicketDemo {

	public static void main(String[] args) {
		// 创建资源对象
		SellTicket st = new SellTicket();

		// 创建三个窗口
		Thread t1 = new Thread(st, "窗口1");
		Thread t2 = new Thread(st, "窗口2");
		Thread t3 = new Thread(st, "窗口3");

		// 启动线程
		t1.start();
		t2.start();
		t3.start();
	}

}

继承Thread类实现加锁,不适用于this,因为是三个对象,可以在Object obj = new Object(); 前面加上static,

package com.thread;

public class Ticket {

	public static void main(String[] args) {
		Window win1 = new Window();
		Window win2 = new Window();
		Window win3 = new Window();

		win1.setName("窗口1");
		win2.setName("窗口2");
		win3.setName("窗口3");

		win1.start();
		win2.start();
		win3.start();

	}
}

class Window extends Thread {
	static int ticket = 100;
	static Object obj = new Object();

	@Override
	public synchronized void run() {
		while (true) {
			synchronized (obj) {
				if (ticket > 0) {
					try {
						Thread.sleep(10);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					System.out.println(Thread.currentThread().getName() + "售票" + ticket--);
				} else {
					break;
				}
			}
		}
	}
}

$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$





猜你喜欢

转载自blog.csdn.net/m0_37682436/article/details/80661426