• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Official, Blue, Red player multi-threaded problem

 
Vatsa dude
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Can anyone please give me some hints on the approach for solving this problem?

 
Henry Wong
author
Marshal
Pie
Posts: 21417
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Vatsa dude
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21417
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic