线程并发库(计时器,同步)

本文介绍了如何使用Java计时器定时执行任务,包括设置不同间隔的周期性任务,以及演示了在JDK1.4中实现线程同步的三种方式:同步代码块、同步方法和静态同步方法。
摘要由CSDN通过智能技术生成

计时器:

	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();
	}








  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

傅荣康

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值