darkstar 线程互斥原理以及测试!

测试一:不使用线程互斥:

package cn.vicky.sgs.app; import java.io.Serializable; import java.util.Properties; import cn.vicky.sgs.mo.Player; import cn.vicky.sgs.task.MyThread; import com.sun.sgs.app.AppContext; import com.sun.sgs.app.AppListener; import com.sun.sgs.app.ClientSession; import com.sun.sgs.app.ClientSessionListener; import com.sun.sgs.app.NameNotBoundException; /** * @author Vicky * */ public class MainApp implements AppListener, Serializable { /* * (non-Javadoc) * * @see com.sun.sgs.app.AppListener#initialize(java.util.Properties) */ public void initialize(Properties properties) { AppContext.getDataManager().setBinding("player", new Player()); MyThread m1 = new MyThread(); MyThread m2 = new MyThread(); MyThread m3 = new MyThread(); m1.start(); m2.start(); m3.start(); } /* * (non-Javadoc) * * @see com.sun.sgs.app.AppListener#loggedIn(com.sun.sgs.app.ClientSession) */ public ClientSessionListener loggedIn(ClientSession clientsession) { return null; } }

 

package cn.vicky.sgs.task; import com.sun.sgs.app.AppContext; import cn.vicky.sgs.mo.Player; public class MyThread implements Runnable { private boolean runnning = false; private Player p; public MyThread() { this.p = (Player) AppContext.getDataManager().getBinding("player"); } public void start() { runnning = true; Thread thread = new Thread(this); thread.start(); } public void stop() { runnning = false; } public void run() { while (runnning) { p.noSyncMethod(); } } }

 

package cn.vicky.sgs.mo; import java.io.Serializable; import com.sun.sgs.app.ManagedObject; public class Player implements ManagedObject, Serializable { private static final long serialVersionUID = 1L; private int counter = 0; public void noSyncMethod() { try { counter++; Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + " : " + counter); } public synchronized void syncMethod() { try { counter++; Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + " : " + counter); } }

 

打印:

 

Thread-5 : 3
Thread-4 : 3
Thread-6 : 4
Thread-5 : 6
Thread-6 : 6
Thread-4 : 8
Thread-6 : 9
Thread-4 : 9
Thread-5 : 9
Thread-5 : 12
Thread-4 : 12
Thread-6 : 12
Thread-4 : 15
Thread-5 : 15
Thread-6 : 17

 

由数据可见,darkstar并不能完全的实现线程互斥,因为Thread-5 : 3  Thread-4 : 3 数据是相同的。为什么说是不能完全的实现线程互斥!因为即便Thread-5 : 3  Thread-4 : 3 数据是相同的,但总的数据是改变的:

Thread-5 : 3    3
Thread-4 : 3    4
Thread-6 : 4    5
Thread-5 : 6    6
Thread-6 : 6    7
Thread-4 : 8    8
Thread-6 : 9    9
Thread-4 : 9    10
Thread-5 : 9    11
Thread-5 : 12  12
Thread-4 : 12  13
Thread-6 : 12  14
Thread-4 : 15  15
Thread-5 : 15  16
Thread-6 : 17  17

故darkstar在处理并发性的方案,并非按照线程互斥的思想思想的。更多,请阅读pds服务器端设计文档!

 

 

测试二:使用线程互斥:

修改MyThread类的run()

// p.noSyncMethod();
   p.syncMethod();

 

打印:

Thread-4 : 1
Thread-4 : 2
Thread-4 : 3
Thread-4 : 4
Thread-6 : 5
Thread-5 : 6
Thread-5 : 7
Thread-5 : 8
Thread-5 : 9
Thread-6 : 10
Thread-6 : 11
Thread-6 : 12
Thread-4 : 13
……

 

但,这样的执行条件将导致程序执行事件过长,一个任务必须等待另外一个任务完成后才能使用!而darkstar默认的Task,以100ms为界,显然这样是不符合要求的!

你可能感兴趣的:(thread,properties,测试,服务器,Class,任务)