線程併發庫(計時器,同步)

計時器:

	public static void main(String[] args) throws InterruptedException {

		new Timer().schedule(new MyTask(), 3000); // 安排一個任務, 3秒之後執行
		new Timer().schedule(new MyTask(), new Date(111, 10, 29, 10, 57, 45)); // 指定時間執行
		new Timer().schedule(new MyTask(), 3000, 1000); // 3秒後第一次, 每隔1秒再來一次
		new Timer().schedule(new MyTask(), new Date(111, 10, 29, 10, 59, 45),1000); // 指定時間執行第一次, 每隔1秒再來一次

		while (true) {
			System.out.println(new Date());
			Thread.sleep(1000);
		}
	}

}

class MyTask extends TimerTask {
	public void run() {
		System.out.println("do something");
	}
}

使用計時器安排任務,先2秒執行一次,然後4秒執行一次,再2秒執行一次,4秒執行一次,循環……:

	public static void main(String[] args) throws InterruptedException {
		new Timer().schedule(new Task1(), 2000);

		while (true) {
			System.out.println(new Date());
			Thread.sleep(1000);
		}
	}

}

class Task1 extends TimerTask {
	public void run() {
		System.out.println("Task1");
		new Timer().schedule(new Task2(), 4000);
	}
}

class Task2 extends TimerTask {
	public void run() {
		System.out.println("Task2");
		new Timer().schedule(new Task1(), 2000);
	}
}

使用計時器安排任務,週一到週五每天凌晨4點執行:

public class Exercise2 {
	public static void main(String[] args) {
		Calendar c = Calendar.getInstance();		// 日曆, 指向當前時間
		int hour = c.get(Calendar.HOUR_OF_DAY);		// 當前小時
		if(hour >= 4)								// 如果過了4點, 就翻到明天
			c.add(Calendar.DATE, 1);
		c.set(Calendar.HOUR_OF_DAY, 4);				// 設置爲4點整
		c.set(Calendar.MINUTE, 0);
		c.set(Calendar.SECOND, 0);
		
		new Timer().schedule(new Task(), c.getTime(), 1000 * 60 * 60 * 24);
	}

}

class Task extends TimerTask {
	public void run() {
		int day = Calendar.getInstance().get(Calendar.DAY_OF_WEEK);
		if(day == Calendar.SATURDAY || day == Calendar.SUNDAY)
			return;
		System.out.println("do something");
	}
}

JDK1.4同步的三種方式:

同步代碼塊

使用synchronized(鎖對象){同步代碼}形式進行同步,多個線程執行同步代碼塊時如果使用的鎖對象相同,只能有一個線程執行。

同步方法

使用synchronized關鍵字修飾方法,這時整個方法都是同步的,使用this作爲鎖對象。

靜態同步方法

靜態方法也可以使用synchronized關鍵字修飾,方法內部的代碼也是同步的,這時的鎖對象是當前類的Class對象。


同步代碼塊:

public class SyncDemo1 {

	public static void main(String[] args) {
		final Printer p = new Printer();

		new Thread(new Runnable() {
			public void run() {
				while (true)
					p.print1();
			}
		}).start();

		new Thread(new Runnable() {
			public void run() {
				while (true)
					p.print2();
			}
		}).start();

	}
}

class Printer {
	private Object lock = new Object();
	public void print1() {
		synchronized (lock) {
			System.out.print("清");
			System.out.print("華");
			System.out.print("大");
			System.out.println("學");
		}
	}
	public  void print2() {	
		synchronized (lock) {
		System.out.print("北");
		System.out.print("京");
		System.out.print("大");
		System.out.println("學");
	}
	}
}

同步方法:

class Printer {
	public synchronized void print1() {
		System.out.print("清");
		System.out.print("華");
		System.out.print("大");
		System.out.println("學");
	}

	public synchronized void print2() {
		System.out.print("北");
		System.out.print("京");
		System.out.print("大");
		System.out.println("學");
	}
}
同步方法和同步代碼塊聯合使用實現同步(同步方法使用this當做鎖):

class Printer {

	public void print1() {
		synchronized (this) {
			System.out.print("清");
			System.out.print("華");
			System.out.print("大");
			System.out.println("學");
		}
	}

	public synchronized void print2() {
		System.out.print("北");
		System.out.print("京");
		System.out.print("大");
		System.out.println("學");
	}
}
同步方法和同步代碼塊聯合使用實現同步(靜態同步方法使用當前類的Class對象當做鎖):
class Printer {

	public void print1() {
		synchronized (this.getClass()) {
			System.out.print("清");
			System.out.print("華");
			System.out.print("大");
			System.out.println("學");
		}
	}

	public static synchronized void print2() {
		System.out.print("北");
		System.out.print("京");
		System.out.print("大");
		System.out.println("學");
	}
}
循環嵌套同步鎖可能導致死鎖:

	private static Object lock1 = new Object();
	private static Object lock2 = new Object();

	public static void main(String[] args) {
		new Thread(new Runnable() {
			public void run() {
				synchronized (lock1) {
					System.out.println("鎖定lock1");
					synchronized (lock2) {
						System.out.println("鎖定lock2");
					}
					System.out.println("釋放lock2");
				}
				System.out.println("釋放lock1");
			}
		}).start();

		new Thread(new Runnable() {
			public void run() {
				synchronized (lock2) {
					System.out.println("鎖定lock2");
					synchronized (lock1) {
						System.out.println("鎖定lock1");
					}
					System.out.println("釋放lock1");
				}
				System.out.println("釋放lock2");
			}
		}).start();
	}








發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章