Java 线程问题
我希望能够使用线程在我的程序中同时运行两个循环,以实现类似于游戏的程序。然而,我并不完全确定如何使用线程,我遇到了一些我一无所知的错误。我将发布代码,但忽略大部分代码,它只是我想要的一个有点空洞的外壳,我只需要让线程工作,开始清理所有代码。Java 线程问题,java,multithreading,Java,Multithreading,我希望能够使用线程在我的程序中同时运行两个循环,以实现类似于游戏的程序。然而,我并不完全确定如何使用线程,我遇到了一些我一无所知的错误。我将发布代码,但忽略大部分代码,它只是我想要的一个有点空洞的外壳,我只需要让线程工作,开始清理所有代码。 我得到的错误是“不是抽象的,不重写”,它突出显示了“类游戏实现可运行”部分。代码如下: public class game implements Runnable { @Override public void run(int time){
我得到的错误是“不是抽象的,不重写”,它突出显示了“类游戏实现可运行”部分。代码如下:
public class game implements Runnable
{
@Override
public void run(int time){
try{
time = 1;
while (time<=10){
Thread.sleep(10);
System.out.print(time);
time++;
}
char clearer = (char)(12);
System.out.print(clearer);
System.out.println("You have ran out of time! Game over!");
System.exit(0);
}catch (Exception e){}
}
public static void main (String args[]) throws Exception{
System.out.println("Welcome to pseudo-Where's Wally, look for the lower cal l character.");
Thread.sleep(500);
System.out.println("You get 10 seconds to find it.");
Thread.sleep(500);
System.out.println("Ready?..");
char clear = (char)(12);
Thread.sleep(500);
System.out.print(clear);
boolean timer = false, col = false, guess = false;
int length = 5, time = 0, rowNum = 1, y = 0;
String manip = ("");
int x = (length*length);
Random gen = new Random();
int find = gen.nextInt(x)+1;
for (int collumn = 0; collumn<=length; collumn++){
for (int row = 0; row<=length; row++){
while (!col){
y++;
System.out.print(" "+y);
if (y-1 == length){
System.out.println();
System.out.println();
col = true;
}
}
System.out.print(" I");
manip = manip + (" I");
}
System.out.println("\t" + rowNum);
rowNum++;
manip = manip + (" I");
}
boolean contin = false;
do{
if (find%3==0){
contin = true;
find = find - 1;
} else if (find%3>0){
find = find - 1;
}
}while (!contin);
String newManip = manip.substring(0,find)+'l'+manip.substring(find+1);
String subOne = newManip.substring(0,18);
String subTwo = newManip.substring(18,36);
String subThree = newManip.substring(36,54);
String subFour = newManip.substring(54,72);
String subFive = newManip.substring(72,90);
String subSix = newManip.substring(90,108);
System.out.println(subOne);
System.out.println(subTwo);
System.out.println(subThree);
System.out.println(subFour);
System.out.println(subFive);
System.out.println(subSix);
Thread threadA = new ThreadA();
threadA.start();
while(guess != true){
System.out.print("Answer (Only one try): ");
int answer = sc.nextInt();
if (answer == finalAnswer){
guess = true;
}
}
}
}
公共类游戏实现可运行
{
@凌驾
公共无效运行(整数时间){
试一试{
时间=1;
while(time为了实现Runnable
你需要重写run()
方法(除非你的类是抽象的
,这打开了另一个讨论)。你在游戏中的类是run(int-time)
,它不算在内
从API:
Runnable接口应由其实例将由线程执行的任何类实现。该类必须定义一个名为run的无参数方法
粗体由我添加。您的错误与线程无关。您不重写run()
方法,因此您并没有真正实现可运行的
。如果您知道错误是什么,您能帮忙吗?编写一个新程序,它只生成两个线程,每个线程在一个循环中打印一些内容,然后从那里构建。