多线程问题实例--火车票销售模拟

1 10张票放开6个窗口同时销售,火车票的数量为线程之间的公共资源。

package cyb.lxf.staring.thread;

import java.util.Date;

public class SaleTicketThread implements Runnable{
    private int ticket  = 0;//多个线程会共用这个资源
	public SaleTicketThread(int num){
		this.ticket = num;
	}
	@Override
	public void run() {
		while(true){
			synchronized (this) {
				if(this.ticket>0){
					System.out.println(new Date()+":"+Thread.currentThread().getName()+"卖出票"+this.ticket);		
				}else{
					System.out.println(new Date()+":"+Thread.currentThread().getName()+" 没票啦!");
				    //Thread.currentThread().interrupt();
					Thread.currentThread().stop();
				    break;
				}
				this.ticket--;
			  }
		    try {
				Thread.sleep(5000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			
		}
	}
    public static void main(String[] args) {
    	SaleTicketThread th = new SaleTicketThread(10);
    	new Thread(th,"1号窗口").start();
    	new Thread(th,"2号窗口").start();
    	new Thread(th,"3号窗口").start();
    	new Thread(th,"4号窗口").start();
    	new Thread(th,"5号窗口").start();
    	new Thread(th,"6号窗口").start();
	}
}


运行结果:

Wed May 14 10:40:01 GMT+08:00 2014:1号窗口卖出票10
Wed May 14 10:40:01 GMT+08:00 2014:6号窗口卖出票9
Wed May 14 10:40:01 GMT+08:00 2014:5号窗口卖出票8
Wed May 14 10:40:01 GMT+08:00 2014:4号窗口卖出票7
Wed May 14 10:40:01 GMT+08:00 2014:3号窗口卖出票6
Wed May 14 10:40:01 GMT+08:00 2014:2号窗口卖出票5
Wed May 14 10:40:06 GMT+08:00 2014:1号窗口卖出票4
Wed May 14 10:40:06 GMT+08:00 2014:6号窗口卖出票3
Wed May 14 10:40:06 GMT+08:00 2014:4号窗口卖出票2
Wed May 14 10:40:06 GMT+08:00 2014:2号窗口卖出票1
Wed May 14 10:40:06 GMT+08:00 2014:5号窗口 没票啦!
Wed May 14 10:40:06 GMT+08:00 2014:3号窗口 没票啦!
Wed May 14 10:40:12 GMT+08:00 2014:1号窗口 没票啦!
Wed May 14 10:40:12 GMT+08:00 2014:4号窗口 没票啦!
Wed May 14 10:40:12 GMT+08:00 2014:2号窗口 没票啦!
Wed May 14 10:40:12 GMT+08:00 2014:6号窗口 没票啦!


多线程知识参见:http://www.cnblogs.com/GnagWang/archive/2011/02/27/1966606.html


import cyb.lxf.staring.swing.action.MultiThread;
//使用继承的方式来实现多线程
public class SeqThread extends Thread {	
	public SeqThread(String name) {
		super(name);
	}
	private int count=5;
	@SuppressWarnings({ "static-access", "static-access" })
	@Override
	public void run() {
		System.out.println(this.currentThread().getName()+"--"+new Date()+" start...");
			while(true){
			synchronized (new MultiThread()) {
				if(count==0){
				    System.out.println(this.currentThread().getName()+"--"+new Date()+" end...");
				    Thread.currentThread().stop();
					break;
				}
				try {
					System.out.println(this.currentThread().getName()+"--"+new Date()+" "+this.count);
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				count--;
			  }
		}
      } 
	 public static void main(String[] args) {
		 SeqThread thread2 = new SeqThread("thread-1");
		 SeqThread thread1 = new SeqThread("thread-2");
		 thread1.start(); 
		 try {
				Thread.sleep(2000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		  thread2.start();
		}
}


thread-2--Wed May 14 11:21:28 GMT+08:00 2014 start...
thread-2--Wed May 14 11:21:29 GMT+08:00 2014 5
thread-2--Wed May 14 11:21:30 GMT+08:00 2014 4
thread-1--Wed May 14 11:21:30 GMT+08:00 2014 start...
thread-1--Wed May 14 11:21:30 GMT+08:00 2014 5
thread-2--Wed May 14 11:21:31 GMT+08:00 2014 3
thread-1--Wed May 14 11:21:31 GMT+08:00 2014 4
thread-2--Wed May 14 11:21:32 GMT+08:00 2014 2
thread-1--Wed May 14 11:21:32 GMT+08:00 2014 3
thread-2--Wed May 14 11:21:33 GMT+08:00 2014 1
thread-1--Wed May 14 11:21:33 GMT+08:00 2014 2
thread-2--Wed May 14 11:21:34 GMT+08:00 2014 end...
thread-1--Wed May 14 11:21:35 GMT+08:00 2014 1
thread-1--Wed May 14 11:21:36 GMT+08:00 2014 end...

//使用内部类实现并发操作

public class InnerSeqThread  {	
	private int count=5;
	 public class MyThread extends Thread{
		 public MyThread(String name) {
				super(name);
			}
			@SuppressWarnings({ "static-access", "static-access" })
			@Override
			public void run() {
				System.out.println(this.currentThread().getName()+"--"+new Date()+" start...");
					while(true){
					synchronized (InnerSeqThread.this) {//这个同步对象InnerSeqThread.this
						if(InnerSeqThread.this.count<=0){
						    System.out.println(this.currentThread().getName()+"--"+new Date()+" end...");
						    Thread.currentThread().stop();
							break;
						}
						try {
							System.out.println(this.currentThread().getName()+"--"+new Date()+" "+InnerSeqThread.this.count);
							Thread.sleep(1000);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
						InnerSeqThread.this.count--;
					  }
				}
	 }
	}
	 public static void main(String[] args) {
		 InnerSeqThread inner = new InnerSeqThread();
		 MyThread thread2 = inner.new MyThread("thread-1");
		 MyThread thread1 = inner.new MyThread("thread-2");
		 thread1.start(); 
		 try {
				Thread.sleep(2000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		  thread2.start();
		}
}
运行结果:
thread-2--Wed May 14 12:16:33 GMT+08:00 2014 start...
thread-2--Wed May 14 12:16:33 GMT+08:00 2014 5
thread-2--Wed May 14 12:16:34 GMT+08:00 2014 4
thread-1--Wed May 14 12:16:35 GMT+08:00 2014 start...
thread-2--Wed May 14 12:16:35 GMT+08:00 2014 3
thread-2--Wed May 14 12:16:36 GMT+08:00 2014 2
thread-2--Wed May 14 12:16:37 GMT+08:00 2014 1
thread-2--Wed May 14 12:16:38 GMT+08:00 2014 end...
thread-1--Wed May 14 12:16:38 GMT+08:00 2014 end...




评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值