Balking Pattern

        这种模式主要是如果对于一个警戒条件成立,那么就可以执行,如果不满足警戒条件,那么只需要返回,不做任何工作。

        示例:Data类表示数据,字段content表示内容,changed字段表示是否改变,如果content改变了而没有存储,那么该字段为true,如果content没有改变或者改变了却已经存储了,那么该字段为false。

        change方法改变数据,save方法保存数据,仅当changed为true时候才保存。

        SaverThread是一个每隔一秒就保存一次数据的线程。

        ChangeThread是一个每隔一秒改变数据并且将其保存的线程。

        那么应该解决如何:当数据没有改变时,而SaveThread尝试保存数据的问题、以及当数据改变时两个线程先后尝试保存数据的问题。

import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;

public class Data {
	private String filename;
	private String content;
	private boolean changed;//修改内容后是否保存
	
	public Data(String filename,String content){
		this.filename=filename;
		this.content=content;
		changed=true;
	}
	public synchronized void chang(String s){
		content=s;
		changed=true;
	}
	public synchronized void save() throws IOException{
		if(!changed)
			return;
		doSave();
		changed=false;
	}
	private void doSave() throws IOException {
		System.out.println(Thread.currentThread().getName()+" save "+content);
		Writer writer=new FileWriter(filename);
		writer.write(content);
		writer.close();
	}
}

 

import java.io.IOException;

public class SaverThread extends Thread{
	private Data data;
	public SaverThread(String name,Data data){
		super(name);
		this.data=data;
	}
	public void run(){
		try{
			while(true){
				data.save();
				Thread.sleep(1000);
			}
		}catch(IOException e){
			e.printStackTrace();
		}catch(InterruptedException e){
			e.printStackTrace();
		}	
	}
}

 

import java.io.IOException;
import java.util.Random;

public class ChangerThread extends Thread{
	private Data data;
	private Random random=new Random();
	public ChangerThread(String name,Data data){
		super(name);
		this.data=data;
	}
	public void run(){
		try{
			for(int i=0;true;i++){
				data.chang("No."+i);
				Thread.sleep(random.nextInt(1000));
				data.save();
			}
		}catch(IOException e){
			e.printStackTrace();
		}catch(InterruptedException e){
			e.printStackTrace();
		}
	}
}

 

public class Test {
	public static void main(String[] args){
		Data data=new Data("data.txt","(empty)");
		new ChangerThread("ChangerThread",data).start();
		new SaverThread("SaverThread",data).start();
	}
}

         通常有两种方式表示balk的发生,一种是用boolean值,一种是用异常表示,这时会从throw中出异常而不是用return返回。

        Guarded Suspension Pattern 中当警戒条件不成立时会一直等待到成立;而Balking Pattern中当警戒条件不成立时会直接balk退出。在这两种模式中有一种折中方案:在条件成立之前等待一段时间。即timeout。

obj.wait(1000);

 让线程进入对象obj的等待区域暂停,并且释放obj的锁定。如果一下条件发生线程才会退出等待区域:一、被notify或者notifyAll唤醒;二、对这条线程执行interrupt方法,这是线程重新获得obj的锁定,并抛出InterruptException;三、发生了timeout,即1s钟大概过了,线程又获得obj的锁定。

public class TimeoutException extends Exception{
	public TimeoutException(String smg){
		super(smg);
	}     
}

 

public class Host {
        private final long timeout;
        private boolean ready=false;
        public Host(long timeout){
        	this.timeout=timeout;
        }
        public synchronized void setExecutable(boolean ready){
        	this.ready=ready;
        	notifyAll();
        }
        public synchronized void execute() throws TimeoutException, InterruptedException{
        	long start=System.currentTimeMillis();  //开始计时
        	while(!ready){
        		long now=System.currentTimeMillis(); //现在的时间
        		long rest=timeout-(now-start);
        		if(rest<=0){
        			throw new TimeoutException("rest="+rest+";timeout="+timeout);
        		    //setExecutable(true);
        		}
        		else{
        			wait(rest);//等待还剩下的时间,如果rest小于0呢(没有机会执行的)?
        		}
        	}
        	doExecute();  
        }
        private void doExecute(){
        	System.out.println(Thread.currentThread().getName()+" doExecute ");
        }
}

 

public class Main {
	public static void main(String[] args){
		Host host=new Host(10000);
		System.out.println("Main Begin");
		try {
			host.execute();
		} catch (TimeoutException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}

 运行结果;

Main Begin
TimeoutException: rest=0;timeout=10000
    at Host.execute(Host.java:18)
    at Main.main(Main.java:7)

 

        synchronized没有timeout,也不能中断。synchronized块中无法设置timeout。

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值