观察者机制 在非UI线程中访问UI
package com.shishuo.myplugin1.test; public class Main { private static int count=0; private static Thread thread; public static void update(){ new Thread{ //建立一个新的线程,实现每隔三秒count累加一次,到10后变为0 public void run(){ while(count<=10){ //在线程中加入监听机制,旨在实现count每一次发生变化都会触发监听事件 //EventProducer是事件源 EventProducer producer=new EventProducer(); //为事件源添加监听器,参数为监听事件 producer.addListener(new EventConsumer); //触发 producer.setValue(""+count); try{ Thread.sleep(3000); }catch(Exception e){ e.printStackTrace(); } count++; if(count==10){ count=0; } } } }.start(); } }
package com.shishuo.myplugin1.test; public class EventProducer{ //监听器要在事件源上注册 ListenerRegister register=new ListenerRegister(); private String value=""; public String getValue(){ return value; } public void setValue(String newValue){ //当有事件触发时,事件源要把事件传递给监听器,监听器会根据事件对象采取相应的方法响应这个事件 value=newValue; ValueChangeEvent event=new ValueChangeEvent(this,value); fireAEvent(event); } public void addListener(ValueChangeListener a){ register.addListener(a); } public void removeListener(ValueChangeListener){ register.removeListener(a); } public void fireAEvent(ValueChangeEvent event){ register.fireAEvent; } }
package com.shishuo.myplugin1.test; import java.util.Vector; public class ListenerRegister{ //存放监听器的Vector private Vector<ValueChangeListener> listeners=new Vector<ValueChangeListener>(); public synchronized void addListener(ValueChangeListener a){ listeners.addElement(a); } public synchronized void removeListener(ValueChangeListener a){ listeners.removeElement(a); } public void fireAEvent(ValueChangeEvent evt){ Vector<ValueChangeEvent> currentListeners=null; synchronized(this){ currentListener=(Vector<ValueChangeEvent>)listeners.clone(); } for(int i;i<currentListener.size();i++){ //事件发生后,监听器接收来自事件源的事件,根据不同的事件调用相应的方法 //遍历监听器数组中的监听器,根据事件调用相应的响应方法 ValueChangeListener listener=(ValueChangeListener)currentListeners.elementAt(i); listener.performed(evt); } } }
package com.shishuo.myplugin1.test; import java.util.EventObject; //事件 public class ValueChangeEvent extends EventObject { private static final long serialVersionUID=767352958358520268L; private String value; public ValueChangeEvent(Object source){ this(source,"0"); } public ValueChangeEvent(Object source,String newValue){ super(source); value=newValue; } public String getValue(){ return value; } }
package com.shishuo.myplugin1.test; import java.util.EventListener; //监听器接口 public interface ValueChangeListener extends EventListener{ public abstract void performed(ValueChangeEvent e); }
package com.shishuo.myplugin1.test; import org.eclipse.swt.widgets.*; import com.shishuo.myplugin1.test.Views.view1; public class EventConsumer implements ValueChangeListener{ //以下方法是事件发生后采取的动作 @Override public void performed(final ValueChangeEvent e){ //在非UI线程中访问UI,必须要有以下语句否则会报以下异常 Display.getDefault().syncExec(new Runnable(){ public void run(){ //这里写对SWT控件的设置 Text text=View1.text1; text.setText(""+e.getValue()); } }); } }
不添加syncExec语句会报以下异常:
Exception in thread "Thread-0" org.eclipse.swt.SWTException: Invalid thread access at org.eclipse.swt.SWT.error(SWT.java:4441) at org.eclipse.swt.SWT.error(SWT.java:4356) at org.eclipse.swt.SWT.error(SWT.java:4327) at org.eclipse.swt.widgets.Widget.error(Widget.java:476) at org.eclipse.swt.widgets.Widget.checkWidget(Widget.java:367) at org.eclipse.swt.widgets.Text.getText(Text.java:1350) at test.Snippet108$1$1.run(Snippet108.java:24)
最后的结果就是在Text中每隔三秒更新一次显示内容为0~9