1. import java.util.ArrayList;
    2. import java.util.List;
    3. import java.util.UUID;
    4. /**
    5. * @author zhenlong
    6. * @version $Id: PC.java, v 0.1 2020年05月11日 12:48 AM zhenlong Exp $
    7. */
    8. public class PC {
    9. public static void main(String[] args) {
    10. Resource resource = new Resource(10);
    11. //生产者线程
    12. ProducerThread p1 = new ProducerThread(resource, "生产者1号");
    13. ProducerThread p2 = new ProducerThread(resource, "生产者2号");
    14. ProducerThread p3 = new ProducerThread(resource, "生产者3号");
    15. //消费者线程
    16. ConsumerThread c1 = new ConsumerThread(resource, "消费者1号");
    17. //ConsumerThread c2 = new ConsumerThread(resource);
    18. //ConsumerThread c3 = new ConsumerThread(resource);
    19. p1.start();
    20. p2.start();
    21. p3.start();
    22. c1.start();
    23. //c2.start();
    24. //c3.start();
    25. }
    26. // 公共资源类
    27. static class Resource {
    28. // 锁
    29. private final Object lock;
    30. // 当前数量
    31. private List<String> taskList;
    32. // 任务上限
    33. private int capacity;
    34. public Resource(int capacity) {
    35. this.capacity = capacity;
    36. this.taskList = new ArrayList<>();
    37. this.lock = new Object();
    38. }
    39. public void consume() throws Exception {
    40. synchronized (lock) {
    41. if (taskList.size() > 0) {
    42. String task = taskList.remove(0);
    43. System.out.println(Thread.currentThread().getName() +
    44. "处理任务:" + task + "完成,当前池中任务数量:" + taskList.size());
    45. //通知生产者生产资源
    46. lock.notifyAll();
    47. } else {
    48. //如果没有资源,则消费者进入等待状态
    49. System.out.println(Thread.currentThread().getName() + "线程进入等待");
    50. lock.wait();
    51. }
    52. }
    53. }
    54. /**
    55. * 向资源池中添加资源
    56. */
    57. public void produce() throws Exception {
    58. synchronized (lock) {
    59. if (taskList.size() < capacity) {
    60. taskList.add(UUID.randomUUID().toString());
    61. System.out.println(Thread.currentThread().getName()
    62. + "生成任务,当前池中任务数量:" + taskList.size());
    63. //通知等待的消费者
    64. lock.notifyAll();
    65. } else {
    66. // 队列已满计入等到
    67. System.out.println(Thread.currentThread().getName() + "线程进入等待");
    68. lock.wait();
    69. }
    70. }
    71. }
    72. }
    73. // 消费者线程
    74. static class ConsumerThread extends Thread {
    75. private Resource resource;
    76. public ConsumerThread(Resource resource, String name) {
    77. this.resource = resource;
    78. super.setName(name);
    79. }
    80. @Override
    81. public void run() {
    82. while (true) {
    83. try {
    84. Thread.sleep((long) (Math.random() * 1000));
    85. resource.consume();
    86. } catch (Exception e) {
    87. e.printStackTrace();
    88. }
    89. }
    90. }
    91. }
    92. // 生产者线程
    93. static class ProducerThread extends Thread {
    94. private Resource resource;
    95. public ProducerThread(Resource resource, String name) {
    96. this.resource = resource;
    97. super.setName(name);
    98. }
    99. @Override
    100. public void run() {
    101. //不断地生产资源
    102. while (true) {
    103. try {
    104. Thread.sleep((long) (Math.random() * 1000));
    105. resource.produce();
    106. } catch (Exception e) {
    107. e.printStackTrace();
    108. }
    109. }
    110. }
    111. }
    112. }