我的问题是我的代码正在跳过自己,在这里停止循环:
while (!engineOn) {
System.out.println("Push on. ");
engStatus = input.nextLine();
if (engStatus.equals("on")) {
engineOn = true;
System.out.println("Starting Engine ");
rpm = idleRpm;
System.out.println("RPM: " + rpm + "\nGear: " + gear);
} else {
engineOn = false;
System.out.println("Engine off ");
}
}
在InputThread类中。
输出到达此处:
Output:
MultiThreaded Practice - RPM Shifter
line 26
Push on.
on
Starting Engine
RPM: 800
Gear: 1
终止..
忽略我尚未解决的代码中的其他问题。
我尝试过将线程设置为不同的优先级。仍然没有运气。我以前遇到过这个问题,但我从来没有试图找出原因,只是继续前进。现在,了解为什么会发生这种情况对我来说很重要。
这是我的代码..我已经到处寻找答案,大多数建议我的代码可能是错误的。但是因为这只是一种练习......这是占用我太多的空闲时间。我试图练习线程并使用一些随机的花絮我已经学会了......
我还不熟悉编码,更不用说java了。
任何提示都会有所帮助。
package Engine;
import java.util.Scanner;
import Engine.Engine.Gear;
import Engine.Engine.InputThread;
import Engine.Engine.RpmCycle;
public class App extends Thread {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("MultiThreaded Practice - RPM Shifter ");
// System.out.println("Type start to run program. ");
// String run = input.nextLine();
// Call threads.. Wait on input for thread kill or gear switch
RpmCycle rpm = new RpmCycle();
Gear gear = new Gear();
InputThread control = new InputThread();
Thread.currentThread().setPriority(2);
control.setPriority(8);
gear.setPriority(8);
rpm.setPriority(8);
control.start();
System.out.println("line 26");
while (Engine.engineOn == true) {
rpm.start();
gear.start();
control.start();
}
if(Engine.engineOn==false && Engine.getRpm() != 0){
System.out.println("You lost: Engine Blown ");
}
}
}
package Engine;
import java.util.Scanner;
public class Engine {
private static int rpm = 0, idleRpm = 800, rpmLimit = 8000, gear = 1,
maxGear = 6, waitTime = 100;
public static boolean engineOn = false, asked = false;
public static long startTime = System.currentTimeMillis();
public static String shift = "";
public static int getRpm() {
return rpm;
}
static Scanner input = new Scanner(System.in);
public static class Gear extends Thread {
@Override
public void run() {
while (shift == "" && asked != true) {
System.out.println("Shift? ");
if(shift.equals("1")){
gear++;
System.out.println(gear);
}
shift="";
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
System.out.println(e);
}
}
}
public static class RpmCycle extends Thread {
@Override
public void run() {
if (rpm <= rpmLimit) {
System.out.println("RPM: " + rpm + "\nGear: " + gear);
rpm += 500;
} else {
System.out.println("Engine Blown ");
engineOn = false;
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
System.out.println(e);
}
}
}
public static class InputThread extends Thread {
@Override
public void run() {
@SuppressWarnings("resource")
Scanner input = new Scanner(System.in);
String engStatus = null;
while (!engineOn) {
System.out.println("Push on. ");
engStatus = input.nextLine();
if (engStatus.equals("on")) {
engineOn = true;
System.out.println("Starting Engine ");
rpm = idleRpm;
System.out.println("RPM: " + rpm + "\nGear: " + gear);
} else {
engineOn = false;
System.out.println("Engine off ");
}
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
System.out.println(e + ": line 70 in input thread");
}
}
}
}