您的位置:首页 > 其它

Immutable

2016-04-19 00:00 381 查看
摘要: 当数据不可以修改的时候,就没有数据同步的必要,自然就没有临界区了。

Immutable

Immutable pattern 也是多线程设计时候的一个准则,初学多线程把数据能准确分辨会变和不会变就可以前面两种模式混合使用,这个是一个比较基础,但却是十分重要的一个使用。

public class Main {
public static void main(String args[]){
People people=new People("cdk","chengdu");
new PrintThread(people).start();
new PrintThread(people).start();
new PrintThread(people).start();
}
}

public final class People {
private final String name;

private final String address;

public People(String name, String address) {
this.name = name;
this.address = address;
}

public String getName(){
return name;
}

public String getAddress(){
return address;
}

public String toString(){
return "[ person : name ="+name+" , address = "+address+" ]";
}
}

public class PrintThread extends Thread {
private People people;

public PrintThread(People people) {
this.people=people;
}

public void run(){
while(true){
System.out.println(Thread.currentThread().getName()+" "+people);
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: