Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

New thread multiple times

I want to run a thread when I press a button

public void ButtonClick(){

    Thread thread = new Thread(){
        public void run(){
            Log.i("Test", "I'm in thread");
        }
    };
    thread.start();
}

My question is : I want to click several times on this button. Are several thread still existing after the message "I'm in thread" is printed? Or each time the run function is finished, the thread is destroyed?

In case I create several threads which are running at the same time, how can I close them in a clean way?

Thanks for your help!

like image 803
Stochelo Avatar asked Dec 17 '15 10:12

Stochelo


People also ask

Can a thread be run multiple times?

You cannot run a thread more than once. Once a thread finishes, it's done.

What happens if you start a thread twice?

After starting a thread, it can never be started again. If you does so, an IllegalThreadStateException is thrown. In such case, thread will run once but for second time, it will throw exception.

Can you start a thread multiple times Python?

you can only start a specific instance of a thread once. but in your case, the for loop is looping over different instances of a thread, each instance being assigned to the variable thread in the loop, so there is no problem at all in calling the start() method over each thread.

How do I run a thread twice?

A java Thread cannot be run twice. Once it has been started and finished its work, it cannot be started again (calling method start will fail). So you'll have to create a new instance of Thread (using the same Runnable ) and start it.


2 Answers

Are several thread still existing after the message "I'm in thread" is printed?

No. Each of them will be destroyed automatically.

In case I create several threads which are running at the same time, how can I close them in a clean way?

No need to stop threads, they will be destroyed automatically once they finish their task(execution of run).

To handle the concurrency and safety you should look in to java.util.concurrent which is utility framework for handling concurrency in java.

like image 158
akash Avatar answered Sep 23 '22 20:09

akash


Are several thread still existing after the message "I'm in thread" is printed? Or each time the run function is finished, the thread is destroyed?

In your case, you are creating many threads since a Thread is created for every button click.

The life span of Thread is over after completion of last statement in run() method. After execution of run() method, the thread will enter into TERMINATED State and it can't be re-run.

Better solution is not creating a new Thread for every button click. If you need more Threads in your application, go for a pool of Threads.

Java provides Executor framework for this purpose. It manages Thread life cycle in better way.

Use one of APIs, which will return ExecutorService from Executors

e.g. newFixedThreadPool(4)

Have a look at this post and this article for more options.

In case I create several threads which are running at the same time, how can I close them in a clean way?

You can shutdown the ExecutorService as quoted in below SE post:

How to properly shutdown java ExecutorService

Since you are using Android, you have one more good alternative for multithreading : HandlerThread and Handler

Refer to below post for more details:

Android: Toast in a thread

like image 30
Ravindra babu Avatar answered Sep 26 '22 20:09

Ravindra babu