package cn.itcast.demo2;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/*
-
使用JDK1.5 的接口Lock,替换同步代码块,实现线程的安全性
-
Lock接口方法:
-
lock() 获取锁
-
unlock()释放锁
-
实现类ReentrantLock
*/
public class Tickets implements Runnable{//定义出售的票源
private int ticket = 100;
//在类的成员位置,创建Lock接口的实现类对象
private Lock lock = new ReentrantLock();public void run(){
while(true){
//调用Lock接口方法lock获取锁
lock.lock();
//对票数判断,大于0,可以出售,变量–操作
if( ticket > 0){
try{
Thread.sleep(10);
System.out.println(Thread.currentThread().getName()+" 出售第 "+ticket–);
}catch(Exception ex){}finally{ //释放锁,调用Lock接口方法unlock lock.unlock(); } } }
}
}
package cn.itcast.demo2;
/*
-
多线程并发访问同一个数据资源
-
3个线程,对一个票资源,出售
*/
public class ThreadDemo {
public static void main(String[] args) {
//创建Runnable接口实现类对象
Tickets t = new Tickets();
//创建3个Thread类对象,传递Runnable接口实现类
Thread t0 = new Thread(t);
Thread t1 = new Thread(t);
Thread t2 = new Thread(t);t0.start();t1.start();t2.start();
}
}