上杭网站建设公司,推广计划书怎么写,网站设计用什么字体,企业介绍怎么写呢akka与neety乒乓是一个经典示例#xff0c;其中2个玩家#xff08;或线程#xff09;访问共享资源–乒乓表并在彼此之间传递Ball#xff08;状态变量#xff09;。 使用任何共享资源#xff0c;除非我们同步访问#xff0c;否则线程可能会遇到潜在的死锁情况。 PingPong… akka与neety 乒乓是一个经典示例其中2个玩家或线程访问共享资源–乒乓表并在彼此之间传递Ball状态变量。 使用任何共享资源除非我们同步访问否则线程可能会遇到潜在的死锁情况。 PingPong算法非常简单 如果轮到我{ 更新下一轮 ping / pong-记录点击 通知其他线程 }其他{ 等待通知 } 让我们来看一个例子看看它是如何工作的 这是我们的Player类它实现Runnable并接受对共享资源和消息的访问 public class Player implements Runnable {PingPong myTable; Table where they playString myOpponent;public Player(String opponent, PingPong table) {myTable table;myOpponent opponent;}public void run() {while (myTable.hit(myOpponent));}} 其次我们看到PingPong表类该类具有一个同步方法hit无论是否轮到我都将在其中进行检查。 如果轮到我了请记录ping并更新对手姓名的共享变量。 public class PingPong {state variable identifying whose turn it is.private String whoseTurn null;public synchronized boolean hit(String opponent) {String x Thread.currentThread().getName();if (x.compareTo(whoseTurn) 0) {System.out.println(PING! ( x ));whoseTurn opponent;notifyAll();} else {try { wait(2500); } catch (InterruptedException e) { }}}} 接下来我们开始游戏并使玩家开始 public class Game {public static void main(String args[]) {PingPong table new PingPong();Thread alice new Thread(new Player(bob, table));Thread bob new Thread(new Player(alice, table));alice.setName(alice);bob.setName(bob);alice.start(); alice starts playingbob.start(); bob starts playingtry {Wait 5 secondsThread.sleep(5000);} catch (InterruptedException e) {}table.hit(DONE); cause the players to quit their threads.try {Thread.sleep(100);} catch (InterruptedException e) {}}} 就是这样我们正在运行PingPong游戏。 在这种情况下我们看到了同步方法hit如何仅允许一个线程访问共享资源–其Turn。 Akka STM提供了两个构造Refs和Agents。 引用事务引用提供对多个身份的协调同步访问。 代理提供对单个身份的非协调异步访问。 参考 在我们的例子中由于共享状态变量是单个标识因此引用的使用是过大的但是我们仍然会继续查看它们的用法。 public class PingPong {updates to Ref.View are synchronousRef.ViewString whoseTurn;public PingPong(Ref.ViewString player) {whoseTurn player;}public boolean hit(final String opponent) {final String x Thread.currentThread().getName();if (x.compareTo(whoseTurn.get()) 0) {System.out.println(PING! ( x ));whoseTurn.set(opponent);} else {try {wait(2500);} catch (Exception e) {}}}} 这里的关键是以下 同步关键字丢失 将状态变量定义为Ref //对Ref.View的更新是同步的 Ref.View string其Turn; 调用更新Ref是协调和同步的 whoTurn.setopponent ; 因此当我们使用Ref保持状态时对Ref的访问会在事务中自动同步。 代理商 由于代理提供了不协调的异步访问因此使用代理进行状态操纵将意味着我们需要等到所有更新都已应用到代理之后。 代理为获取提供非阻塞访问。 public class PingPong {AgentString whoseTurn;public PingPong(AgentString player) {whoseTurn player;}public boolean hit(final String opponent) {final String x Thread.currentThread().getName();wait till all the messages are processed to make you get the correct value, as updated to Agents areasyncString result whoseTurn.await(new Timeout(5, SECONDS));if (x.compareTo(result) 0) {System.out.println(PING! ( x ));whoseTurn.send(opponent);} else {try {wait(2500);} catch (Exception e) {}}return true; keep playing.}} 这里的关键是以下 同步关键字丢失 将状态变量定义为Agent //对Ref.View的更新是同步的 Agent string其Turnn; 等待对代理的更新因为对代理的更新是异步的 字符串结果 whoTurn.awaitnew Timeout5SECONDS; 调用更新Ref是协调和同步的 whoTurn.sendopponent ; 这些示例中引用的所有代码都可以在– https://github.com/write2munish/Akka-Essentials/tree/master/AkkaSTMExample/src/main/java/org/akka/essentials/stm/pingpong中找到 示例1 –用于基于普通线程的同步 示例2 –使用Refs进行同步 示例3 –使用代理进行同步 参考在Akka Essentials博客上与我们的JCG合作伙伴 Munish K Gupta 一起使用STM玩 -Refs 和Agents 。 翻译自: https://www.javacodegeeks.com/2012/05/akka-stm-playing-pingpong-with-stm-refs.htmlakka与neety