Thread implementation methods and differences extends thread, implements Runable

Source: Internet
Author: User

The/** thread exists in the process, and the process is created by the system. There are two ways to create a new thread of execution: The line reanalysis writes the Run method, and then calls with the start () method, which is actually called the Run () method, but if the run () method is started directly, then the execution of the main function occurs after the thread call ends.  Another situation is to use start () to say that multiple threads are looting the executive right, to be clear: a moment, there can only be a program in the implementation, except multicore. Also note that thread priority is represented by 0-5 for 1, real-time: real-time2, High: Tall3, Above Normal: higher than the standard4. Normal: Standard5, Below Normal: Below the standard6, Idle: free. The first is a subclass that declares a class as thread. The subclass overrides the Run method of the thread class and can then assign and start an instance of the subclass. The second kind: declares the interface class that implements Runnable. Implement the Run () method in the class, and then assign the class instance to pass and start as a parameter when the thread is created. Runnable provides a way to activate a class that is not a thread subclass.Because the interface class of the thread that the interface implements is not a thread, it is used to create the class and then create the thread and pass the class object into the created thread class.The difference between the two ways:The second implementation of the way, that is, to implement the interface, to avoid but the limitations of inheritance, generally with this.There is also a different location for thread code storage. Interface method: The code is placed in the run () method of the interface subclass, and the inheritance method: The code is placed in the run () method of the thread subclass. **/class Demo extends Thread//implements runnable{public void Run (){for (int x=0;x<60;x++)System.out.println (Thread.CurrentThread () + "sub-thread Run");}}class threadtest1{public static void Main (string[] args){How to inherit: extendsDemo demo= new demo ();Demo.start ();for (int x=0;x<60;x++)System.out.println ("Main thread Run");/** Implementation Interface mode: ImplementsDemo Demo = new Demo ();Thread TD1 = new Thread (demo);Thread TD2 = new Thread (demo);Thread td3 = new Thread (demo);Thread td4 = new Thread (demo);Td1.start ();Td2.start ();Td3.start ();Td4.start ();The advantage is that threads are created separately, without causing similar problems in extends, which means that "tickets" are not duplicated, and the benefits are stated in the notes above**/}}

Thread implementation methods and differences extends thread, implements Runable

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.