0

Hi I am doing a rather simple "karaoke" program... I'm trying to change shown text with a java Thread that starts on mouse click. When there is no loop and i click the mouse repetitively it works but When I add the infinite while loop into thread.run() it becomes stuck... it does nothing... what am I doing wrong? here is my code:

public class Timer extends Thread {
MainWindow window;
public int timeSec;
ArrayList<Integer> times;
public Song song;

public Timer(MainWindow window){
    times = new ArrayList<Integer>();
    times.add(10);      // de alto
    times.add(50);      // el carino
    times.add(70);      // cuando juanita 
    times.add(92);      // Limpia el
    times.add(113);     // de alto
    times.add(160);     // sabes
    times.add(215);     // la cosa esta + o.J
    times.add(226);     // mira
    times.add(244);     // ref
    times.add(266);     // matus
    times.add(272);     // Janka + krik
    times.add(293);     // mira

    song = new Song();
    this.window = window;
    timeSec = 0;
    //run();
}

public void start(){
    run();
}

public void run(){
    while (true){
        try {
            sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        timeSec++;
        if (times.contains(timeSec)){
            song.next();
        }
        window.repaint();

    }
} 
}
3
  • 2
    while(true) keeps running ? You don't say ?? Commented Feb 6, 2015 at 13:48
  • In your run method, what is the purpose of while(true){} Commented Feb 6, 2015 at 13:50
  • its not that it keep running... it should call song.next() and repaint the JPanel... and i need it to go forever... Commented Feb 6, 2015 at 13:55

2 Answers 2

6

You've override start() method of Thread. So once you call start() no actual thread are spawn. See how to override thread.start() method in java?.

Sign up to request clarification or add additional context in comments.

2 Comments

plus one, the moral of the story is don't sub-class Thread unless you can't avoid it. It just leads to confusion.
tahank you... its been some time since i used thread so i didnt remember that
0

Nikolay Ivanov already posted the answer, I'll try to add some context:

In the Thread class all code that actually creates a new native thread is called from the start()-method. Your own implementation for start() hides the one in Thread and does not call it via super() - so it is basically just like any other method you might implement and does not do any thread-creation. As a result your infinite loop is run on the main-thread (or the EDT in swing) and thus freezes your application.

So the best way really is not to mess around inside the Thread-class. Instead create a Runnable, pass it to a Thread-constructor and start() the thread - way less possibilities to do things wrong.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.