multithreaded learning

1. Asynchrony may lead to incorrect allocation of public resources

class SaleTicketThread implements Runnable {
    private int ticket = 10;

    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            if (ticket > 0) {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace ();
                }
                System.out.println("Remaining" + ticket-- + "Ticket");
            }
        }
    }
}

public class NoSynchronized {
    public static void main(String[] args) {
        SaleTicketThread saleTicketThread = new SaleTicketThread ();
        for (int i = 0; i < 3; i++) {
            Thread thread = new Thread(saleTicketThread);
            thread.start();
        }
    }

}

result:

10 tickets left

9 tickets left

8 tickets left

7 tickets left

6 tickets left

5 tickets left

4 tickets left

3 tickets left

2 tickets left

1 ticket left

0 tickets left

-1 ticket left

 

 

Plus synchronized synchronization to ensure resources

class SaleTicketThread2 implements Runnable {
    private int ticket = 10;

    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            synchronized (this) {
                if (ticket > 0) {
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace ();
                    }
                    System.out.println("Remaining" + ticket-- + "Ticket");
                }
            }
        }
    }
}

public class HaveSynchronized {
    public static void main(String[] args) {
        SaleTicketThread2 saleTicketThread2 = new SaleTicketThread2 ();
        for (int i = 0; i < 4; i++) {
            Thread thread = new Thread(saleTicketThread2);
            thread.start();
        }
    }
}

 

result:

10 tickets left

9 tickets left

8 tickets left

7 tickets left

6 tickets left

5 tickets left

4 tickets left

3 tickets left

2 tickets left

1 ticket left

 

2. Deadlock

class ZhangSan {
    public void say() {
        System.out.println("Li Si, you draw me, I will give you a book");
    }

    public void get() {
        System.out.println("Zhang San got drawn");
    }
}


class Lisi {
    public void say() {
        System.out.println("Zhang San, you give me the book, I will draw you");
    }

    public void get() {
        System.out.println("Li Si got the book");
    }
}

public class DeadLock implements Runnable {
    private static ZhangSan zhangSan = new ZhangSan();
    private static Lisi lisi = new Lisi();
    private boolean flag = false;

    public DeadLock(boolean flag) {
        this.flag = flag;
    }

    @Override
    public void run() {
        if (flag) {
            synchronized (zhangSan) {
                zhangSan.say();
                try {
                    Thread.sleep(0);
                } catch (InterruptedException e) {
                    e.printStackTrace ();
                }
                synchronized (lisi) {
                    lisi.get();
                }
            }
        } else {
            synchronized (lisi) {
                lisi.say();
                try {
                    Thread.sleep(200);
                } catch (InterruptedException e) {
                    e.printStackTrace ();
                }
                synchronized (zhangSan) {
                    zhangSan.get();
                }
            }
        }
    }

    public static void main(String[] args) {
        DeadLock deadLock1 = new DeadLock(true);
        DeadLock deadLock2 = new DeadLock(false);
        Thread thread1 = new Thread(deadLock1);
        Thread thread2 = new Thread(deadLock2);
        thread1.start();
        thread2.start();
    }

}

 

result:

Li Si, you paint me, I'll give you a book

Zhang San, you give me the book and I will draw you

......

 

3. suspend, resume, stop may cause deadlock, use the flag to end the process

class SellProduct implements Runnable {
    private boolean flag = true;

    @Override
    public void run() {
        int i = 0;
        while (flag) {
            System.out.println(Thread.currentThread().getName() + "运行,i = " + (i++));
        }
    }

    public void stopSell() {
        flag = false;
    }
}

public class StopThread {

    public static void main(String[] args) {
        SellProduct sellProduct = new SellProduct();
        Thread thread = new Thread(sellProduct);
        thread.start();
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace ();
        }
        sellProduct.stopSell();
    }
}

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326611998&siteId=291194637