什么是处理器,可运行和线程之间有什么不同?线程、有什么不同、处理器

2023-09-05 05:10:56 作者:君为红颜醉

处理器之间的区别是什么,Runnable接口,和线程?

虽然我的工作与Android,我需要的东西在后台运行。我使用线程来运行它。通常我会写扩展Thread类,并实现run方法。

我也看到了一些例子,implments可运行并进入可运行成线。

不过我仍然困惑。有人可以给我一个明确的解释吗?

什么是可运行的点,如果可以写后台code在线程的run方法? 如何用内螺纹的处理程序,为什么我们需要使用它。 在Android有另一件事通话runOnUiThread,我们如何使用呢?我知道,它是用于更新UI。 解决方案

为什么要使用可运行在主题?

的Runnable 分离需要异步运行,从code 如何的code运行。这样可以使你的code灵活。例如,异步code处于可运行可以在一个线程池,或者一个专门的线程中运行。

A 的说明你的可运行可能不需要访问。有机会获得更多的政府不必要设计很糟糕。

线程占用了大量的内存。为每一个小动作一个新的线程需要处理的时间分配和释放此内存。

什么是runOnUiThread实际上在做什么?

Android的 runOnUiThread 排队一个的Runnable 到UI线程上执行。这是因为你永远不应该更新从多个线程的用户界面是非常重要的。 runOnUiThread 使用处理程序

请注意,如果在UI线程的队列已满,或者项目需要执行是冗长的,它可能是一段时间之前,你的排队的Runnable 实际运行。

什么是处理器?

在处理程序允许你发布可运行在特定线程中执行。在幕后,runOnUi线程队列中的的Runnable 与Android的用户界面处理程序,以便您可运行能在UI线程安全运行。

What is the difference between Handler, Runnable, and Threads?

While I was working with android, and I need something to run in the background. I use Threads to run it. Usually I would write a class that extends Thread and implement the run method.

怎样查看CPU几个线程

I have also saw some examples that implments runnable and pass into runnable into Threads.

However I am still confused. Can someone give me a clear explanation?

What is the point of Runnable if one can write the background code in the Thread's run method? How is Handler used inside thread and why do we need to use it. Android has another thing call runOnUiThread, How do we use that? I know that it is used for updating the UI.

解决方案

Why use Runnable over Thread?

Runnable separates code that needs to run asynchronously, from how the code is run. This keeps your code flexible. For instance, asynchronous code in a runnable can run on a threadpool, or a dedicated thread.

A Thread has state your runnable probably doesn't need access to. Having access to more state than necessary is poor design.

Threads occupy a lot of memory. Creating a new thread for every small actions takes processing time to allocate and deallocate this memory.

What is runOnUiThread actually doing?

Android's runOnUiThread queues a Runnable to execute on the UI thread. This is important because you should never update UI from multiple threads. runOnUiThread uses a Handler.

Be aware if the UI thread's queue is full, or the items needing execution are lengthy, it may be some time before your queued Runnable actually runs.

What is a Handler?

A handler allows you to post runnables to execute on a specific thread. Behind the scenes, runOnUi Thread queues your Runnable up with Android's Ui Handler so your runnable can execute safely on the UI thread.