I never seen Java act this way, but it seems to be bypassing my loop.
I'm using slick2D with 2 classes.
Graphic
public class graphic extends 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;
}
}
}
The loop waiting for boolean "m" is instatly bypassed! Even though it is false.
And this code seems to be running before the thread is created!
double month = Main.month;
double monthcomparing = month + 1;
monthcomparing + "Main month" + Main.month);
This multithreading is being really, really strange and its been over 24 hours and I can't figure out why.
while(graphic.m = false)
, in addition to being a spinlock and thus a Bad Thing, is an assignment tographic.m
and will always be immediately false.Using
while(graphic.m == false)
will make your code "correct" but still Bad. Usingwhile(!graphic.m)
is a little less bad, but using a better waiting strategy is the Right Thing.Update: Now that I think about it, this should be producing an "unreachable code" error. A compile-time constant of
false
is only permitted inif
, notwhile
.