【生意多】-免费发布分类信息
当前位置: 首页 » 新闻 » 教程 » 操作系统 » 正文

怎么解决线程同步(实现线程同步的几种方式)

放大字体  缩小字体 发布日期:2022-06-30 15:06:32    浏览次数:15

  Java线程同步是保证多线程状态下安全访问竞争资源的一种编程手段,但线程的同步在Java多线程编程中算是一个比较难的难点,很多开发者甚至都不知道什么是竞争资源,也不知道时候需要进行线程同步,当然这是没有明确答案的,一些原则问题还是要考虑。

  Java如何实现线程同步?

  对于同步,在具体的Java代码中需要完成一下两个操作:

  1、把竞争访问的资源标识为private;

  2、同步哪些修改变量的代码,使用synchronized关键字同步方法或代码。

  当然这不是唯一控制并发安全的途径。

  synchronized 关键字使用说明

  synchronized 只能标记非抽象的方法,不能标识成员变量。

  为了演示同步方法的使用,构建了一个信用卡账户,起初信用额为100w,然后模拟透支、存款等多个操作。显然银行账户User对象是个竞争资源,而多个并发操作的是账户方法oper(int x),当然应该在此方法上加上同步,并将账户的余额设为私有变量,禁止直接访问。

  1. 01
  2. 06public class Test {
  3. 07public static void main(String[] args) {
  4. 08User u = new User("张三", 100);
  5. 09MyThread t1 = new MyThread("线程A", u, 20);
  6. 10MyThread t2 = new MyThread("线程B", u, -60);
  7. 11MyThread t3 = new MyThread("线程C", u, -80);
  8. 12MyThread t4 = new MyThread("线程D", u, -30);
  9. 13MyThread t5 = new MyThread("线程E", u, 32);
  10. 14MyThread t6 = new MyThread("线程F", u, 21);
  11. 15t1.start();
  12. 16t2.start();
  13. 17t3.start();
  14. 18t4.start();
  15. 19t5.start();
  16. 20t6.start();
  17. 21}
  18. 22}
  19. 23class MyThread extends Thread {
  20. 24private User u;
  21. 25private int y = 0;
  22. 26MyThread(String name, User u, int y) {
  23. 27super(name);
  24. 28this.u = u;
  25. 29this.y = y;
  26. 30}
  27. 31public void run() {
  28. 32u.oper(y);
  29. 33}
  30. 34}
  31. 35class User {
  32. 36private String code;
  33. 37private int cash;
  34. 38User(String code, int cash) {
  35. 39this.code = code;
  36. 40this.cash = cash;
  37. 41}
  38. 42public String getCode() {
  39. 43return code;
  40. 44}
  41. 45public void setCode(String code) {
  42. 46this.code = code;
  43. 47}
  44. 48
  45. 52public synchronized void oper(int x) {
  46. 53try {
  47. 54Thread.sleep(10L);
  48. 55this.cash += x;
  49. 56System.out.println(Thread.currentThread().getName() + "运行结束,增加“" + x + "”,当前用户账户余额为:" + cash);
  50. 57Thread.sleep(10L);
  51. 58} catch (InterruptedException e) {
  52. 59e.printStackTrace();
  53. 60}
  54. 61}
  55. 62@Override
  56. 63public String toString() {
  57. 64return "User{" +
  58. 65"code='" + code + ''' +
  59. 66", cash=" + cash +
  60. 67'}';
  61. 68}
  62. 69}
复制代码

public class Test {public static void main(String[] args) {User u = new User("张三", 100);MyThread t1 = new MyThread("线程A", u, 20);MyThread t2 = new MyThread("线程B", u, -60);MyThread t3 = new MyThread("线程C", u, -80);MyThread t4 = new MyThread("线程D", u, -30);MyThread t5 = new MyThread("线程E", u, 32);MyThread t6 = new MyThread("线程F", u, 21);t1.start();t2.start();t3.start();t4.start();t5.start();t6.start();}}class MyThread extends Thread {private User u;private int y = 0;MyThread(String name, User u, int y) {super(name);this.u = u;this.y = y;}public void run() {u.oper(y);}}class User {private String code;private int cash;User(String code, int cash) {this.code = code;this.cash = cash;}public String getCode() {return code;}public void setCode(String code) {this.code = code;}public synchronized void oper(int x) {try {Thread.sleep(10L);this.cash += x;System.out.println(Thread.currentThread().getName() + "运行结束,增加“" + x + "”,当前用户账户余额为:" + cash);Thread.sleep(10L);} catch (InterruptedException e) {e.printStackTrace();}}@Overridepublic String toString() {return "User{" +"code='" + code + ''' +", cash=" + cash +'}';}}

 
(文/小编)
打赏
免责声明
• 
本文为小编原创作品,作者: 小编。欢迎转载,转载请注明原文出处:https://www.31duo.com/news/show-3569655.html 。本文仅代表作者个人观点,本站未对其内容进行核实,请读者仅做参考,如若文中涉及有违公德、触犯法律的内容,一经发现,立即删除,作者需自行承担相应责任。涉及到版权或其他问题,请及时联系我们。
 

(c)2016-2019 31DUO.COM All Rights Reserved浙ICP备19001410号-4

浙ICP备19001410号-4