java我的代码正在跳转。。练习穿线
我的问题是,我的代码跳过了自身,停在这里的循环:
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");
}
}
}
}
共 (0) 个答案