计时器:
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(); }