This site requires JavaScript, please enable it in your browser!
Greenfoot back
Kartoffelbrot
Kartoffelbrot wrote ...

2014/1/18

Problems with Threads

Kartoffelbrot Kartoffelbrot

2014/1/18

#
- No Greenfoot - Hello dear programmers, I have a bug in my program and it'S hard for me to solve it. If I start my application the frame opens itself, but then the program doesn't update anymore. THis is the code of the main class:
import javax.swing.JFrame; 
/**
 * @author: Paul Maximilian Bittner
 * 
 * This class controls the speed and performance of the program.
 */
public class Control implements Runnable
{
    /**Fields**/
    public final static int SPEEDMAX = 1000;
    public final static String name = "Spiel";

    /**variables**/
    private static Control me;
    private static Frame frame;
    private static Thread mainThread;

    private boolean running, acting;
    private int speed = 1;

    private Control(){
        running = true;
        acting = true;
    }

    public void run() {
        while(running){
            //System.out.println("running");
            if(acting){
                //System.out.println("acting");
                frame.refresh();
                frame.repaint();
                try{
                    mainThread.sleep(SPEEDMAX - speed);
                }catch(Exception e){}
            }
        }
    }

    public static void main(String args[]) {
        me = new Control();
        
        me.frame = new Frame(name);
        me.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        me.frame.setResizable(false);
        me.frame.setSize(800, 600);
        me.frame.setVisible(true);
        
        mainThread = new Thread(me);
        mainThread.start();
    }
}
As you can see, there are two System.out.println invokings commented. If I uncomment them everything works perfectly. I can move my character and so on. But if the System.out.println invokings aren't there the program doesn't move! What's the fault? Why does the program only realizes, that it should do something if I have the out.printlns there?
Kartoffelbrot Kartoffelbrot

2014/1/18

#
any suggestions?
bourne bourne

2014/1/18

#
Interesting. No exceptions thrown? Please change catch(Exception e) to catch(InterruptedException e) And just to try something out, try doing the following:
private static volatile Frame frame;  
private static volatile Thread mainThread;
Kartoffelbrot Kartoffelbrot

2014/1/18

#
Hm, it's the same.
bourne bourne

2014/1/18

#
Try doing this: private volatile boolean running, acting; I'm at a loss, since you have methods being invoked inside the loop. I think I came across something like this before where something like this: for (int i = 0; i < 999999999; i++) {} would not get executed (or maybe it could just do it really fast) as oppose to: for (int i = 0; i < 999999999; i++) System.out.println(i); There are optimizations I think that Java/Compiler might do such as determining if a recursion can be done linear/iterative and so forth. But again I'm confused with your example since there are things being done in the if(acting) block.
Kartoffelbrot Kartoffelbrot

2014/1/18

#
Wow, thank you, now it works fine!
Kartoffelbrot Kartoffelbrot

2014/1/19

#
But now there is a new problem. In my BlueJ project everything works, as it should, but if I ecport the project to a jarfile the world is empty and nothing happens. I figured out so far, that the program runs, but I don't know, if it's in the "acting-block". Has anyone an idea, what the fault is?
kiarocks kiarocks

2014/1/19

#
Where is the character added? I can't see anything related to the character, and there could very well be something vital going on with that.
danpost danpost

2014/1/20

#
@kiarocks, the class given above or an instance of it becomes a runnable thread that controls how another program runs. It would be the program it controls that may have characters. Chances are the class would not be used for a Greenfoot scenario, but for either a stand-alone or something else. I do not have extensive knowledge on threads and stand-alones, so I may have mis-wrote on one or two (or more) areas. Looking back, it is given that it is a BlueJ project that is exported to a jar file.
bourne bourne

2014/1/20

#
Sorry Kartoffelbrot, I can't speculate what could be wrong after exporting to a jar.
kiarocks kiarocks

2014/1/20

#
I wanted to test this, but I don't have a Frame class besides java's frame, so I can't speak for what yours does. If I re structure for JFrame it's fine.
Kartoffelbrot Kartoffelbrot

2014/1/20

#
Thanks to you all. danpost is right so far. @ kiarocks: I use a JFrame, too and paint onto a JLabel subclass. In older projects I didn't have this problem, too, but now I can't help myself.
You need to login to post a reply.