Java concurrent Programming-thread Creation (ii)

Source: Internet
Author: User

for thread creation and more detailed information, refer to the blog " java-- threads, the following is a refinement of thread creation and simple re-implementation.

In Java if you want to create a thread, there are generally two ways: 1) inherit the thread class; 2) Implement the Runnable interface.

Way One: Inherit the thread class 


MyThread :

Package Com.tgb.hjy;public class MyThread extends Thread{private string name;public MyThread (string name) {This.name= Name;} @Overridepublic void Run () {System.out.println ("Name:" +name+ ", Child thread ID:" +thread.currentthread (). GetId ());}}

Clientthread :

Package Com.tgb.hjy;public class Clientthread{public static void Main (string[] args) {System.out.println ("Main thread ID:" + Thread.CurrentThread (). GetId ()); MyThread thread1=new MyThread ("Thread1"); Thread1.start ();}}

mode Two: Implement Runnable interface  


Myrunnable:

Package Com.tgb.hjy;public class Myrunnable implements Runnable{public Myrunnable () {} @Overridepublic void run () { System.out.println ("Main thread ID:" +thread.currentthread (). GetId ());}}

Clientrunnable:

Package Com.tgb.hjy;public class Clientrunnable {public static void main (string[] args) {System.out.println ("Child thread ID:" + Thread.CurrentThread (). GetId ()); Myrunnable myrunnable=new myrunnable (); Thread thread=new thread (myrunnable); Thread.Start ();}}

Note that this method must use runnable as the parameter of the thread class, and then create a new thread to execute the subtask by using the thread's Start method. If you call Runnable's Run method directly, you will not create a new thread, and this common method call is no different.

In fact, looking at the implementation source code of the thread class will find that the thread class implements the Runnable interface.

We've been saying that if you call the Run method, which is equivalent to executing the Run method in the main thread, there is no difference from a normal method call, and a new thread is not created to perform the defined task.

Let's take a look at the following:

public static void Main (string[] args) {System.out.println ("Main thread ID:" +thread.currentthread (). GetId ()); MyThread thread1=new MyThread ("Thread1"); Thread1.start (); MyThread thread2=new MyThread ("Thread2"); Thread2.run ();}


Call Thread2.start (); The method outputs the result:




The following conclusions can be drawn from the output:

1) The thread IDs of Thread1 and Thread2 are different, and the thread2 and the main thread IDs are the same, stating that the Run method call does not create a new thread, but rather runs the run method directly in the main thread, without any difference from the normal method invocation;

2) Although the Thread1 start method call is called before the Thread2 Run method, the first output is information about the Thread2 Run method call, stating that the process created by the new thread does not block subsequent executions of the main thread.

Summary:

In Java, a class supports only single inheritance, meaning that when a new class is defined, It can only extend an external class. This way, if the custom thread class is created by extending the thread class, then the custom class cannot extend other classes, nor can it implement more complex functions. Therefore, if your custom class must extend other classes, you can define the class as a threading class by using a method that implements the Runnable interface, which avoids the limitations of Java single inheritance.

It is also important that threads created with the implementation of the Runnable interface can handle the same resource, thus sharing resources. -- For details, see the blog java-- thread "



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java concurrent Programming-thread Creation (ii)

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.