JavaSE_day44(多线程实际运用:火车卖票

1 A.java


//需求:用三个线程模拟三个售票窗口,共同卖100张火车票,每个线程打印出卖第几张票
/*
 * 问题出现的原因:
 *         要有多个线程
 *         要有被多个线程所共享的数据
 *         多个线程并发的访问共享的数据
 * 
 * 在火车上上厕所
 * 张三来了,一看门是绿的,他就进去了,把门锁上了,门就变红了
 * 李四来了,一看门市红色的,他就只能憋着
 * 张三用完了厕所,把锁打开了,门就变成了绿色
 * 李四一看门变绿了,他就进去了,把门锁上,门就变红了
 * 王五来了,一看们是红色的,他也只能憋着
 * 李四用完测试了,把锁打开了,肚子又不舒服了,扭头回去了,又把门锁上了,
 * 
 * 关键字:synchronized:同步(锁),可以修饰代码块和方法,被修饰的代码块和方法一旦被某个线程访问,则直接锁住,其他的线程将无法访问
 * 
 * 同步代码块:
 *             synchronized(锁对象){
 * 
 *             }
 * 
 * 注意:锁对象需要被所有的线程所共享
 * 

*
 * 同步方法:使用关键字synchronized修饰的方法,一旦被一个线程访问,则整个方法全部锁住,其他线程则无法访问
 * 
 * synchronized
 * 注意:
 *         非静态同步方法的锁对象是this
 *         静态的同步方法的锁对象是当前类的字节码对象

参考:(116条消息) Java多线程-44-静态和非静态方法同步锁对象是什么_Anthony_tester的博客-CSDN博客


 */
 * 
 * 同步:安全性高,效率低
 * 非同步:效率高,但是安全性低
 * 
 */

锁住代码块:

public class A_TicketThread implements Runnable{

	int tickets = 100;//火车票数量
	Object obj = new Object();
	
	@Override
	public void run() {
		//出售火车票
		while(true) {
			synchronized (obj) {
			//当火车票小于0张,则停止售票
			if(tickets > 0) {
				/*
				 * t1,t2,t3
				 * 假设只剩一张票
				 * t1过来了,他一看有票,他就进来了,但是他突然肚子不舒服,然后他就去上卫生间了
				 * t2也过来了,他一看也有票,他也进来了,但是他的肚子也不舒服,他也去上卫生间了
				 * 
				 * t1上完了卫生间回来了,开始售票
				 * 	tickets = 0;
				 * t2也上完卫生间回来了,他也进行售票
				 *  tickets = -1;
				 * 
				 * 
				 */
				try {
					Thread.sleep(400);//让线程睡一会再工作
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				System.out.println(Thread.currentThread().getName() + ":" +tickets--);
			}
		}
		}
	}
}

锁住方法:

public class A_TicketThread2 implements Runnable {
	static int tickets = 100;// 火车票数量
	Object obj = new Object();

	@Override
	public void run() {
		// 出售火车票
		while (true) {
			/*synchronized (obj) {
				method();
			}*/
			
			//method();
			method2();

		}
	}

	private synchronized void method() {
		if (tickets > 0) {
           
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			System.out.println(Thread.currentThread().getName() + ":" + tickets--);
		}
	}
	
	
	private static synchronized void method2() {
	
		if (tickets > 0) {

			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			System.out.println(Thread.currentThread().getName() + ":" + tickets--);
		}
	}

	

}

主类:

public class A_TicketTest {
    public static void main(String[] args) {
        // 创建线程对象
        A_TicketThread2 tt1 = new A_TicketThread2();
    

        Thread t1 = new Thread(tt1);
        t1.setName("窗口1");
        Thread t2 = new Thread(tt1);
        t2.setName("窗口2");
        Thread t3 = new Thread(tt1);
        t3.setName("窗口3");

        // 启动线程对象
        t1.start();
        t2.start();
        t3.start();
    }
}

以下均是本站参考的代码: 

/**
 * 方式一:使用synchronized同步代码块解决继承Thread方式解决售票问题中的线程安全问题
 *
 * @author wds
 * @date 2021-11-16-9:53
 */
public class B {
    public static void main(String[] args) {
        Thread01 thread01 = new Thread01();
        Thread01 thread02 = new Thread01();
        Thread01 thread03 = new Thread01();
        thread01.setName("窗口1");
        thread02.setName("窗口2");
        thread03.setName("窗口3");
        thread01.start();
        thread02.start();
        thread03.start();
    }
}
class Thread01 extends Thread {
    private static int ticket = 100;                     //静态常量数据共享
    private static Object obj = new Object();
    @Override
    public void run() {
        while (true) {
            synchronized (obj) {                            //同步代码块
                if (ticket > 0) {
                    try {
                        Thread.sleep(100);      //模拟售票花费的时间
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + "正在售票,票号为" + ticket);
                    ticket--;
                }
            }
        }

    }
}

package day44;

/**
 * 方式二:使用synchronized同步方法解决继承Thread方式解决售票问题中的线程安全问题
 * @author wds
 * @date 2021-11-16-11:20
 */
public class C {
    public static void main(String[] args) {
        Thread03 t1 = new Thread03();
        Thread03 t2 = new Thread03();
        Thread03 t3 = new Thread03();
        t1.setName("窗口1");
        t2.setName("窗口2");
        t3.setName("窗口3");
        t1.start();
        t2.start();
        t3.start();
    }
}
class Thread03 extends Thread {
    private static int ticket = 100;            //静态常量数据共享
    @Override
    public void run() {
        while (true) {
            show();
        }
    }
    public static synchronized void show() {   //同步方法
        if (ticket > 0) {
            try {
                Thread.sleep(100);      //模拟售票所需要的时间
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "正在售票,票号为" + ticket);
            ticket--;
        }
    }
}

package day44;

/**
 * 方式一:使用synchronized同步代码块解决实现Runnable接口方式解决售票问题中的线程安全问题
 * @author wds
 * @date 2021-11-16-10:09
 */
public class D {
    public static void main(String[] args) {
        Thread02 thread02 = new Thread02();
        Thread t1 = new Thread(thread02, "窗口1");
        Thread t2 = new Thread(thread02, "窗口2");
        Thread t3 = new Thread(thread02, "窗口3");
        t1.start();
        t2.start();
        t3.start();
    }
}

class Thread02 implements Runnable {
    private static int ticket = 100;                 //静态常量数据共享

    //private static Object obj = new Object();
    @Override
    public void run() {
        while (true) {
            //方式1:对象做为监视器
            //synchronized (obj){                       //同步代码块
            //方式2:this关键字作为监视器
            //synchronized (this){
            //方式3:类名.class作为监视器
            synchronized (Thread02.class) {
                if (ticket > 0) {
                    try {
                        Thread.sleep(200);  //模拟售票花费的时间
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + "正在售票,票号为" + ticket);
                    ticket--;
                }
            }


        }


    }
}

package day44;

/**
 * 方式二:使用synchronized同步方法解决实现接口Runnable方式解决售票问题中的线程安全问题
 * @author wds
 * @date 2021-11-16-11:31
 */
public class E {
    public static void main(String[] args) {
        Thread04 thread04 = new Thread04();
        Thread t1 = new Thread(thread04, "窗口1");
        Thread t2 = new Thread(thread04, "窗口2");
        Thread t3 = new Thread(thread04, "窗口3");
        t1.start();
        t2.start();
        t3.start();
    }
}
class Thread04 implements Runnable{
    private static int ticket = 100;                //静态常量共享数据
    @Override
    public void run() {
        while(true){
            show();
        }
    }
    public synchronized static void show(){         //同步方法
        if(ticket>0){
            try {
                Thread.sleep(300);          //模拟售票过程需要的时间
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+"正在售票,票号为"+ticket);
            ticket--;
        }
    }
}




 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值