无法同时运行2个线程

时间:2016-11-27 12:16:19

标签: java multithreading

我有一个名为GameThread的超类:

public class GameThread implements Runnable {
public static Thread thread;
public static boolean running = false;
public static double fps = 1;
public static double timePerTick = 1000000000 / fps;

public static ArrayList<GameThread> gameThreads = new ArrayList<>();

public void run(){
    double delta = 0;
    long now;
    long lastTime = System.nanoTime();
    long timer = 0;

    while(running){
        now = System.nanoTime();
        delta += (now - lastTime) / timePerTick;
        timer += now - lastTime;
        lastTime = now;

        setFps();

        if(delta >= 1){
            action();
            delta--;
        }

        if(timer >= 1000000000){
            timer = 0;
        }
    }
}

public synchronized void start(){
    if (running) return;
    running = true;
    startThread();
    thread.start();
}

public void action(){}
public void setFps(){}
public void startThread(){}

2个线程是GameThread的孩子:

public class JumpThread extends GameThread {
public JumpThread(){
    start();
}

@Override
public void action(){
    //code
}

@Override
public void setFps(){
    //code
}

@Override
public void startThread(){
    thread = new Thread(new JumpThread());
}

public class HydroponicsThread extends GameThread {
public HydroponicsThread(){
    start();
}

@Override
public void action(){
    //code
}

@Override
public void setFps(){
    //code
}

@Override
public void startThread(){
    thread = new Thread(new HydroponicsThread());
}

我的问题是,当我初始化它们时:

GameThread.gameThreads.add(new HydroponicsThread());
GameThread.gameThreads.add(new JumpThread());

只有第一个(HydroponicsThread)有效,&#34; run()&#34;另一方法不被称为。 我的猜测是while循环停止了JumpThread的初始化,但是如果没有它我就找不到办法。提前谢谢。

2 个答案:

答案 0 :(得分:1)

我在start()方法中找到了我的错误:

import java.util.Scanner;

class RomanNumeral {
    public static String romanNumeralToInt(int romanNumeral) {
        String Numeral = "";
        int repeat;

        int value[] = {1000, 500, 100, 50, 10, 5, 1 };
        String symbol[] = {"M", "D", "C", "L", "X", "V", "I" };

        for(int x = 0; romanNumeral > 0; x++) {
            repeat = romanNumeral / value[x];

            for(int i = 1; i <= repeat; i++) {
                Numeral = Numeral + symbol[x];
            }

            romanNumeral = romanNumeral % value[x];
        }

        return Numeral;
    }

    public static void main(String args[]){
        Scanner input = new Scanner(System.in);

        final String INVALID = "Invalid number, try again!";

        final int VALIDATE_NUMBER_1 = 1;
        final int VALIDATE_NUMBER_5 = 5;
        final int VALIDATE_NUMBER_10 = 10;
        final int VALIDATE_NUMBER_50 = 50;
        final int VALIDATE_NUMBER_100 = 100;
        final int VALIDATE_NUMBER_500 = 500;
        final int VALIDATE_NUMBER_1000 = 1000;

        while (true) {
            System.out.print("Enter a number: ");
            int inputValue = input.nextInt();

            if (inputValue == VALIDATE_NUMBER_1) {
                System.out.println(VALIDATE_NUMBER_1 + " = " + romanNumeralToInt(1));
            }
            else if (inputValue == VALIDATE_NUMBER_5) {
                System.out.println(VALIDATE_NUMBER_5 + " = " + romanNumeralToInt(5));
            }
            else if (inputValue == VALIDATE_NUMBER_10) {
                System.out.println(VALIDATE_NUMBER_10 + " = " + romanNumeralToInt(10));
            }
            else if (inputValue == VALIDATE_NUMBER_50) {
                System.out.println(VALIDATE_NUMBER_50 + " = " + romanNumeralToInt(50));
            }
            else if (inputValue == VALIDATE_NUMBER_100) {
                System.out.println(VALIDATE_NUMBER_100 + " = " + romanNumeralToInt(100));
            }
             else if (inputValue == VALIDATE_NUMBER_500) {
                System.out.println(VALIDATE_NUMBER_500 + " = " + romanNumeralToInt(500));
            }
             else if (inputValue == VALIDATE_NUMBER_1000) {
                System.out.println(VALIDATE_NUMBER_1000 + " = " + romanNumeralToInt(1000));
            }
             else {
                 System.out.println(INVALID);
             }
        }

    }
}

另外,我从线程&#39;中删除了start()方法。构造

答案 1 :(得分:0)

为了避免拥抱&#34; cpu你应该在run方法的操作中引入一些暂停,使用 sleep wait

检查Difference between wait() and sleep()以了解您的选项。