Java ~ multi-threaded algorithm actual combat problem store ticket sales

Article Directory


PS: I encountered this question in the interview, so there is no link. If I think this question is very interesting, share it with me

Title description

There is a store with 100 tickets, and there are three ticket booths. The three ticket booths are independent and each sells its own. However, the tickets are not independent, and each ticket cannot be sold repeatedly.

Note:
All 100 tickets must be sold, but no more.
Each window sells a ticket every 100 milliseconds.

Requirement:
output which window each ticket was sold by

Ideas

This is a typical multi-threaded safety issue, because once the ticket is improperly operated, it will be sold repeatedly

So the key point of this question is that the two operations of checking how many tickets are left at the time and getting the tickets must be synchronized at each ticket office.

Then we use a synchronization method to ensure the synchronization of the above two operations.

class Store {
    
    

    private volatile int ticket = 1;

    /**
     * A售票口
     * @throws InterruptedException
     */
    public void A() throws InterruptedException {
    
    

        int temp;
        while ((temp = getSetTicket()) != -1) {
    
    
            System.out.println(temp + " A");
            Thread.sleep(100);
        }
    }

    /**
     * B售票口
     * @throws InterruptedException
     */
    public void B() throws InterruptedException {
    
    
        
        int temp;
        while ((temp = getSetTicket()) != -1) {
    
    
            System.out.println(temp + " B");
            Thread.sleep(100);
        }
    }

    /**
     * 售票口
     * @throws InterruptedException
     */
    public void C() throws InterruptedException {
    
    
        
        int temp;
        while ((temp = getSetTicket()) != -1) {
    
    
            System.out.println(temp + " C");
            Thread.sleep(100);
        }
    }

    /**
     * 查看票和取票的同步操作
     * @return
     */
    public synchronized int getSetTicket() {
    
    
        int temp = this.ticket;

        //查看此时票是否能卖出
        if (temp > 100) {
    
    
            return -1;
        }
        
        //取票
        this.ticket++;
        return temp;
    }
}

test

Enable three threads to call three windows at the same time

    public static void main(String[] args) throws InterruptedException {
    
    
        Store store = new Store();
        new Thread(new Runnable() {
    
    
            @Override
            public void run() {
    
    
                try {
    
    
                    store.A();
                } catch (InterruptedException e) {
    
    
                    e.printStackTrace();
                }
            }
        }).start();

        new Thread(new Runnable() {
    
    
            @Override
            public void run() {
    
    
                try {
    
    
                    store.B();
                } catch (InterruptedException e) {
    
    
                    e.printStackTrace();
                }
            }
        }).start();

        new Thread(new Runnable() {
    
    
            @Override
            public void run() {
    
    
                try {
    
    
                    store.C();
                } catch (InterruptedException e) {
    
    
                    e.printStackTrace();
                }
            }
        }).start();

        Thread.sleep(1000);
    }

Test Results

1 A
2 B
3 C
4 A
6 C
5 B
7 A
8 C
9 B
10 A
11 C
12 B
13 A
14 C
15 B
16 A
17 C
18 B
19 A
20 C
21 B
22 A
23 C
24 B
25 A
26 C
27 B
28 A
29 C
30 B
31 A
33 B
32 C
34 A
35 C
36 B
37 A
38 C
39 B
40 A
41 B
42 C
43 A
45 C
44 B
46 A
48 B
47 C
49 A
50 C
51 B
52 A
53 C
54 B
55 A
56 B
57 C
58 A
59 B
60 C
61 A
62 B
63 C
64 A
66 B
65 C
67 A
68 B
69 C
70 A
72 C
71 B
73 A
74 C
75 B
76 A
77 C
78 B
79 A
80 C
81 B
82 A
83 C
84 B
85 A
87 B
86 C
88 A
89 B
90 C
91 A
92 B
93 C
94 A
95 B
96 C
97 A
98 B
99 C
100 A

Process finished with exit code 0

Guess you like

Origin blog.csdn.net/Shangxingya/article/details/114366295