日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2024/4/20 编程问答 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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!");

}

}

提前致謝

總結

以上是生活随笔為你收集整理的java乒乓_java – 正确实现乒乓游戏的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。