多线程转账代码示例

网友投稿 813 2022-05-29

package com.concurrent.test4; import lombok.extern.slf4j.Slf4j; import java.util.Random; @Slf4j(topic = "c.test11:") /** *买票问题 */ public class Test15 { public static void main(String[] args) throws InterruptedException { //创建两个账户相互转账,每人开始拥有1000快 Account a = new Account(1000); Account b = new Account(1000); Thread t1 = new Thread(() -> { for (int i = 0; i < 1000; i++) { //a给b转账1000次,每次金额随机 a.transfer(b, randomAmount()); } }, "t1"); Thread t2 = new Thread(() -> { for (int i = 0; i < 1000; i++) { //b给a转账1000次,每次金额随机 b.transfer(a, randomAmount()); } }, "t2"); t1.start(); t2.start(); t1.join(); t2.join(); // 查看转账2000次后的总金额 log.debug("total:{}",(a.getMoney() + b.getMoney())); } // Random 为线程安全 static Random random = new Random(); // 随机 1~100 public static int randomAmount() { return random.nextInt(100) +1; } } class Account {//账户类 private int money;//初始化金额 public Account(int money) { this.money = money; } public int getMoney() { return money; } public void setMoney(int money) { this.money = money; } public synchronized void transfer(Account target, int amount) { //如果加synchronized还是不安全的,因为加在方法上等于保护的是自己的共享变量 //但是此处有两个共享变量this.money和target.money //所以这儿应该用synchronized锁住类对象,因为这俩共享变量是一个类中 synchronized (Account.class){ if (this.money > amount) { this.setMoney(this.getMoney() - amount); target.setMoney(target.getMoney() + amount); } } } }

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

任务调度 多线程

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Java进阶(四十三)线程与进程的区别
下一篇:☕【Java深层系列】「并发编程系列」让我们一起探索一下CountDownLatch的技术原理和源码分析
相关文章