Java多线程同步:死锁示例

原创 Dear 丶 2024-12-10 19:51 100阅读 0赞

在Java多线程编程中,死锁是一种常见的并发问题。当两个或更多的线程相互等待对方释放资源时,就会发生死锁。

以下是一个简单的Java死锁示例:

  1. // 创建两个互斥的资源
  2. class Resource {
  3. private boolean locked;
  4. Resource() {
  5. locked = false;
  6. }
  7. // 锁定资源,如果已被锁定则返回false
  8. synchronized void lock() {
  9. if (locked) {
  10. System.out.println("Resource is already locked by another thread.");
  11. return;
  12. }
  13. locked = true;
  14. System.out.println("Resource " + this + " is locked by thread " + Thread.currentThread().getName());
  15. }
  16. // 解锁资源
  17. synchronized void unlock() {
  18. if (!locked) {
  19. System.out.println("Resource is already unlocked by another thread.");
  20. return;
  21. }
  22. locked = false;
  23. System.out.println("Resource " + this + " is unlocked by thread " + Thread.currentThread().getName());
  24. }
  25. }
  26. // 创建两个线程,分别尝试获取资源
  27. class ThreadA extends Thread {
  28. Resource resource;
  29. ThreadA(Resource res) {
  30. resource = res;
  31. }
  32. public void run() {
  33. try {
  34. System.out.println("Thread A starting to acquire resource " + resource);
  35. resource.lock(); // 线程A尝试锁定资源
  36. System.out.println("Thread A successfully acquired resource " + resource);
  37. Thread.sleep(2000); // 模拟长时间操作,防止死锁的发生
  38. } catch (InterruptedException e) {
  39. e.printStackTrace();
  40. } finally {
  41. try {
  42. if (resource.isLocked()) { // 确认资源是否已被其他线程锁定
  43. System.out.println("Thread A detected that resource " + resource + " is still locked by another thread.");
  44. resource.unlock(); // 解锁资源,释放其他线程等待的锁
  45. }
  46. else {
  47. System.out.println("Resource " + resource + " was already unlocked by another thread.");
  48. }
  49. } catch (Exception e) {
  50. e.printStackTrace();
  51. }
  52. System.out.println("Thread A ending.");
  53. }
  54. }
  55. class ThreadB extends Thread {
  56. Resource resource;
  57. ThreadB(Resource res) {
  58. resource = res;
  59. }
  60. public void run() {
  61. try {
  62. System.out.println("Thread B starting to acquire resource " + resource);
  63. resource.lock(); // 线程B尝试锁定资源
  64. System.out.println("Thread B successfully acquired resource " + resource);
  65. Thread.sleep(2000); // 模拟长时间操作,防止死锁的发生
  66. } catch (InterruptedException e) {
  67. e.printStackTrace();
  68. } finally {
  69. try {
  70. if (resource.isLocked()) { // 确认资源是否已被其他线程锁定
  71. System.out.println("Thread B detected that resource " + resource + " is still locked by another thread.");
  72. resource.unlock(); // 解锁资源,释放其他线程等待的锁
  73. }
  74. else {
  75. System.out.println("Resource " + resource + " was already unlocked by another thread.");
  76. }
  77. } catch (Exception e) {
  78. e.printStackTrace();
  79. }
  80. System.out.println("Thread B ending.");
  81. }
  82. }
  83. // 创建两个互斥资源
  84. Resource resource1 = new Resource();
  85. Resource resource2 = new Resource();
  86. // 启动两个线程,分别尝试获取资源
  87. new ThreadA(resource1).start();
  88. new ThreadB(resource2).start();

这个例子中,如果两个线程都试图获取resource1resource2中的一个资源,就会发生死锁。为防止这种情况,通常会设置超时时间或者使用其他的同步机制。

文章版权声明:注明蒲公英云原创文章,转载或复制请以超链接形式并注明出处。

发表评论

表情:
评论列表 (有 0 条评论,100人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Java线同步示例

    在Java多线程编程中,死锁是一种常见的并发问题。当两个或更多的线程相互等待对方释放资源时,就会发生死锁。 以下是一个简单的Java死锁示例: ```java // 创建两