java中的 java.util.concurrent.locks.ReentrantLock类的使用方式 - 温柔的鲨鱼 - 博客园

实现了lock的类为:ReentrantLock

接口的方式解释:

lock()方法为获取锁对象,如果未获取到锁就一直获取锁。

trylock():为布尔值,返回是否获取到了锁,如果没有获取到锁则返回false,如果获取到了则返回true

tryLock(long timeout, TimeUnit unit):表示在指定的时间内获取锁,如果未获取到,则返回false,否则返回true

unlock():为释放锁,使其他线程有机会执行。

lockInterruptibly():表示获取锁,如果线程被中断则抛出异常。

例如代码实例:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

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()方法的使用演示:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

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()


原网址: 访问
创建于: 2023-02-22 11:26:58
目录: default
标签: 无

请先后发表评论
  • 最新评论
  • 总共0条评论