2

So I want the one thread to constantly update how much "gold" you have, and the other one would be running a game.

The problem is that the cursor is being changed for each, I fixed it at first by having them not in sync, with sleep, but when I want the user to input text, it will go to the first line. Anyone know how to fix this?

import java.awt.*;
import java.lang.*;
import java.util.*;
import java.awt.Color;
import hsa.Console;


public class Money {
    public static long gold = 0;
    public static long rate = 1;
    public static void main(String args[]) {
        Console con = new Console (41, 100);
        ThreadTest test1 = new ThreadTest();
        ThreadTest2 test2 = new ThreadTest2();
        test1.setConsole(con);
        test2.setConsole(con);
        test1.start();
        test2.start();
    }
}

public class ThreadTest extends Thread {
    Console c; 
    public void setConsole(Console con) {
        c = con;
    }
    public void run() {
        try {
            sleep(900);
        } catch (InterruptedException e) {
        }
        c.println("Welcome to the world of grind\nThe goal of the game is to amass money and get stuff!\nWhat would you like to do?\n\n<q>uest | <s>hop | <i>nventory");
        c.setCursor(5,1);
        String choice = c.readString();
        }
}

public class ThreadTest2 extends Thread {
    Console c; 
    public void setConsole(Console con) {
        c = con;
    }
    public void run(){
        do
        {
            Money.gold = Money.gold + 1*Money.rate;
            c.setCursor(1,1);
            c.print("You currently have: " + Money.gold + " gold");
            try {
                sleep(1000);
            } catch (InterruptedException e) {
            }
        }
        while (true); 
    }
}

1 Answer 1

2

It sounds like you need to synchronize via your console object, so that only one thread can use it at once. For example with your ThreadTest2:

public class ThreadTest2 extends Thread {
    Console c; 
    public void setConsole(Console con) {
        c = con;
    }
    public void run(){
        do
        {
            Money.gold = Money.gold + 1*Money.rate;
            synchronized(c) {
                c.setCursor(1,1);
                c.print("You currently have: " + Money.gold + " gold");
            }
            try {
                sleep(1000);
            } catch (InterruptedException e) {
            }
        }
        while (true); 
    }
}

As long as all console output from all threads is via the same instance of the console object, and as long as each thread synchronizes like the above example, it should fix the problem.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.