栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java多线程同步模拟卖票过程的问题

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java多线程同步模拟卖票过程的问题

Java两种方法实现模拟卖票 第一种继承Thread类
public class TestThread extends Thread{
    //定义静态变量,为所有对象所共有
    private static int ticket = 50;
    //定义静态对象 object,保证在使用时synchronized获得对象的唯一性
    //不然main方法里new多个对象就有多个锁
    private static Object object = new Object();
    //定义线程名称
    public TestThread(String name) {
        super(name);
    }
    @Override
    public void run(){
        while (true){
            //这里不能用this,这里的this代表test1,test2,test3三个对象,锁不唯一
            //方式一
            //synchronized(object){
            //方式二
            //获取TestThread1的class对象,class 类名 = 类名.class,类名.class只会加载一次
            synchronized(TestThread1.class){
                 if(ticket > 0){
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + "卖票,票号为:" + ticket);
                    ticket--;
                }else{
                     break;
                 }
            }
        }
    }
    public static void main(String[] args) throws Exception {
        TestThread test1 = new TestThread("窗口1");
        TestThread test2 = new TestThread("窗口2");
        TestThread test3 = new TestThread("窗口3");

        test1.start();
        test2.start();
        test3.start();

    }
}


第二种实现Runnable接口
public class RunnableTest {
    public static void main(String[] args) {
        MyThear1 myThear1 = new MyThear1();

        Thread thread1 = new Thread(myThear1,"窗口1");
        Thread thread2 = new Thread(myThear1,"窗口2");
        Thread thread3 = new Thread(myThear1,"窗口3");

        thread1.start();
        thread2.start();
        thread3.start();


    }
}

class MyThear1 implements Runnable{
    private static int ticket = 30;
    //方式一 保证在使用时synchronized获得对象的唯一性
    Object object = new Object();
    //该子类应重写 Thread 类的 run 方法
    @Override
    public void run() {

        while (true){
            //方式一
            //synchronized(object){
            //方式二
            //此时的this唯一的MyThear1对象
            //synchronized (this){
            //方式三 获取的MyThear1的class对象
            synchronized(MyThear1.class){
                if (ticket > 0) {
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    System.out.println(Thread.currentThread().getName() + "卖出票号:" + ticket);
                    ticket--;
                } else {
                    break;
                }
          }

        }
    }

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/340774.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号