多线程县交互实例,生产消费

生产者消费者问题是一个非常典型性的线程交互的问题。

1. 使用来存放数据
1.1 把栈改造为支持线程安全
1.2 把栈的边界操作进行处理,当栈里的数据是0的时候,访问pull的线程就会等待。 当栈里的数据时200的时候,访问push的线程就会等待
2. 提供一个生产者(Producer)线程类,生产随机大写字符压入到堆栈
3. 提供一个消费者(Consumer)线程类,从堆栈中弹出字符并打印到控制台
4. 提供一个测试类,使两个生产者和三个消费者线程同时运行,结果类似如下 :

练习-生产者消费者问题

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

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

import java.util.*;

public class Test {

    public static void main(String[] args){

        MyStack<Character> stack = new MyStack<>();

        for (int i = 0; i < 3; ++i) {

            new Thread(new Producer(stack)).start();//参数为一个带有runnable接口改写了run方法的对象

        }

        for (int i = 0; i < 2; ++i) {

            new Thread(new Consumer(stack)).start();

        }

    }   

}

class MyStack<T> {

    private LinkedList<T> list;

    private int count;

    public MyStack() {

        list = new LinkedList<>();

        count = 0;

    }

    public synchronized int getSize() {

        return count;

    }

    public synchronized void push(T ele) {

        while (count >= 200) {

            try {

                this.wait();

            catch (InterruptedException e) {

                e.printStackTrace();

            }

        }

        list.offerLast(ele);

        count++;

        this.notifyAll();

    }

    public synchronized T pull() {

        while (count <= 0) {

            try {

                this.wait();

            catch (InterruptedException e) {

                e.printStackTrace();

            }

        }

        T res =  list.pollLast();

        count--;

        this.notifyAll();

        return res;

    }

    // return null if stack is empty

    public synchronized T peek() {

        return list.peekLast();

    }

}

class Producer implements Runnable {

    private MyStack<Character> stack;

    public Producer(MyStack<Character> stack) {

        this.stack = stack;

    }

    @Override

    public void run() {

        while (true) {

            try {

                Thread.sleep(100);

            catch (InterruptedException e) {

                e.printStackTrace();

            }

            char c = generateChar();

            stack.push(c);

            System.out.println("Producer pushed: " + c + " " + stack.getSize());

        }

    }

    private char generateChar() {

        return (char)(new Random().nextInt(26) + 'a');//随机生成英文

    }

}

class Consumer implements Runnable {

    private MyStack<Character> stack;

    public Consumer(MyStack<Character> stack) {

        this.stack = stack;

    }

    @Override

    public void run() {

        while (true) {

            try {

                Thread.sleep(100);

            catch (InterruptedException e) {

                e.printStackTrace();

            }

             

            System.out.println("Consumer pulled: " + stack.pull()" " + stack.getSize());

        }

    }

}

猜你喜欢

转载自blog.csdn.net/Whiteleaf3er/article/details/82703565
今日推荐