1

I have a thread Class that implements Thread. I want to have 3 instances of this class and each of these instances has different implementation of the run method of the thread class. Can i do that? I tried passing parameters to the run method, but no luck. This is what ive tried so far.

class thread extends Thread{
    int reelValue;
    JButton name;
    boolean stop;
    Reel r;
    int k;

    thread(int reelValue, JButton name, boolean stop, Reel r, int k){
        this.name = name;
        this.reelValue = reelValue;
        this.stop = stop;
        this.r = r;
        this.k = k;
    }
    public void run(){
        r = new Reel();
        while (!stop){
             k = ThreadLocalRandom.current().nextInt(0, 5 + 1);
            name.setIcon(r.symbols.get(k).getImage());
            reelValue = r.symbols.get(k).getValue();
            try {
                sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }

}

public static void main(String[]args) {
    thread t1 = new thread(reelOneValue, reel1,stopT1, r1, a);
    thread t2 = new thread(reelTwoValue, reel2, stopT2, r2, b);
    thread t3 = new thread(reelThreeValue, reel3, stopT3, r3, c);

    t1.start();
    t2.start();
    t3.start();
}
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • 1
    What's not working with the code you have? – John Kugelman Dec 17 '16 at 14:53
  • It throws a NullPointer Exception.. – Manuka Hatharasinghe Dec 17 '16 at 14:53
  • Accessing Swing components from a thread other than the event dispatch thread is not thread-safe, and will thus cause troubles. Respoect the Java naming conventions, choose better names than "thread", and read the swing tutorial about concurrency: https://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html – JB Nizet Dec 17 '16 at 14:54
  • The approach you've taken is fine. It sounds like the real problem is the null pointer exception, so see the linked question for debugging tips. – John Kugelman Dec 17 '16 at 14:57
  • 1
    *it throws a NullPointerException*: then read the stack trace of the exception, and fix your code to avoid it. I don't want to be rude, but dealing with threads correctly is quite an advanced, very complex topic, whereas dealing with a NullPointerException is something quite basic. Learn the simple stuff first, before diving into threads. – JB Nizet Dec 17 '16 at 14:57
  • @JBNizet You were kinda rude, but anyway i fixed the Exception. Thank You! – Manuka Hatharasinghe Dec 17 '16 at 15:22
  • if you really want 3 different implementations of the run method, then you have to have 3 different subclasses of class Thread instead of one. – Alexei Kaigorodov Dec 17 '16 at 21:23
  • Thats how i did it before. But is it really a good practice to do that/? – Manuka Hatharasinghe Dec 18 '16 at 07:59
  • "I have a thread Class that implements Thread": no you don't. You have a class that *extends* `Thread`. Be precise. – user207421 Jun 18 '21 at 09:54

0 Answers0