public class Publish extends Observable{ private String data = ""; public String getData() { return data; } public void setData(String data) { if(!this.data.equals(data)){ this.data = data; setChanged(); } notifyObservers("~ ~ ~"); } public static void main(String[] args) { Publish publish = new Publish(); for(int i=0;i<10;i++){ new Subscribe(i,publish); } publish.setData("开始"); } } public class Subscribe implements Observer{ private int i; public Subscribe(int i, Observable observable){ this.i = i; observable.addObserver(this); } @Override public void update(Observable o, Object arg) { System.out.println(i + "收到通知:"+((Publish)o).getData()+arg); } }