0

I have some problems with my code. For some reason my thread.start() doesn't activate my run() method. In pure desperation I have simply replaced my code in run() with a printing function, but nothing is being printed. Can someone help me by explaining what is wrong in my code?

public class Screen extends JPanel implements Runnable{
    Thread thread = new Thread();

    Frame frame;

    public Screen(Frame frame){
        this.frame = frame;
        frame.setSize(horizontal * 25 + 24 , (vertical) * 25 + 48);
        this.frame.addKeyListener(new KeyHandler(this));
        thread.start();
    }
    public void run(){
        System.out.println("Boom");
    }
}

I got a lot of stuff in between and under this code, but this is the only part essential to the thread and frame.

tshepang
  • 12,111
  • 21
  • 91
  • 136
  • when you create a Thread. which thread? It doesnt have any reference to tie back to your class. it should be new Thread(this) isnt it? – Hrishikesh Jan 08 '14 at 14:30
  • when you implement thread using Runnable u need to pass object of that class to thread.... here do the following.... **new Thread(this)** – Mitesh Pathak Jan 08 '14 at 14:32
  • Btw, it is considered a bad pattern to fork a thread in an object constructor. This causes a leak of `this` to the other thread before the object is fully constructed. It is a better pattern to add a `screen.start()` method which calls `thread.start()`. – Gray Jan 08 '14 at 14:33

5 Answers5

4

You must pass the Thread a Runnable. Since thread is an instance variable and the class implements Runnable I guess you want to do this:

Thread thread = new Thread(this);

But be careful when calling overridable methods from a constructor and be more careful if these methods are called by a separate thread that runs the code in parralel to the constructor initialization. It might run while the constructor still initializes the object. Think about what will happen if you subclass Screen, override the run method and the run method accesses properties of the superclass Screen while it is initializing.

Also see What's wrong with overridable method calls in constructors?

Community
  • 1
  • 1
René Link
  • 48,224
  • 13
  • 108
  • 140
0

This is because your thread doesn't know about the run method. You can do it by changing

Thread thread = new Thread();

to

Thread thread = new Thread(this);

because your class is an instance of Runnable.

p.s. try to avoid messing with threads and Swing. Use SwingWorkers if you really have to.

Silviu Burcea
  • 5,103
  • 1
  • 29
  • 43
0

Thread needs a Runnable instance, which has run() method to call. But you are not providing Runnable instance to Thread.

Do Thread t = new Thread(this);

Narendra Pathai
  • 41,187
  • 18
  • 82
  • 120
0

You are creating a simple thread.

Thread thread = new Thread();

It does call run() method but of class Thread and not your Screen class runnable implementation.

You can do

Thread thread = new Thread(new Screen());
Aniket Thakur
  • 66,731
  • 38
  • 279
  • 289
0

You have two solutions:
1) Using the override run method in your class

Thread thread = new Thread(this);

2) Using new Runnable

Thread th = new Thread(new Runnable() {

        @Override
        public void run() {
            // your code

        }
    });
Ahmed Hamdy
  • 2,547
  • 1
  • 17
  • 23