android Thread 与 Runable的区别

来源:互联网 发布:牛尔产品怎么样 知乎 编辑:程序博客网 时间:2024/06/05 05:09

Runnable

Represents a command that can be executed. Often used to run code in a different Thread.

 

Thread

A Thread is a concurrent unit of execution. It has its own call stack for methods being invoked, their arguments and local variables. Each application has at least one thread running when it is started, the main thread, in the main ThreadGroup. The runtime keeps its own threads in the system thread group.

There are two ways to execute code in a new thread. You can either subclass Thread and overriding its run() method, or construct a new Thread and pass a Runnable to the constructor. In either case, the start() method must be called to actually execute the new Thread.

Each Thread has an integer priority that affect how the thread is scheduled by the OS. A new thread inherits the priority of its parent. A thread's priority can be set using thesetPriority(int) method.

 

Runnable 里面只有一个abstract 方法 run(), 必须implements 这个方法

Runnable 自身无法单独开启一个线程,它必须依赖于Thread().start(), run()方法才能被执行起来。

 

原创粉丝点击