使用Java编写并发问题案例

原创 àì夳堔傛蜴生んèń 2024-11-29 21:21 87阅读 0赞

在Java中,并发问题通常涉及到线程的创建、同步机制的使用以及可能出现的竞争条件等问题。下面提供一个简单的Java并发问题案例——生产者消费者问题。

  1. import java.util.concurrent.*;
  2. // 生产者类
  3. class Producer implements Runnable {
  4. private final int capacity;
  5. private producerLock lock;
  6. public Producer(int capacity) {
  7. this.capacity = capacity;
  8. lock = new producerLock();
  9. }
  10. @Override
  11. public void run() {
  12. while (true) {
  13. try {
  14. // 生产
  15. lock.acquire();
  16. System.out.println("Producer produces: " + Math.random());
  17. // 消费
  18. lock.release();
  19. System.out.println("Consumer consumes: " + Math.random());
  20. } catch (InterruptedException e) {
  21. Thread.currentThread().interrupt();
  22. break;
  23. }
  24. }
  25. }
  26. }
  27. // 消费者类
  28. class Consumer implements Runnable {
  29. private final int capacity;
  30. private consumerLock lock;
  31. public Consumer(int capacity) {
  32. this.capacity = capacity;
  33. lock = new consumerLock();
  34. }
  35. @Override
  36. public void run() {
  37. while (true) {
  38. try {
  39. // 生产
  40. lock.acquire();
  41. System.out.println("Consumer consumes: " + Math.random());
  42. // 消费
  43. lock.release();
  44. System.out.println("Producer produces: " + Math.random());
  45. } catch (InterruptedException e) {
  46. Thread.currentThread().interrupt();
  47. break;
  48. }
  49. }
  50. }
  51. }
  52. // 信号量锁类
  53. class producerLock implements Lock {
  54. private final int capacity;
  55. private AtomicInteger count;
  56. public producerLock(int capacity) {
  57. this.capacity = capacity;
  58. count = new AtomicInteger(capacity);
  59. }
  60. @Override
  61. public void lock() throws LockFailedException {
  62. while (count.getAndDec() < 0) {
  63. // 生产者数量超过容量,等待
  64. try {
  65. Thread.sleep(100); // 假设生产与消费的时间间隔为100ms
  66. } catch (InterruptedException e) {
  67. Thread.currentThread().interrupt();
  68. break;
  69. }
  70. }
  71. // 加锁成功,更新计数器
  72. count.setAndIncrement();
  73. }
  74. @Override
  75. public void unlock() throws UnlockFailedException {
  76. // 减锁,如果当前计数为0,则表示其他线程已经获取了锁,解锁失败
  77. if (count.decrementAndGet() < 0) {
  78. throw new UnlockFailedException("Failed to unlock. Lock count is negative.");
  79. }
  80. }
  81. @Override
  82. public boolean tryLock() throws锁定失败异常 {
  83. // 同理,尝试加锁,如果当前计数为0,则表示其他线程已经获取了锁,尝试加锁失败
  84. if (count.incrementAndGet() > capacity) {
  85. throw new UnlockFailedException("Failed to lock. Lock count is greater than the capacity.");
  86. }
  87. return true;
  88. }
  89. @Override
  90. public void lockInterruptibly() throws InterruptedException, 无法锁定异常 {
  91. tryLock();
  92. if (Thread.currentThread().isInterrupted()) {
  93. unlock();
  94. throw new InterruptedException("Interrupted while waiting to lock.");
  95. }
  96. }
  97. @Override
  98. public boolean tryLockInterruptibly() throws InterruptedException, 无法锁定异常 {
  99. return tryLock();
  100. if (Thread.currentThread().isInterrupted()) {
  101. unlock();
  102. throw new InterruptedException("Interrupted while waiting to lock.");
  103. }
  104. }
  105. }

以上代码创建了一个生产者和消费者模型,每个类都实现了一个Runnable接口,通过线程来模拟生产和消费的过程。在锁的使用上,我们采用了信号量的方式,保证了多线程环境下的同步问题。

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

发表评论

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

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

相关阅读