【Java】【多线程】卖票

多线程的常见例子

分别继承Thread和实现Runnable,创建三个线程卖票。

package com.itheima;

class MyThread extends Thread{
    private static int tickets = 100;
    @Override
    public void run() {
        while (true){
            if(tickets > 0){
                System.out.println(Thread.currentThread().getName() + ": 卖票,票号为:" + tickets );
                tickets--;
            }else return;
        }
    }
}
public class T {
    public static void main(String[] args) {

        MyThread thread1 = new MyThread();
        thread1.setName("窗口1");
        thread1.start();
        MyThread thread2 = new MyThread();
        thread2.setName("窗口2");
        thread2.start();
        MyThread thread3 = new MyThread();
        thread3.setName("窗口3");
        thread3.start();

    }
}


==========================================================

package com.itheima;

class MyThread implements Runnable{
    private int tickets = 100;
    @Override
    public void run() {
        while (true){
            if(tickets > 0){
                System.out.println(Thread.currentThread().getName() + ": 卖票,票号为:" + tickets );
                tickets--;
            }else return;
        }
    }
}
public class T {
    public static void main(String[] args) {
        MyThread myThread = new MyThread();
        Thread thread1 = new Thread(myThread);
        thread1.setName("窗口1");
        thread1.start();
        Thread thread2 = new Thread(myThread);
        thread2.setName("窗口2");
        thread2.start();
        Thread thread3 = new Thread(myThread);
        thread3.setName("窗口3");
        thread3.start();

    }
}


在这里插入图片描述

=================================================
在这里插入图片描述
仔细会发现有重票问题。多个线程操作共享数据。
问题: 出现重票、错票 -->出现了线程的安全问题。
原因: 当某个线程操作车票的过程中,尚未操作完成时,其他线程参与进来,也操作车票。
解决: 当一个线程a在操作ticket的时候,其他线程不能参与进来。直到线程a操作完ticket时,其他线程才可以操作ticket。即使线程a出现了阻塞,也不能被改变。

继承Thread :

package com.itheima;
public class Main {
    public static void main(String[] args) {
        new MyThread("窗口1").start();
        new MyThread("窗口2").start();
        new MyThread("窗口3").start();

    }
}
class MyThread extends Thread{
    private static int tickets = 100;
    private static String str = "aa";

    public MyThread(String name) {
        super(name);
    }

    @Override
    public void run() {
        while(tickets > 0){
            synchronized (str){
                if(tickets > 0){
                    System.out.println(Thread.currentThread().getName() +
                            "卖出第"+tickets+"张票!");
                    tickets--;
                }
                else
                    System.out.println("票卖完了");
            }
            try{
                sleep(100);
            }catch (InterruptedException e){
                e.printStackTrace();
            }

        }
    }
}

在这里插入图片描述
实现Runable接口:

package com.itheima;

class MyThread implements Runnable{
    private int tickets = 100;
    @Override
    public void run() {
        while (true){
            init();
            if(tickets == 0){
                break;
            }
        }
    }
    private synchronized void init(){
        if(tickets > 0){
            System.out.println(Thread.currentThread().getName() + ": 卖票,票号为:" + tickets );
            tickets--;
        }
        try{
                Thread.sleep(100);
            }catch (InterruptedException e){
                e.printStackTrace();
            }
    }
}
public class T {
    public static void main(String[] args) {
        MyThread myThread = new MyThread();
        Thread thread1 = new Thread(myThread);
        thread1.setName("窗口1");
        thread1.start();
        Thread thread2 = new Thread(myThread);
        thread2.setName("窗口2");
        thread2.start();
        Thread thread3 = new Thread(myThread);
        thread3.setName("窗口3");
        thread3.start();

    }
}



在这里插入图片描述

上面当继承Thread类实现线程同步时,
1.同步代码块需要同一把锁,即静态对象

private static String str = "aa";
synchronized (str){
..........
..........
..........
            
}

2.同步方法时需要设置为静态方法。

private synchronized static void init(){
..........
..........
..........  
}

上面通过实现Runable接口实现线程同步时,
1.同步代码块需要同一把锁,即静态对象。
但是更常用的使用以下同步代码块。使用this对象

synchronized (this){
..........
..........
..........                
}

2.同步方法不需要设置为静态方法

private synchronized void init(){
..........
..........
.......... 
}

多线程实现窗口售票的功能

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值