模拟买票小练习-线程资源同步小练习-synchronized使用

模拟买票练习多线程

  • 使用Thread和Runable练习买票和练习共享资源问题
  • 方式一:继承Thread类
  •  1.首先定义一个ticks变量为总票数为10
    
  •  2.定义run方法,循环的扣除票数量(简单写一下打印ticks票数)
    
  •  3.模拟四个窗口购买票
    
  • 问题1:打印的结果是每个窗口都打印了10张票,也就是说变量tickets没有共享
  • 解决1:通过给变量添加static,使变量变为共享变量如(代码1)
  • 解决2:通过实现runable接口解决共享变量问题(代码2)
  • 问题2:打印的ticks数量顺序不是从10->1,顺序乱了,也就是数据不同步
  • 解决:通过代码块同步或者方法同步解决问题(加锁)(代码3、4)

代码1:

package com.lzw.java_thread.tickets;

/**
 * 使用Thread和Runable练习买票和练习共享资源问题
 * 方式一:继承Thread类
 *      1.首先定义一个ticks变量为总票数为10
 *      2.定义run方法,循环的扣除票数量(简单写一下打印ticks票数)
 *      3.模拟四个窗口购买票
 */
public class ThreadTicks extends Thread{
    
    
    private static int tickets = 10;
    @Override
    public void run() {
    
    
        while (tickets>0){
    
    
            try {
    
    
                Thread.sleep(1);
            } catch (InterruptedException e) {
    
    
                e.printStackTrace();
            }
            synchronized (this){
    
    
                if (tickets>0){
    
    
                    System.out.println(Thread.currentThread().getName() + " 剩余" + (tickets--) + "张票");
                }
            }
        }
    }

    public static void main(String[] args) {
    
    
        ThreadTicks tt1 = new ThreadTicks();
        ThreadTicks tt2 = new ThreadTicks();
        ThreadTicks tt3 = new ThreadTicks();
        ThreadTicks tt4 = new ThreadTicks();
        tt1.start();
        tt2.start();
        tt3.start();
        tt4.start();
    }
}

代码2:

package com.lzw.java_thread.tickets;

/**
 *第二种方式:
 *      1.实现Runable接口
 *      2.创建四个线程都传入一个Runable子类,实现对象资源共享
 *      3.调用start()方法
 */
public class RunableTickets implements Runnable{
    
    
    private int tickets = 10;
    @Override
    public void run() {
    
    
        while (tickets>0){
    
    
            try {
    
    
                Thread.sleep(1);
            } catch (InterruptedException e) {
    
    
                e.printStackTrace();
            }
            if (tickets>0){
    
    
                System.out.println(Thread.currentThread().getName()+" 剩余"+(tickets--)+"张票");
            }
        }
    }

    public static void main(String[] args) {
    
    
        RunableTickets rtt = new RunableTickets();
        Thread td1 = new Thread(rtt);
        Thread td2 = new Thread(rtt);
        Thread td3 = new Thread(rtt);
        Thread td4 = new Thread(rtt);
        td1.start();
        td2.start();
        td3.start();
        td4.start();
    }
}

代码3:

package com.lzw.java_thread.tickets;

/**
 * @Auther: lzw
 * 使用同步代码块,实现共享资源同步
 */
public class RunableTickets1 implements Runnable{
    
    
    private int tickets = 10;
    @Override
    public void run() {
    
    
        while (tickets>0){
    
    
            try {
    
    
                Thread.sleep(100);
            } catch (InterruptedException e) {
    
    
                e.printStackTrace();
            }
            synchronized (this){
    
    
                if (tickets>0){
    
    
                    System.out.println(Thread.currentThread().getName()+" 剩余"+(tickets--)+"张票");
                }
            }

        }
    }

    public static void main(String[] args) {
    
    
        RunableTickets1 rtt = new RunableTickets1();
        Thread td1 = new Thread(rtt);
        Thread td2 = new Thread(rtt);
        Thread td3 = new Thread(rtt);
        Thread td4 = new Thread(rtt);
        td1.start();
        td2.start();
        td3.start();
        td4.start();
    }
}

代码4:

package com.lzw.java_thread.tickets;

/**
 * @Auther: lzw
 * 使用同步方法,实现共享资源同步
 */
public class RunableTickets2 implements Runnable{
    
    
    private int tickets = 10;
    @Override
    public void run() {
    
    
        while (tickets>0){
    
    
            try {
    
    
                Thread.sleep(100);
            } catch (InterruptedException e) {
    
    
                e.printStackTrace();
            }

                this.sale();

        }
    }
    public synchronized void sale(){
    
    
        if (tickets>0){
    
    
            System.out.println(Thread.currentThread().getName()+" 剩余"+(tickets--)+"张票");
        }
    }

    public static void main(String[] args) {
    
    
        RunableTickets2 rtt = new RunableTickets2();
        Thread td1 = new Thread(rtt);
        Thread td2 = new Thread(rtt);
        Thread td3 = new Thread(rtt);
        Thread td4 = new Thread(rtt);
        td1.start();
        td2.start();
        td3.start();
        td4.start();
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_47402482/article/details/109298324