New topic several times

I want to start a stream 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 this button several times. Are there a few lines that still exist after the post "I'm in the stream" is posted? Or every time the start function is completed, the thread is destroyed?

If I create multiple threads that work simultaneously, how can I close them my own way?

Thank you for your help!

+5
source share
4 answers

Are there several threads that still exist after the message "I'm in the stream" is printed?

No. Each of them will be automatically destroyed.

If I create several threads that work simultaneously, how can I close them in a clean way?

No need to stop threads, they will be destroyed automatically as soon as they finish their task (launch execution).

To handle concurrency and security, you should refer to java.util.concurrent , which is a utility for handling concurrency in java.

+8
source

Are there multiple threads still existing after the message "I'm in the thread"? Or every time the start function is completed, the thread is destroyed?

In your case, you create a lot of threads, because a stream is created for each button click.

Thread lifespan is completed after the last statement in the run() method has completed. After the run() method is executed, the thread will enter the TERMINATED State , and it cannot be restarted.

A better solution does not create a new thread for every button click. If you need more threads in your application, go to the thread pool.

Java provides the Executor framework for this purpose. It better manages the life cycle of threads.

Use one of the APIs that will return the ExecutorService from Executors

eg. newFixedThreadPool(4)

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

If I create multiple threads that work simultaneously, how can I close them my own way?

You can close the ExecutorService as indicated below in the SE message:

How to close java ExecutorService correctly

Since you are using Android, you have another good alternative to multithreading: HandlerThread and Handler

See below for more details.

Android: toast in the stream

+2
source

Creating a thread every time is a bad idea using a thread pool.

+1
source

create a class that implements Runnable instead of an anonymous thread ... pass runnable object that creates as many threads as you like. Since creating an object, Anonymous Runnable creates only one object, which limits you to achieve your requirements. Check the status of the thread before creating another, or create a thread group (amortized) or a thread pool using concurrency, you use the callable instead of runnable and transfer it to a thread pool of a certain size or you can convert runnable to callable and then transfer its pool as many times as you need like

 class Thop implements Runnable { public void run() { // operation } } 
0
source

Source: https://habr.com/ru/post/1238508/


All Articles