Java多线程(三)- 线程同步

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_18149897/article/details/72597784

当线程目标对象有多个线程同事使用方法进行修改时,程序应对这样的问题做出处理,否则程序将发生混乱。
所谓线程同步就是若干个线程都需要使用一个synchronize(同步)修饰方法,多个线程调用synchronize方法必须遵守同步机制。

package test;

public class Ticket implements Runnable {

    int Ticket = 0;
    public void setNum(int num) {
        this.Ticket = num;
    }
    @Override
    public void run() {
        if (Ticket > 0) {
            if (Thread.currentThread().getName().equals("group1")) {
                System.out.println("group1 come here~");
                groupBuyTicketorpersonalBuyTicket(20);
            } else if (Thread.currentThread().getName().equals("group2")) {
                System.out.println("group2 come here~");
                groupBuyTicketorpersonalBuyTicket(30);
            }
        }
       else {
        System.out.println("Tickets is not enough~~~");
        Thread.currentThread().interrupt();
    }
}
    public void  groupBuyTicketorpersonalBuyTicket(int BuyTicket) {
        if (Thread.currentThread().getName().equals("group1")) {
            for (int i = 0; i <10; i++) {
                Ticket = Ticket - 1;
                System.out.println("group1 buy one ticket");
                try {
                    Thread.sleep(200);
                } catch (InterruptedException e) {
                    System.out.println("group1 Thread is error , InterruptedException");
                } 
            }
        }
        else if (Thread.currentThread().getName().equals("group2")) {
            for (int i = 0; i < 10; i++) {
                Ticket  = Ticket - 1;
                System.out.println("group2 one Buy one and wait a min ,then buy another one~");
                try {
                    Thread.sleep(200);
                } catch (InterruptedException e) {
                    System.out.println("group2 Thread is error ,  InterruptedException");
                }
            }
        }

    }
}

group2 come here~
group2 one Buy one and wait a min ,then buy another one~
group1 come here~
group1 buy one ticket
group1 buy one ticket
group2 one Buy one and wait a min ,then buy another one~
group1 buy one ticket
group2 one Buy one and wait a min ,then buy another one~
group2 one Buy one and wait a min ,then buy another one~
group1 buy one ticket
group1 buy one ticket
group2 one Buy one and wait a min ,then buy another one~
group1 buy one ticket
group2 one Buy one and wait a min ,then buy another one~
group2 one Buy one and wait a min ,then buy another one~
group1 buy one ticket
group1 buy one ticket
group2 one Buy one and wait a min ,then buy another one~
group1 buy one ticket
group2 one Buy one and wait a min ,then buy another one~
group2 one Buy one and wait a min ,then buy another one~
group1 buy one ticket

添加synchronize关键字给groupBuyTicketorpersonalBuyTicket()方法

package test;

public class Ticket implements Runnable {

    int Ticket = 0;
    public void setNum(int num) {
        this.Ticket = num;
    }
    @Override
    public void run() {
        if (Ticket > 0) {
            if (Thread.currentThread().getName().equals("group1")) {
                System.out.println("group1 come here~");
                groupBuyTicketorpersonalBuyTicket(20);
            } else if (Thread.currentThread().getName().equals("group2")) {
                System.out.println("group2 come here~");
                groupBuyTicketorpersonalBuyTicket(30);
            }
        }
       else {
        System.out.println("Tickets is not enough~~~");
        Thread.currentThread().interrupt();
    }
}
    public synchronized void  groupBuyTicketorpersonalBuyTicket(int BuyTicket) {
        if (Thread.currentThread().getName().equals("group1")) {
            for (int i = 0; i <10; i++) {
                Ticket = Ticket - 1;
                System.out.println("group1 buy one ticket");
                try {
                    Thread.sleep(200);
                } catch (InterruptedException e) {
                    System.out.println("group1 Thread is error , InterruptedException");
                } 
            }
        }
        else if (Thread.currentThread().getName().equals("group2")) {
            for (int i = 0; i < 10; i++) {
                Ticket  = Ticket - 1;
                System.out.println("group2 one Buy one and wait a min ,then buy another one~");
                try {
                    Thread.sleep(200);
                } catch (InterruptedException e) {
                    System.out.println("group2 Thread is error ,  InterruptedException");
                }
            }
        }

    }
}

group2 come here~
group2 one Buy one and wait a min ,then buy another one~
group1 come here~
group2 one Buy one and wait a min ,then buy another one~
group2 one Buy one and wait a min ,then buy another one~
group2 one Buy one and wait a min ,then buy another one~
group2 one Buy one and wait a min ,then buy another one~
group2 one Buy one and wait a min ,then buy another one~
group2 one Buy one and wait a min ,then buy another one~
group2 one Buy one and wait a min ,then buy another one~
group2 one Buy one and wait a min ,then buy another one~
group2 one Buy one and wait a min ,then buy another one~
group1 buy one ticket
group1 buy one ticket
group1 buy one ticket
group1 buy one ticket
group1 buy one ticket
group1 buy one ticket
group1 buy one ticket
group1 buy one ticket
group1 buy one ticket
group1 buy one ticket

你可以发现当线程1使用groupBuyTicketorpersonalBuyTicket方法时,线程二不能使用方法。

猜你喜欢

转载自blog.csdn.net/qq_18149897/article/details/72597784