线程的死锁
/**
* 线程死锁,当多个线程访问同一份资源的时候就有可能发生死锁
* 过多的同步方法可能造成死锁
* @author Administrator
*
*/
public class SynDeath {
public static void main(String[] args) {
Object m = new Object();
Object g = new Object();
Test1 t1 = new Test1(g, m);
Test2 t2 = new Test2(g, m);
Thread th1 = new Thread(t1);
Thread th2 = new Thread(t2);
th1.start();
th2.start();
}
}
class Test1 implements Runnable{
Object goods;
Object money;
public Test1(Object goods, Object money) {
super();
this.goods = goods;
this.money = money;
}
@Override
public void run() {
while(true){
test();
}
}
public void test(){
synchronized(goods){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized(money){
}
}
System.out.println("一手给货");
}
}
class Test2 implements Runnable{
Object goods;
Object money;
public Test2(Object goods, Object money) {
super();
this.goods = goods;
this.money = money;
}
@Override
public void run() {
while(true){
test();
}
}
public void test(){
synchronized(money){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized(goods){
}
}
System.out.println("一手给钱");
}
}
生产者消费者模式
public class Player implements Runnable {
private Movie m;
public Player(Movie m) {
super();
this.m = m;
}
@Override
public void run() {
for(int i=0;i<10;i++){
if(i%2==0){
m.play("表演动作片");
}else{
m.play("表演爱情片");
}
}
}
}
public class watcher implements Runnable{
private Movie m;
public watcher(Movie m) {
super();
this.m = m;
}
@Override
public void run() {
for(int i=0;i<10;i++){
m.watch();
}
}
}
public class Movie {
private boolean flag=true;
private String pic;
public synchronized void play(String pic){
if(!flag){
try {
this.wait(); //如果标识为错的话,停止演出,进行观看
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//开始演出
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
//演出完毕
this.pic = pic;
System.out.println(pic);
//通知观看,也就是吧当前暂停的线程唤醒
this.notify();
//停止演出
this.flag = false;
}
public synchronized void watch(){
if(flag){
try {
this.wait(); //停止观看,进行演出
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//开始观看
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
//观看完毕
System.out.println(pic+"被观看了");
//通知表演,同时观看停止
this.notify();
this.flag = true;
}
public static void main(String[] args){
Movie m = new Movie();
Player p = new Player(m);
watcher w = new watcher(m);
new Thread(p).start();
new Thread(w).start();
}
}
输出结果:
表演动作片
表演动作片被观看了
表演爱情片
表演爱情片被观看了
表演动作片
表演动作片被观看了
.........
任务调度
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
/**
* Timer()
* void schedule(TimerTask task, Date time)
安排在指定的时间执行指定的任务。
void schedule(TimerTask task, Date firstTime, long period)
安排指定的任务在指定的时间开始进行重复的固定延迟执行。
* 自学Quartz框架:http://www.quartz-scheduler.org/
*/
public class TimerDemo {
public static void main(String[] args) {
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
System.out.println("SO EASY...");
}
}, new Date(System.currentTimeMillis() + 1000), 500);
}
}