Structure on Threads in Java Swing

8

I make my first consultation available and in advance I appreciate the future answers.

To start I have a form that contains 2 jcombobox :

jcbSerie and jcbPartido .

The form integrates

Runnable()

In the form variables I have a Thread and a int :

Thread Serie = new Thread();
int opt = 1;

When the form is initialized, it executes:

Serie.start();

I initialize it with start() because I am informed that the difference with run() is that it is executed asynchronously. In the fragment run() < = method of execution , there is the code to execute which has the following format:

@Override
public void run() {
    switch (opt) {
        case 1:
               MetodoEjecutado1();
            break;
        case 2:
               MetodoEjecutado2();
            break;
        case 3:
            break;
    }
}

Then, as expected, conditional 1 is executed when the form is executed. But my query is as follows: How or what is the way in which conditional 2 should execute the run() from jcbPartido . To be more precise:

  

Execute the Executed Method1 () only once and the thread is no longer available or destroy it (I am new I am willing to read the answers);

     

In the jcbSerie item change event, if you select a different value than the "Select option" option, unblock the jcombobox jcbPartido.

     

When changing the item in jcbPartido the variable% co_of% that is worth 1 changes to opt , then how can I execute the run () method that only executes once only Method2 () asynchronously, and like the first part, to die or stop when you finish.

Thank you very much for your time!

    
asked by Christian Cespedes 24.05.2018 в 16:37
source

1 answer

0

I see 2 solutions to your problem. A simple but inelegant:

@Override
public void run() {
  try {
    while(true){
      switch (opt) {
          case 1:
              MetodoEjecutado1();
              opt = 3;
              break;
          case 2:
              MetodoEjecutado2();
              opt = 3;
              break;
          case 3:
              //Espera indefinidamente a una modificación externa de opt.
              sleep(100);
              break;
      }
    }
  }catch(InterruptedException e){
    //exception necesaria del sleep.
    e.printStackTrace();
  }
}

In this way, you only have to create an instance of the Series class and start the Thread. If it is necessary to stop the thread, you can do it at any time with Serie.interrupt ();

And a second solution that forces the programmer to create new instances of the Thread every time you want to run this but it is cleaner.

The run () code can only be started once per instance. Once the instructions are finished within run () , the Thread is automatically interrupted.

In this way, you could instantiate Series, decide which option you want to execute, and call the method Serie.start ();

If you are working with Threads i Swing I recommend you research this post and this oracle article.

    
answered by 18.09.2018 в 08:39