简体   繁体   English

新线程多次

[英]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! 谢谢你的帮助!

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. 要处理并发性和安全性,您应该查看java.util.concurrent ,它是用于处理java中并发性的实用程序框架。

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. 在您的情况下,您创建了许多线程,因为为每个按钮单击创建了一个Thread。

The life span of Thread is over after completion of last statement in run() method. run()方法中完成最后一个语句后,Thread的生命周期结束。 After execution of run() method, the thread will enter into TERMINATED State and it can't be re-run. 执行run()方法后,线程将进入TERMINATED 状态 ,无法重新运行。

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. Java为此提供了Executor框架。 It manages Thread life cycle in better way. 它以更好的方式管理Thread生命周期。

Use one of APIs, which will return ExecutorService from Executors 使用API中的一个,将返回ExecutorService执行人

eg newFixedThreadPool(4) 例如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: 您可以按照以下SE帖子中的引用关闭ExecutorService

How to properly shutdown java ExecutorService 如何正确关闭java ExecutorService

Since you are using Android, you have one more good alternative for multithreading : HandlerThread and Handler 由于您使用的是Android,因此您还有一个很好的多线程替代方案: HandlerThreadHandler

Refer to below post for more details: 有关详细信息,请参阅以下帖子:

Android: Toast in a thread Android:在线程中敬酒

每次创建线程都是一个坏主意使用线程池。

create a class implementing Runnable instead of Anonymous thread ... pass runnable object creating as many threads as you likeSince creating Anonymous Runnable object creates only one object thus limiting you to achieve your requirements. 创建一个实现Runnable而不是匿名线程的类...传递runnable对象创建尽可能多的线程因为创建Anonymous Runnable对象只创建一个对象,从而限制您实现您的要求。 Check the thread status before creating another or create a thread group (depreciated) or a thread pool using concurrency you use callable instead of runnable and pass it to thread pool of definite size or you could convert runnable to callable and then pass it to the pool as many times as you like 在创建另一个线程组或创建线程组(折旧)或线程池之前使用可调用而不是runnable来检查线程状态,并将其传递给一定大小的线程池,或者将runnable转换为callable,然后将其传递给池多少次你喜欢

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM