0

I want to start a thread many times, without creating a new Thread evrytime when I need to call my function in a own thread. That´s the reason why I called .run instead of .start because .start can only be called once. So how can I solve my problem?

I have a UI-Programm and want that after pressing any button...

...the first thread disable all buttons

...the second thread enable the buttons, which should be pressed next.

So I have a method in the first thread which disable all buttons and I run it.

Then I run the second thread which enables the buttons. The second thread sleeps for 5 sec and then enables the buttons.

Thread updateUIthread, disableUIthread;
Runnable disableUIrun = new Runnable() {
    @Override
    public void run() {
        disableUserInput();
    }
};
Runnable updateUIrun = new Runnable() {
    @Override
    public void run() {
        updateUIinteraction();
    }
};


public void updateUIinteraction() {

 try {
            Thread.sleep(1000);
        } catch (InterruptedException ex) {
            Logger.getLogger(VorgangController.class.getName()).log(Level.SEVERE, null, ex);
        }

// enable buttons
}

// and when I need it:

disableUIthread.run();
updateUIthread.run();

But when I press a button, the effect is that both threads are sleeping. Nothing happens for 5 sec. then all buttons are disabled and enabled quickly.

user2784676
  • 105
  • 1
  • 12
  • 1
    You should call `start()` to run it on a separate thread, not `run()`, which is executed on the **current** thread. – Mark Rotteveel Apr 24 '17 at 09:30
  • 1
    where are you `start()`ing the threads? If you call run() you are calling that method in the current thread. – Peter Lawrey Apr 24 '17 at 09:30
  • ok, but I want to call those threads multiple times. Because I need to call the included methods multiple times, but just when I need it not in a loop. And I think the start-Method is just for calling it once, right? – user2784676 Apr 24 '17 at 09:37
  • duplicate :/ The posted answer isn´t really answering my question and I need 50 Rep to comment it?? – user2784676 Apr 24 '17 at 11:08

0 Answers0