2
所以我想要一個線程不斷更新你有多少「黃金」,另一個線程會運行一個遊戲。Java線程併發性
問題在於光標正在爲每個光標進行更改,我首先通過讓它們與睡眠不同步來固定它,但是當我希望用戶輸入文本時,它將轉到第一行。有人知道怎麼修這個東西嗎?
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);
}
}