`

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。

 

 

 

分享到:
评论

相关推荐

    Java多线程模式之Balking模式详解

    主要介绍了Java多线程模式之Balking模式,结合实例形式较为详细的分析了Balking模式的原理、用法与相关注意事项,需要的朋友可以参考下

    Java多线程详解

    4、Balking ———— 不需要的话,就算了吧 5、Producer-Consumer ———— 我来做,你来用 6、Read-Write Lock ———— 大家想看就看吧,不过看的时候不能写哦 7、Thread-Per-Message ———— 这个工作交给你了 8...

    java多线程设计模式 (PDF中文版, 附源码)

    第4章 Balking——不需要的话,就算了吧 第5章 Producer-Consumer——我来做,你来用 第6章 Read-Write Lock——大家想看就看吧,不过看的时候不能写喔 第7章 read-Per-Message——这个工作交给你了 第8章 Worker ...

    JCS简介

    ● 完全可配置的运行时参数 ● 局部数据分割和配置 ● 纤细粒度的元素配置选项 ● 远程同步 ● 远程存储恢复 ● 非阻塞 "zombie" (balking facade(等待外观)) 模式 ● 横向的元素分发...

    JCS 实践

    ● 完全可配置的运行时参数 ● 局部数据分割和配置 ● 纤细粒度的元素配置选项 ● 远程同步 ● 远程存储恢复 ● 非阻塞 "zombie" (balking facade(等待外观)) 模式 ● 横向的元素分发...

    java多线程设计模式详解(PDF及源码)

    第1章 Single Threaded Execution——能通过这座桥的,只有一个人 第2章 Immutable——想破坏它也没办法 第3章 Guarded Suspension——要等到我准备好喔 第4章 Balking——不需要的话,就算了吧 第5章 Producer-...

    java8源码-GraphicMultiThreadDesignPattern:图解Java多线程设计模式

    +---balking--------------------------------停止返回模式 +---future---------------------------------等待返回模式 +---builder--------------------------------建造者模式 +---guardedSuspension-------------...

    汪文君高并发编程实战视频资源全集

    │ 高并发编程第二阶段30讲、多线程Balking设计模式-下.mp4 │ 高并发编程第二阶段31讲、多线程Producer and Consumer设计模式.mp4 │ 高并发编程第二阶段32讲、多线程Count Down设计模式.mp4 │ 高并发编程第二...

    汪文君高并发编程实战视频资源下载.txt

    │ 高并发编程第二阶段30讲、多线程Balking设计模式-下.mp4 │ 高并发编程第二阶段31讲、多线程Producer and Consumer设计模式.mp4 │ 高并发编程第二阶段32讲、多线程Count Down设计模式.mp4 │ 高并发编程第二...

Global site tag (gtag.js) - Google Analytics