Search code examples
javamultithreadingrunnable

Java runnable problems


Currently this is my code

package com.raggaer.frame;

import java.awt.Dimension;
import java.awt.Graphics;

import javax.swing.JFrame;

public class Frame {

    private JFrame frame;

    public Frame() {

        this.frame = new JFrame("Java Snake");
        this.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);      
        this.frame.add(new Panel());

        Paint game = new Paint();

        this.frame.setResizable(false);
        this.frame.pack();
        this.frame.setVisible(true);

    }
}

And this is my paint class

package com.raggaer.frame;

public class Paint implements Runnable {

    private Thread thread;

    public Paint() {

        thread = new Thread(this);
        thread.start();
    }

    public void run() {

        System.out.println("aaa");

    }

}

But the System.out.println("aaa"); is just executing one time instead of forever.. what Im doing wrong?


Solution

  • You'll have to loop in the run() method if you want it to run forever. Otherwise the thread finishes execution and exits.