Java multithreading Summary 1: Two Methods of thread creation and comparison of advantages and disadvantages

Source: Internet
Author: User

 

1. Create a thread by implementing the runnable interface

(1) define a class to implement the runnable interface and override the run () method in the interface. Add specific task code or processing logic to the run () method.

(2) Create the runnable interface implementation class object.

(3) To create a Thread class object, encapsulate the object of the previous runnable interface implementation class. (The interface can implement multi-inheritance)

(4) Call the START () method of the thread object to start the thread.

Sample Code:

01. Package demo. thread;
02.
03. Public class treaddemo1 implements runnable {
04. Private int countdown = 10;
05. @ override
06. // define the task in the run Method
07. Public void run (){
08. While (countdown --> 0 ){
09. system. Out. println ("#" + thread. currentthread (). getname () + "("
10. + countdown + ")");
11 .}
12 .}
13.
14. Public static void main (string [] ARGs ){
15. // The run method in runnable is an empty method and does not produce any thread behavior. A task must be explicitly attached to the thread.
16. treaddemo1 TT = new treaddemo1 ();
17. New thread (TT). Start ();
18. New thread (TT). Start ();
19. system. Out. println ("countdown before rocket launch :");
20 .}
21 .}
22.
Running result:

Countdown before rocket launch:
# Thread-1 (8)
# Thread-1 (7)
# Thread-1 (6)
# Thread-1 (5)
# Thread-1 (4)
# Thread-1 (3)
# Thread-1 (2)
# Thread-1 (1)
# Thread-1 (0)
# Thread-0 (9)

2. Create a thread by inheriting the Thread class

(1) first define a class to inherit the thread parent class and override the run () method in the parent class. Add specific task code or processing logic to the run () method.
(2) directly create a threaddemo2 class object, or use polymorphism to declare the variable as the type of the parent class.

(3) Call the start method, thread t to start, and implicitly call the run () method.

Sample Code:

View plaincopy to clipboardprint?
01. Package demo. thread;
02.
03. Public class threaddemo2 extends thread {
04. Private int countdown = 10;
05.
06. @ override
07. // define the task in the run Method
08. Public void run (){
09. While (countdown --> 0 ){
10. system. Out. println ("#" + this. getname () + "(" + countdown + ")");
11 .}
12 .}
13.
14. Public static void main (string [] ARGs ){
15. New threaddemo2 (). Start ();
16. New threaddemo2 (). Start ();
17. // because the start method Returns quickly, the main thread can perform other operations. At this time, two independent threads are running concurrently.
18. system. Out. println ("countdown before rocket launch :");
19 .}
20 .}
21.
Package demo. thread;

Public class threaddemo2 extends thread {
Private int countdown = 10;

@ Override
// Define the task in the run Method
Public void run (){
While (countdown --> 0 ){
System. Out. println ("#" + this. getname () + "(" + countdown + ")");
}
}

Public static void main (string [] ARGs ){
New threaddemo2 (). Start ();
New threaddemo2 (). Start ();
// Because the start method Returns quickly, the main thread can execute other operations. At this time, two independent threads are running concurrently.
System. Out. println ("countdown before rocket launch :");
}
}

Running result:

# Thread-0 (9)
# 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-0 (0)
Countdown before rocket launch:
# Thread-1 (9)
# Thread-1 (8)
# Thread-1 (7)
# Thread-1 (6)
# Thread-1 (5)
# Thread-1 (4)
# Thread-1 (3)
# Thread-1 (2)
# Thread-1 (1)
# Thread-1 (0)
3. Comparison of the two methods

First, analyze the output results of the two methods, and create two threads. Why are the results different?

Using the runnable interface to create a thread can share the same target object (treaddemo1 TT = new treaddemo1 ();), so that multiple identical threads can process the same resource.

Then let's look at the explanation from JDK:

Runnable interfaces should be implemented by classes that intend to execute their instances through a thread. Class must define a non-parameter method called run.

This interface is designed to provide a public protocol for objects that want to execute code during activity. For example, the thread class implements runnable. Activation means that a thread has been started and has not been stopped.

In addition, runnable provides an activation method for non-thread subclass classes. By instantiating a thread instance and taking itself as the running target, you can run the runnable class without creating a subclass of the thread. In most cases, if you only want to override the run () method, instead of other thread methods, you should use the runnable interface. This is important because you should not create a subclass for the class unless the programmer intends to modify or enhance the basic behavior of the class.

Inherit the Thread class:
(1) Advantages: simple writing. If you need to access the current thread, you can obtain the current thread without using the thread. currentthread () method and directly using this.
(2) Disadvantage: Because the Thread class has inherited the Thread class, it cannot inherit other parent classes.
Runnable interface implementation:
(1) advantage: the Thread class only implements the runable interface and can inherit other classes. In this way, multiple threads can share the same target object, so it is very suitable for multiple identical threads to process the same resource, so that the CPU code and data can be separated, the formation of a clear model better reflects the idea of object-oriented.
(2) Disadvantages: programming is a little complicated. To access the current thread, you must use the thread. currentthread () method.

From: http://software.intel.com/zh-cn/blogs/2011/11/16/java-12? Cid = Sw: prccsdn2063

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.