线程问题

时间:2014-10-17 22:37:58

标签: java multithreading

我希望能够在我的程序中使用线程一次运行两个循环,用于某种类似程序的游戏。但是,我不完全确定如何使用Threads,我遇到了一些我一无所知的错误。我会发布代码,但忽略大部分内容,它是我想要它的一个空洞的外壳我只需要让线程工作开始清理它。
我收到错误“不是抽象的,不会覆盖”,它突出了“类游戏实现可运行”部分。这是代码:

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 个答案:

答案 0 :(得分:1)

要实施Runnable,您需要覆盖run()方法(除非您的班级为abstract,这会打开另一个讨论)。您game课程中的内容为run(int time),不计算在内。

来自Runnable API:

  

Runnable接口应由任何其实例由线程执行的类实现。 该类必须定义一个名为run 的无参数的方法。

Bold由我添加。