实现了lock的类为:ReentrantLock
接口的方式解释:
lock()方法为获取锁对象,如果未获取到锁就一直获取锁。
trylock():为布尔值,返回是否获取到了锁,如果没有获取到锁则返回false,如果获取到了则返回true
tryLock(long timeout, TimeUnit unit):表示在指定的时间内获取锁,如果未获取到,则返回false,否则返回true
unlock():为释放锁,使其他线程有机会执行。
lockInterruptibly():表示获取锁,如果线程被中断则抛出异常。
例如代码实例:
package TestThread.ThreadLockDemo; import java.util.concurrent.locks.ReentrantLock; public class TestLock {
public static void main(String[] args) {
ReentrantLock lock = new ReentrantLock();// 初始化lock对象
Test2 test2 = new Test2("苹果", 100);// 初始化苹果的数量
Test1 test1 = new Test1(lock, 10, test2);// 初始化线程对象
Thread t1 = new Thread(test1, "线程1");// 创建线程
Thread t2 = new Thread(test1, "线程2");
Thread t3 = new Thread(test1, "线程3");
t1.start();// 启动线程
t2.start();
t3.start();
}
} class Test1 implements Runnable {
private int count;
private ReentrantLock lock;
private Test2 test2; public Test1(ReentrantLock lock, int count, Test2 test2) {
this.lock = lock;
this.count = count;
this.test2 = test2;
} @Override
public void run() {
lock.lock();// 枷锁
try {
test2.DiscountApple(count);
} catch (Exception e) {
System.out.print("调用卖苹果的方法发生异常!");
} finally {
lock.unlock();// 解锁
}
}
} class Test2 {
private String name;
int count; /**
* @param name苹果的名字
* @param count初始化苹果的数量
*/
public Test2(String name, int count) {
this.name = name;
this.count = count;
} /**
* * @author 作者 E-mail:
*
* @date 创建时间:2017年3月24日 下午1:13:14
* @version 1.0
* @parameter
* @since
* @return
*/
public void DiscountApple(int discount) {
this.count = this.count - discount;
System.out.println(Thread.currentThread().getName() + ":苹果的数量为:" + this.count + ",卖掉了" + discount);
}
}
实例结果:
trylock()方法的使用演示:
package TestThread.ThreadLockDemo; import java.util.concurrent.locks.ReentrantLock; public class TestLock {
public static void main(String[] args) {
ReentrantLock lock = new ReentrantLock();// 初始化lock对象
Test2 test2 = new Test2("苹果", 100);// 初始化苹果的数量
Test1 test1 = new Test1(lock, 10, test2);// 初始化线程对象
Thread t1 = new Thread(test1, "线程1");// 创建线程
Thread t2 = new Thread(test1, "线程2");
Thread t3 = new Thread(test1, "线程3");
t1.start();// 启动线程
t2.start();
t3.start();
}
} class Test1 implements Runnable {
private int count;
private ReentrantLock lock;
private Test2 test2; public Test1(ReentrantLock lock, int count, Test2 test2) {
this.lock = lock;
this.count = count;
this.test2 = test2;
} @Override
public void run() {
if (lock.tryLock()) {
// lock.lock();// 枷锁
try {
test2.DiscountApple(count);
} catch (Exception e) {
System.out.print("调用卖苹果的方法发生异常!");
} finally {
lock.unlock();// 解锁
}
} else {
System.out.println(Thread.currentThread().getName() + "未获取到锁");
}
}
} class Test2 {
private String name;
int count; /**
* @param name苹果的名字
* @param count初始化苹果的数量
*/
public Test2(String name, int count) {
this.name = name;
this.count = count;
} /**
* * @author 作者 E-mail:
*
* @date 创建时间:2017年3月24日 下午1:13:14
* @version 1.0
* @parameter
* @since
* @return
*/
public void DiscountApple(int discount) {
this.count = this.count - discount;
System.out.println(Thread.currentThread().getName() + ":苹果的数量为:" + this.count + ",卖掉了" + discount);
}
}
测试结果为:
unlock() |