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

当线程目标对象有多个线程同事使用方法进行修改时,程序应对这样的问题做出处理,否则程序将发生混乱。
所谓线程同步就是若干个线程都需要使用一个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方法时,线程二不能使用方法。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值