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