1, through the implementation of Runnable interface thread creation
(1). Defines a class that implements the Runnable interface and overrides the Run () method in the interface. Add specific task code or processing logic to the run () method.
(2). Creates an object for the class that implements the Runnable interface.
(3). Creates an object of the thread class that encapsulates the object of the previous Runnable interface implementation class. (Interfaces can implement multiple inheritance)
(4). Invokes the start () method of the thread object, starting the thread. This way the source code for creating threads is as follows:
public class Runnabledemo {public
static void Main (string[] args) {
//Runnable The Run method is an empty method and does not produce any threading behavior. You must explicitly attach a task to the thread
taskdemo demo = new Taskdemo ();
New Thread (Demo). Start ();
New Thread (Demo). Start ();
System.out.println ("Countdown before rocket Launch");
}
Class Taskdemo implements Runnable {
private int countdown = ten;
@Override public
Void Run () {while
(countdown--> 0) {
System.out.println ("#" + Thread.CurrentThread (). GetName () + "("
+ countdown +) ");}
}
execution Result: The following sequence occurs because a thread shares a task and there is no synchronization.
Countdown before launch
#Thread -0 (8)
#Thread -0 (7)
#Thread -0 (6)
#Thread -0 (5)
#Thread -0 (4)
#Thread-0 (3)
#Thread -0 (2)
#Thread -0 (1)
#Thread-1 (8)
#Thread-0 (0)
2. Create thread by inheriting thread class
(1). First define a class to inherit the thread parent class, overriding the run () method in the parent class. Add specific task code or processing logic to the run () method.
(2). The object that creates a Threaddemo class directly can also take advantage of polymorphism, and the variable is declared as the type of the parent class.
(3). Call the Start method, the thread starts, and the implicit call to the run () method. The sample code looks like this:
public class Threaddemo extends Thread {
private int countdown = ten;
@Override public
Void Run () {while
(countdown--> 0) {
System.out.println ("#" + Thread.CurrentThread (). GetName () + "("
+ countdown +) ");
}
public static void Main (string[] args) {
new Threaddemo (). Start ();
New Threaddemo (). Start ();
Because the Start method returns quickly, the main thread can perform other operations, at which point there are two separate threads running
System.out.println ("Countdown before rocket Launch");
Execution Results:Since two threads do not share tasks, and the CPU dispatch order is uncertain, the following results occur, and two threads have their own execution paths and are not shared.
#Thread -0 (9)
#Thread-1 (9)
#Thread-1 (8)
pre-launch Countdown
#Thread-1 (7)
#Thread-1 (6)
#Thread-1 (5)
#Thread-1 (4)
#Thread-1 (3)
#Thread -0 (8)
#Thread -0 (7)
#Thread-1 (2)
#Thread-1 (1)
#Thread-1 (0)
#Thread -0 (6)
#Thread -0 (5)
#Thread -0 (4)
#Thread -0 (3)
#Thread -0 (2)
# Thread-0 (1)
#Thread-0 (0)
3, the comparison of two ways
First of all, analyze the output of two ways, the same is the creation of two threads, why the results are different.
Creating a thread using the implementation Runnable interface allows you to share the same target object (Taskdemo demo = new Taskdemo ();), implementing multiple identical threads to handle the same resource. Then look at an explanation from the JDK: the runnable interface should be implemented by classes that intend to execute their instance through a thread. Class must define a parameter-free method called run. The purpose of designing this interface is to provide a public protocol for objects that you want to execute code at activity time. For example, the thread class implements Runnable. Activation means that a thread has started and has not stopped. In addition, Runnable provides a way to activate a class that is not a thread subclass. By instantiating a thread instance and using itself as a running target, you can run the class that implements runnable without creating a subclass of Thread. In most cases, if you only want to override the run () method without overriding the other Thread methods, you should use the Runnable interface. This is important because unless the programmer intends to modify or enhance the basic behavior of the class, you should not create subclasses for that class.
adopt the Inheritance Thread class method:
(1) Advantages: Simple to write, if you need to access the current thread, without using the Thread.CurrentThread () method, direct use of this, you can get the current thread.
(2) Disadvantage: Because the thread class has inherited the thread class, no other parent class can be inherited.
The implementation of Runnable interface mode:
(1) Advantage: The thread class only implements the Runable interface and can also inherit other classes. In this way, multiple threads can share the same target object, so it is very suitable for multiple identical threads to handle the same resource, so that the CPU code and data can be separated to form a clear model to better embody the object-oriented thinking.
(2) Disadvantage: Programming is slightly more complex, and if you need to access the current thread, you must use the Thread.CurrentThread () method.