java乒乓_java – 正确实现乒乓游戏

我被要求执行一个名为“ping”和“pong”的pingpong游戏(意思是ping之前没有pong)10次.意思是,控制台中的最终输出应该是:“ping!(1)”,“pong!(1)”,“ping!(2)”,“pong!(2)”等.

需求是使用信号量,reetrantlock和倒计时锁存器实现gamepingpongthread.

我的问题是打印顺序并不总是如我所要求的那样,我想知道我做错了什么.

这是代码:

// Import the necessary Java synchronization and scheduling classes.

import java.util.concurrent.Semaphore;

import java.util.concurrent.CountDownLatch;

import java.util.concurrent.locks.ReentrantLock;

import java.util.concurrent.locks.Condition;

/**

* @class PingPongRight

*

* @brief This class implements a Java program that creates two

* instances of the PlayPingPongThread and start these thread

* instances to correctly alternate printing "Ping" and "Pong",

* respectively, on the console display.

*/

public class PingPongRight

{

/**

* @class SimpleSemaphore

*

* @brief This class provides a simple counting semaphore

* implementation using Java a ReentrantLock and a

* ConditionObject.

*/

static public class SimpleSemaphore

{

private int mPermits;

private ReentrantLock lock = new ReentrantLock();

private Condition isZero = lock.newCondition();

/**

* Constructor initialize the data members.

*/

public SimpleSemaphore (int maxPermits)

{

mPermits = maxPermits;

}

/**

* Acquire one permit from the semaphore.

*/

public void acquire() throws InterruptedException

{

lock.lock();

while (mPermits == 0)

isZero.await();

mPermits--;

lock.unlock();

}

/**

* Return one permit to the semaphore.

*/

void release() throws InterruptedException

{

lock.lock();

try {

mPermits++;

isZero.signal();

} finally {

lock.unlock();

}

}

}

/**

* Number of iterations to run the test program.

*/

public static int mMaxIterations = 10;

/**

* Latch that will be decremented each time a thread exits.

*/

public static CountDownLatch latch = new CountDownLatch(2);

/**

* @class PlayPingPongThread

*

* @brief This class implements the ping/pong processing algorithm

* using the SimpleSemaphore to alternate printing "ping"

* and "pong" to the console display.

*/

public static class PlayPingPongThread extends Thread

{

private String message;

private SimpleSemaphore semaphore;

/**

* Constructor initializes the data member.

*/

public PlayPingPongThread (String msg, SimpleSemaphore pingOrPong)

{

message = msg;

semaphore = pingOrPong;

}

/**

* Main event loop that runs in a separate thread of control

* and performs the ping/pong algorithm using the

* SimpleSemaphores.

*/

public void run ()

{

for (int i = 1 ; i <= mMaxIterations ; i++) {

try {

semaphore.acquire();

System.out.println(message + "(" + i + ")");

semaphore.release();

} catch (InterruptedException e) {

e.printStackTrace();

}

}

latch.countDown();

}

}

/**

* The main() entry point method into PingPongRight program.

*/

public static void main(String[] args) {

try {

// Create the ping and pong SimpleSemaphores that control

// alternation between threads.

SimpleSemaphore pingSemaphore = new SimpleSemaphore(mMaxIterations);

SimpleSemaphore pongSemaphore = new SimpleSemaphore(mMaxIterations);

System.out.println("Ready...Set...Go!");

// Create the ping and pong threads, passing in the string

// to print and the appropriate SimpleSemaphores.

PlayPingPongThread ping = new PlayPingPongThread("Ping!", pingSemaphore);

PlayPingPongThread pong = new PlayPingPongThread("Pong!", pongSemaphore);

// Initiate the ping and pong threads, which will call the run() hook method.

ping.start();

pong.start();

// Use barrier synchronization to wait for both threads to finish.

latch.await();

}

catch (java.lang.InterruptedException e)

{}

System.out.println("Done!");

}

}

提前致谢

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值