我从未见过Java以这种方式行事,但它似乎绕过了我的循环。
我正在使用带有2个类的slick2D。
图形
public class graphic扩展了BasicGame {
public static int POS1 = 100;
public static int POS2 = 100;
public static String text = "f";
public static boolean test = false;
public static boolean test3 = false;
public static boolean w = false;
public static boolean m = false;
public static boolean left = false;
public static boolean right = false;
Boolean test2 = false;
public graphic(String gamename){
super(gamename);
}
public void start() {
try{
AppGameContainer appgc;
appgc = new AppGameContainer(new graphic("ProjectES"));
appgc.setDisplayMode(1300, 800, false); //width, height
appgc.start();
}catch(SlickException es){
System.out.println("MAJOR ERROR, GRAPICH.START");
}
}
@Override
public void render(GameContainer gc, Graphics g) throws SlickException {
g.setColor(Color.white);
g.drawString("Y " + Main.year + " M" + Main.month + " W" + Main.week, 550, 10);
if(Objectsellinghandler.test == true){
//g.fillRect(100, 100, 500, 150); //window
Image popup1 = new Image("res/Window.png");
popup1.draw(370,250);
g.setColor(Color.orange);
g.drawString(Main.text0, 380,260);
g.drawString(Main.text1, 380,280);
g.drawString(Main.text2, 380,300);
g.drawString(Main.text3, 380,320);
g.drawString(Main.text4, 380,340);
}
if(Objectsellinghandler.test == true){
//g.fillRect(100, 100, 500, 150); //window
Image popup1 = new Image("res/Window.png");
popup1.draw(370,250);
g.setColor(Color.orange);
g.drawString(Main.text0, 380,260);
g.drawString(Main.text1, 380,280);
g.drawString(Main.text2, 380,300);
g.drawString(Main.text3, 380,320);
g.drawString(Main.text4, 380,340);
}
//Image img = new Image("res/image.png");
//img.draw(POS1,POS2);
}
@Override
public void init(GameContainer arg0) throws SlickException {
// TODO Auto-generated method stub
}
@Override
public void update(GameContainer gc, int arg1) throws SlickException {
Input input = gc.getInput();
Main.MainInput = input.toString();
if(test2 == false){
Main.MainInput = "w";
text = "gg";
System.out.println("gg");
test2 = true;
Main.year++;
Thread time = new Thread(new Time());
time.start();
}
if(input.isKeyDown(Input.KEY_W) && test3 == false){
test3 = true;
test = true;
w = true;
m = false;
left = false;
right = false;
System.out.println("test1111");
Thread t1 = new Thread(new Objectsellinghandler());
t1.start();
}
if(input.isKeyDown(Input.KEY_M)){
m = true;
w = false;
left = false;
right = false;
System.out.println("RLLY");
}
if(input.isKeyDown(Input.KEY_LEFT)){
left = true;
m = false;
w = false;
right = false;
}
if(input.isKeyDown(Input.KEY_RIGHT)){
right = true;
left = false;
m = false;
w = false;
}
Main.MainInput = "";
//if(){
//test2 = true;
// System.out.println("WHY ARE YOU RUNNING");
//Thread t1 = new Thread(new Objectsellinghandler());
//t1.run();
// }
}
}
Objectsellinghandler。
public class Objectsellinghandler implements Runnable{
public static boolean test = false;
public void beginning(){
}
@Override
public void run() {
System.out.println("gg");
test = true;
if(Main.rep < 1){
Main.text0 = "You are starting out with $" + Main.money + " from a bank loan, to";
Main.text1 = "start you off, were buying 100 units of a red lamp";
Main.text2 = "from china, for $2600.";
Main.text4 = "Press M to continue.";
int I = 0;
while(graphic.m = false){
System.out.println(I);
}
System.out.println(graphic.m);
graphic.m = false;
Main.MainInput = "";
/*Main.text0 = "";
Main.text1 = "";
Main.text2 = "";
Main.text4 = "";
*/
Main.money = Main.money - RedlampI.priceperhundred;
RedlampI.amount = RedlampI.amount + 100;
Main.text0 = "The items have been purchased, it will be here in one";
Main.text1 = "month";
double month = Main.month;
double monthcomparing = month + 1;
System.out.println("month " + Main.month + "monthcomparing " + monthcomparing + "Main month" + Main.month);
while(monthcomparing != month){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
month = Main.month;
}
System.out.println("month " + Main.month + "monthcomparing " + monthcomparing + "Main month" + Main.month);
Main.text0 = "The shipment is here, shipping costed $200";
Main.money = Main.money - 200;
Main.text1 = "You have " + Main.money + " money left, A good price is $35 but you";
Main.text2 = "can choose any price between $20 and $40 for this item.";
Main.text4 = "Press M to continue.";
I = 0;
while(graphic.m = false){
System.out.println(Main.MainInput);
}
graphic.m = false;
Main.MainInput = "";
test = false;
Main.text0 = "";
Main.text1 = "";
Main.text2 = "";
Main.text4 = "";
RedlampI objectselling1 = new RedlampI();
objectselling1.onsale();
Thread.currentThread().interrupt();
return;
}
}
}
绕过等待布尔值“m”的循环!即使它是假的。
此代码似乎在创建线程之前运行!
double month = Main.month;
double monthcomparing = month + 1;
monthcomparing + "Main month" + Main.month);
这种多线程非常非常奇怪,已经超过24小时了,我无法弄清楚原因。
答案 0 :(得分:4)
while(graphic.m = false)
,除了是一个自旋锁,因而是一件坏事,是对graphic.m
的赋值,并且总是立即为假。
使用while(graphic.m == false)
将使您的代码&#34;更正&#34;但仍然很糟糕。使用while(!graphic.m)
稍微不那么糟糕,但使用更好的等待策略是正确的事情。
更新:现在,我想一想,这应该会产生一个&#34;无法访问的代码&#34;错误。编译时常量false
仅允许if
,而不是while
。