aspose file tools*
The moose likes Threads and Synchronization and the fly likes Official, Blue, Red player multi-threaded problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Official, Blue, Red player multi-threaded problem" Watch "Official, Blue, Red player multi-threaded problem" New topic
Author

Official, Blue, Red player multi-threaded problem

Vatsa dude
Greenhorn

Joined: Apr 29, 2009
Posts: 22
I need to develop a solution with the following properties: Please note this is not a homework assignment, I was given this problem by a friend of mine to think about. I am new to "Threads" concept. Can anyone advice me on how to start? Do I need to have 3 classes that extend Runnable. What are the important things to consider?

1. Use 3 threads
a. one thread for the Official
b. one thread for the Blue player
c. and thread for the Red player
2. The Official starts the game (with a message to the screen)
3. The players alternatively write their pings (Red) and pongs (Green) to the screen as fast as they can
4. After 10 seconds the official STOPS
5. The players exchange the ball at most one more time
6. Both players threads cleanly stop, writing a corresponding message to the screen
7. The referee writes the last message
8. The program terminates

The output should look like this:

Official: Players, start game
Blue: Ping
Red: Pong
Blue: Ping
Red: Pong
Blue: Ping
Red: Pong
Blue: Ping
Red: Pong

Blue: Ping
Red: Pong
Blue: Ping
Red: Pong
Referee: Players, s
Blue: Ping
Red: Pong
Blue: I’m done playing
Red: I’m done playing

Vatsa dude
Greenhorn

Joined: Apr 29, 2009
Posts: 22
Sorry, I might have got the description wrong..

I need to develop a solution with the following properties: Please note this is not a homework assignment, I was given this problem by a friend of mine to think about. I am new to "Threads" concept. Can anyone advice me on how to start? Do I need to have 3 classes that extend Runnable. What are the important things to consider?

1. Use 3 threads
a. one thread for the Official
b. one thread for the Blue player
c. and thread for the Red player
2. The Official starts the game (with a message to the screen)
3. The players alternatively write their pings (Blue) and pongs (Red) to the screen as fast as they can
4. After 10 seconds the official STOPS
5. The players exchange the ball at most one more time
6. Both players threads cleanly stop, writing a corresponding message to the screen
7. The referee writes the last message
8. The program terminates

The output should look like this:

Official: Players, start game
Blue: Ping
Red: Pong
Blue: Ping
Red: Pong
Blue: Ping
Red: Pong
Blue: Ping
Red: Pong

Blue: Ping
Red: Pong
Blue: Ping
Red: Pong
Official: Players, STOP!!
Blue: Ping
Red: Pong
Blue: I’m done playing
Red: I’m done playing
Vatsa dude
Greenhorn

Joined: Apr 29, 2009
Posts: 22
Hi,
Can anyone please give me some hints on the approach for solving this problem?

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Please note this is not a homework assignment, I was given this problem by a friend of mine to think about.


Whether it is your homework assignment, or your friend's homework assignment is not relevant. The purpose of the assignment is for you to learn -- and generally, the easiest way to learn is to just start.


Can anyone please give me some hints on the approach for solving this problem?


Well, what have you done so far? And just where are you stuck? Meaning what are you confused with?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Vatsa dude
Greenhorn

Joined: Apr 29, 2009
Posts: 22
I will have some code by tonight. Here's my idea.

1)Use the main thread for the Official
2)Create two new threads for Blue and Red player.
3)While the run() method for the player threads are executing, they will always check (within their internal loop), if the official has called STOP.


Here's where I am not very clear

1) How do I synchronize the 2 players - meaning how do I guarantee that there will be a PONG(Red) if and only if there is a preceding PING(Blue). There cannot be a PONG without a PING, right?
2) Players exchange the ball at most one more time after official STOPS the game - Should I control this simply by using a variable or is there some synchrnization technique to achieve this.

I guess my questions will make more sense once I publish the code.

Thanks
Sri
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Vatsa dude wrote:1) How do I synchronize the 2 players - meaning how do I guarantee that there will be a PONG(Red) if and only if there is a preceding PING(Blue). There cannot be a PONG without a PING, right?


There are a few high level tools that can achieve this... but since you are learning, you need to learn the basics first. Have you learned synchronization yet? Have you learned about wait/notify yet?

Vatsa dude wrote:2) Players exchange the ball at most one more time after official STOPS the game - Should I control this simply by using a variable or is there some synchrnization technique to achieve this.


Using the mechanism above, the official is merely another enitity. The task that checks for ping or pong flag, has to check the stop flag as well.

Henry
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Official, Blue, Red player multi-threaded problem