Java学习札记6:Thread 和 Runnable 的联系和区别(2)
2012-07-24 20:07
330 查看
An application that creates an instance of Thread must provide the code that will run in that thread. There are two ways to do this:
Provide a Runnable object
The Runnable interface defines a single method, run, meant to contain the code executed in the thread. The Runnable object is passed to the Thread constructor, as in the HelloRunnable example:
Subclass Thread
The Thread class itself implements Runnable, though its run method does nothing. An application can subclass Thread, providing its own implementation of run, as in the HelloThread example:
Notice that both examples invoke Thread.start in order to start the new thread.
Which of these idioms should you use? The first idiom, which employs a Runnable object, is more general, because the Runnable object can subclass a class other than Thread. The second idiom is easier
to use in simple applications, but is limited by the fact that your task class must be a descendant of Thread. This lesson focuses on the first approach, which separates the Runnable task from the Thread object that executes the task. Not only is this approach
more flexible, but it is applicable to the high-level thread management APIs covered later.
The Thread class defines a number of methods useful for thread management. These include static methods, which provide information about, or affect the status of, the thread invoking the method.
The other methods are invoked from other threads involved in managing the thread and Thread object. We'll examine some of these methods in the following sections.
参考资料:
http://docs.oracle.com/javase/tutorial/essential/concurrency/runthread.html
Provide a Runnable object
The Runnable interface defines a single method, run, meant to contain the code executed in the thread. The Runnable object is passed to the Thread constructor, as in the HelloRunnable example:
public class HelloRunnable implements Runnable { public void run() { System.out.println("Hello from a thread!"); } public static void main(String args[]) { (new Thread(new HelloRunnable())).start(); } }
Subclass Thread
The Thread class itself implements Runnable, though its run method does nothing. An application can subclass Thread, providing its own implementation of run, as in the HelloThread example:
public class HelloThread extends Thread { public void run() { System.out.println("Hello from a thread!"); } public static void main(String args[]) { (new HelloThread()).start(); } }
Notice that both examples invoke Thread.start in order to start the new thread.
Which of these idioms should you use? The first idiom, which employs a Runnable object, is more general, because the Runnable object can subclass a class other than Thread. The second idiom is easier
to use in simple applications, but is limited by the fact that your task class must be a descendant of Thread. This lesson focuses on the first approach, which separates the Runnable task from the Thread object that executes the task. Not only is this approach
more flexible, but it is applicable to the high-level thread management APIs covered later.
The Thread class defines a number of methods useful for thread management. These include static methods, which provide information about, or affect the status of, the thread invoking the method.
The other methods are invoked from other threads involved in managing the thread and Thread object. We'll examine some of these methods in the following sections.
参考资料:
http://docs.oracle.com/javase/tutorial/essential/concurrency/runthread.html
相关文章推荐
- Java学习札记5:Thread 和 Runnable 的联系和区别(1)
- Java中Runnable 和 Thread的区别及联系
- java thread与runnable联系区别
- java中thread和runnable的区别和联系
- Java中 Thread和Runnable实现多线程的区别和联系
- Java中两种多线程实现方法:Thread类和Runnable接口的联系与区别
- java基础——Thread和Runnable的区别和联系
- Java 多线程 -Thread 和Runnable 的区别和联系
- Java学习从菜鸟变大鸟之三 多线程中Thread 和Runnable的区别与运用
- Java多线程学习-Runnable和Thread的区别
- Java学习从菜鸟变大鸟之三 多线程中Thread 和Runnable的区别与运用
- Java学习提要——浅述'Thread类'和'Runnable接口'的区别
- Java中Runnable和Thread的区别
- java多线程Thread与Runnable的区别与使用深入理解
- Java中Runnable和Thread的区别
- java线程---Runnable和Thread的区别
- Java中继承thread类与实现Runnable接口的区别
- [转] Java中继承thread类与实现Runnable接口的区别
- Java中继承thread类与实现Runnable接口的区别
- Java中Runnable和Thread的区别