1. 程式人生 > >Java多執行緒--售票程式碼

Java多執行緒--售票程式碼

使用繼承自Thread類的子類:

同步塊:

/**
 * 繼承Thread類的售票類
 * 
 * @author XINY
 *
 */
class TicketT extends Thread {
	// 票數,起始數量為100
	private static int tick = 100;
	//隨便寫的一個物件,如沒特殊要求可隨便例項化一個物件,用於同步塊中傳入的值。
	//為保證每個執行緒的鎖一致,必需讓obj為靜態變數
	static Object obj = new Object();

	@Override
	public void run() {
		// TODO Auto-generated method stub
		while (tick > 0) {
			//obj物件無特殊要求,任何物件均可,但是必需保證每個執行緒傳入的物件是同一個物件
			synchronized (obj) {
				if (tick > 0) {
					try {
						// 休眠10毫秒
						sleep(10);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					// 出票
					System.out.println(Thread.currentThread().getName()
							+ "----" + tick--);
				}
			}
		}
	}
}

/**
 * 主類
 * 
 * @author XINY
 *
 */
public class TicketThread {

	public static void main(String[] args) {
		// 建立售票執行緒
		TicketT t1 = new TicketT();
		TicketT t2 = new TicketT();
		TicketT t3 = new TicketT();
		TicketT t4 = new TicketT();

		// 開始執行緒
		t1.start();
		t2.start();
		t3.start();
		t4.start();
	}
}

同步方法:

/**
 * 繼承Thread類的售票類
 * 
 * @author XINY
 *
 */
class TicketT extends Thread {
	// 票數,起始數量為100
	private static int tick = 100;

	@Override
	public void run() {
		// TODO Auto-generated method stub
		while (tick > 0) {
			sale();
		}
	}
	/**
	 * 同步方法
	 * 為保證鎖相同,將sale方法定義為static
	 */
	public static synchronized void sale(){
		if (tick>0) {
			try {
				// 休眠10毫秒
				sleep(10);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			// 出票
			System.out.println(Thread.currentThread().getName()
					+ "----" + tick--);
		}
	}
}

/**
 * 主類
 * 
 * @author XINY
 *
 */
public class TicketThread {

	public static void main(String[] args) {
		// 建立售票執行緒
		TicketT t1 = new TicketT();
		TicketT t2 = new TicketT();
		TicketT t3 = new TicketT();
		TicketT t4 = new TicketT();

		// 開始執行緒
		t1.start();
		t2.start();
		t3.start();
		t4.start();
	}
}

使用實現Runnable介面的子類:

同步塊:

/**
 * 實現Runnable介面的類
 * 
 * @author XINY
 *
 */
class TicketR implements Runnable {
	// 初始票數100
	private int tick = 100;
	Object obj=new Object();
	@Override
	public void run() {
		// TODO Auto-generated method stub
		while (tick > 0) {
			//new Object() 是我隨便寫的,這裡需要傳入一個物件
			synchronized (obj) {
				if (tick > 0) {
					try {
						Thread.sleep(10);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					// 出票
					System.out.println(Thread.currentThread().getName()
							+ "----" + tick--);
				}
			}
		}
	}
}

/**
 * 主類
 * 
 * @author XINY
 *
 */
public class TicketRunnable {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		TicketR t = new TicketR();

		Thread t1 = new Thread(t);
		Thread t2 = new Thread(t);
		Thread t3 = new Thread(t);
		Thread t4 = new Thread(t);

		t1.start();
		t2.start();
		t3.start();
		t4.start();
	}
}

同步方法:

/**
 * 實現Runnable介面的類
 * 
 * @author XINY
 *
 */
class TicketR implements Runnable {
	// 初始票數100
	private int tick = 100;

	@Override
	public void run() {
		// TODO Auto-generated method stub
		while (tick > 0) {
			sale();
		}
	}

	public synchronized void sale() {
		if (tick > 0) {
			try {
				Thread.sleep(10);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			// 出票
			System.out.println(Thread.currentThread().getName() + "----"
					+ tick--);
		}
	}
}

/**
 * 主類
 * 
 * @author XINY
 *
 */
public class TicketRunnable {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		TicketR t = new TicketR();

		Thread t1 = new Thread(t);
		Thread t2 = new Thread(t);
		Thread t3 = new Thread(t);
		Thread t4 = new Thread(t);

		t1.start();
		t2.start();
		t3.start();
		t4.start();
	}
}