I'm doing a school project. I am building a text based game on Java for PC. The game I'm building is quite simple, you buy homes and you rent them out. So what I am asking is, how can i get the money to automatically increase per second ($1 per second) from each house and then automatically add it to their users bank account automatically. I have looked around and they say use a thread to pause the game for 1000(milliseconds) and then do counter++. But I have tried that, and for a text based game that pauses the game and makes the user wait. I want the user to continue interacting with other functionalities of the text based game whilst the money per second in his bank is increasing.
-
3Instead of trying to do manual thread management, create a [ScheduledExecutorService](http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ScheduledExecutorService.html) that runs once per second and does any required updating. – azurefrog Nov 18 '15 at 18:31
-
@Mazzoseven It will be good If you can show a sample program output. – user3437460 Nov 18 '15 at 18:46
2 Answers
I agree that putting in a thread to sleep for 1000 ms is probably the best solution. The issue that you seem to have encountered when trying that solution is likely caused not using multithreading. The Thread.sleep(1000);
command should be on a separate thread from the main thread that you are using for the user interface.
The following could be a possible way to implement the thread that modifies the bank balance:
public class RevenueThread implements Runnable {
public void run() {
while(true){
// add to bank balance
MainClass.BankBalance += MainClass.PropertyCount * INCOME_PER_PROPERTY;
// sleep for 1 second
try{
Thread.sleep(1000);
}catch(Exception ex){
System.err.println( ex.getMessage() );
}
}
}
}
Modify that code to your needs with the proper variable names and whatnot.
To integrate this with your code, you could add this to your main() function:
Runnable rev = new RevenueThread();
Thread revThread = new Thread(rev);
revThread.start();`
Note: I apologize if my answer seems somewhat brief or if it contains any errors. I am typing this solution from my phone, so bear with me :P
EDIT: The following is an alternative (and perhaps more accurate) way to increment the bank balance every second:
public class RevenueThread implements Runnable {
public void run() {
// Variable to keep track of payout timing:
long lNextPayout = System.currentTimeMillis() + 1000; // Current time + 1 second
while(true){
if(lNextPayout <= System.currentTimeMillis()){
// At least 1000 milliseconds have passed since the last payout
// Add money to the player's bank balance
MainClass.BankBalance += MainClass.PropertyCount * INCOME_PER_PROPERTY;
// Now set up the next payout time:
lNextPayout += 1000;
}
// sleep for 50 milliseconds to prevent CPU exhaustion
try{
// Thread.sleep() can throw an InterruptedException.
Thread.sleep(50);
}catch(Exception ex){
// If sleep() is interrupted, we should catch the exception
// and print the error message to the standard error stream
// (STDERR) by using System.err
System.err.println( ex.getMessage() );
}
}
}
}
What's different about this version and why is it better? This version uses the system's current time to payout every 1000 milliseconds. Because sleep()
can possibly throw an exception, this updated version prevents a user from being paid multiple times within 1 second just because sleep()
threw an exception and did not sleep for the full second.
How can this be used? This can be used in the exact same way as the previous version. (I.e., just create a new RevenueThread
object, then create a Thread
object for it, and call .start()
on that new thread.) Again, though, you should replace and rename variables as needed to fit into your project.

- 3,376
- 2
- 27
- 43
Since you are doing a text based game and it is not indicated in your question whether this will be a multiplayer or single player game, if this is a single player game, I will not implement this simulation (game) in real time.
It is probably more suitable to implement a discrete-time simulation (which means you don't have to use threads). You can create each house as an object with a currentTime
attribute. Every time a house is rented, update its currentTime
. Whenever you need to check the bank account for the money received from renting. Check the elapsed renting time of each house and update your bank account accordingly.

- 17,253
- 15
- 58
- 106