Thinking about two methods of multithreading ticket selling

Keywords: Java Swift socket

package com.swift.duoxiancheng;

class Ticket extends Thread {
    Ticket(String name) {
        super(name); // Constructor: set thread name
    }

    public static int ticket = 1000;

    @Override
    public void run() {

        while (true) {
            synchronized ("lock") {
                if (ticket > 0) {
                    ticket--;
                    System.out.println(Thread.currentThread().getName() + "Remaining votes:" + ticket);
                }
            }
        }
    }

    public synchronized void sellTickets() {

    }
}

public class ThreadTest_Jicheng {
    public static void main(String[] args) {
        // One is that multiple threads correspond to one object (such as ticket selling), the other is that multiple threads correspond to multiple objects (such as ticket selling) socket Thread)
        Ticket t1 = new Ticket("Ticket window 1");
        Ticket t2 = new Ticket("Ticket window 2");
        Ticket t3 = new Ticket("Ticket window 3");
        Ticket t4 = new Ticket("Ticket window 4");
        // The above selling ticket is to generate 4 objects, each object has 1000 tickets to sell, just because the thread gets CPU After the allocation
        // Using static member variables to share 1000 tickets, the tickets are not in the object space, but in the memory method area, the synchronization problem should be solved

        t1.start();
        t2.start();
        t3.start();
        t4.start();
        System.out.println("Hello World!");
    }
}

Above is the method to inherit Thread

This method is relatively simple, as long as the run method is copied after inheritance, the disadvantage is that other classes that have already inherited cannot inherit, and there are limitations. Static static keywords are required for multithreading to operate on members in a single object

Here is how to implement the implements Runnable method

package com.swift.duoxiancheng;

class Tickets implements Runnable {

    private int ticket = 1000;

    @Override
    public void run() {
        sell();
    }

    // Add to method with cycle synchronized After that, there is only one thread selling tickets, because locking a thread to execute a loop, of course, there is only one thread selling tickets, so you should add the lock to the loop
    // In addition, there are still threads waiting outside the loop, resulting in a negative number of votes while There's also synchronization,In the method, tickets are not sold circularly, only one ticket is sold
    // while Chinese writing judgment will affect the result, resulting in negative votes
    public void sell() {
        while (true) {
            synchronized (this) {
                if (ticket > 0) {
                    try {
                        Thread.sleep(0);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    ticket--;
                    System.out.println(Thread.currentThread().getName() + "There are still more tickets:" + ticket);
                }
            }
        }
    }
}

public class SellTickets_Runnable {
    public static void main(String[] args) {
        Tickets t = new Tickets();

        Thread t1 = new Thread(t, "Ticket window 1");
        Thread t2 = new Thread(t, "Ticket window 2");
        Thread t3 = new Thread(t, "Ticket window 3");
        Thread t4 = new Thread(t, "Ticket window 4");

        t1.start();
        t2.start();
        t3.start();
        t4.start();
        System.out.println("Hello World!");
    }
}

The lock can be in method or in synchronous code block. The synchronous code block is better and can be locked locally

A lock can make an object synchronized(this) or a string synchronized("lock")

When locking, note that if a loop is locked, only one thread will execute until it is completed

package com.swift.duoxiancheng;

class Ticketz implements Runnable {

    private int ticket = 100;

    @Override
    public void run() {
        sell();

    }
    //Add synchronized After that, there is only one thread selling tickets. Of course, only one thread is selling tickets when locking a thread execution cycle
    public synchronized void sell() {
        while (ticket > 0) {
            try {
                Thread.sleep(5);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            ticket--;
            System.out.println(Thread.currentThread().getName() + "There are still more tickets:" + ticket);
        }
    }
}

public class SellTickets_OnlyOneThreadSell {
    public static void main(String[] args) {
        Ticketz t = new Ticketz();

        Thread t1 = new Thread(t, "Ticket window 1");
        Thread t2 = new Thread(t, "Ticket window 2");
        Thread t3 = new Thread(t, "Ticket window 3");
        Thread t4 = new Thread(t, "Ticket window 4");

        t1.start();
        t2.start();
        t3.start();
        t4.start();
        System.out.println("Hello World!");
    }
}

Posted by tnewton on Sun, 03 May 2020 18:55:22 -0700