Java multithreaded--thread class and Runable interface

Source: Internet
Author: User

In Java, there are two ways to implement multi-threading, one is to inherit the thread class, and the other is to implement the Runnable interface;
The thread class is defined in the Java.lang package. As long as a class inherits the thread class, it also overwrite the
The run () method enables multithreaded operations, but a class can inherit only one parent class, which is the limit of this method.

Let's look at the example below:

[Java] view plaincopy

  1. Package Org.thread.demo;

  2. class MyThread extends thread{

  3. Private String name;

  4. Public MyThread (String name) {

  5. Super ();

  6. this. Name = name;

  7. }

  8. Public void Run () {

  9. for (int i=0;i<; i++) {

  10. System.out.println ("thread start:"+this. name+", i="+i);

  11. }

  12. }

  13. }

  14. Package Org.thread.demo;

  15. Public class ThreadDemo01 {

  16. Public Static void Main (string[] args) {

  17. MyThread mt1=New MyThread ("Thread A");

  18. MyThread mt2=New MyThread ("thread B");

  19. Mt1.run ();

  20. Mt2.run ();

  21. }

  22. }


However, the result is very regular, the first object executes, and then the second object executes, and it does not run against one another. As you can see in the JDK documentation, once the start () method is called, the Run () method is found through the JVM. Starting the Start () method starts the thread:

[Java] view plaincopy

    1. Package Org.thread.demo;

    2. Public class ThreadDemo01 {

    3. Public Static void Main (string[] args) {

    4. MyThread mt1=New MyThread ("Thread A");

    5. MyThread mt2=New MyThread ("thread B");

    6. Mt1.start ();

    7. Mt2.start ();

    8. }

This allows the program to complete the interactive operation normally. So why not use Start ();
Under the JDK installation path, Src.zip is the entire Java source program, and this code finds the definition of the start () method in thread, and you can see that the private native void Start0 () is used in this method; Where the Native keyword means that the underlying function of the operating system can be called, then such a technique is called JNI technology (Java Native Interface)

· Runnable interface
In real-world development, a multithreaded operation rarely uses the thread class, but is accomplished through the Runnable interface.

[Java] view plaincopy

    1. package  org.runnable.demo;  

    2. class   MYTHREAD&NBSP implements  runnable{  

    3. private   string name;  

    4. public  mythread (string name)  {   

    5. this . name = name;  

    6. }  

    7. public  , void  run () {  

    8. for ( int  i= 0 ; i< + ; i++) {  

    9. System.out.println ( thread Start: + this . name+ ", i=" +i);   

    10. }  

    11. }  

    12. }  


However, there is no start () method in a subclass defined with runnable, only in the thread class. There is a constructor method for observing the thread class at this point: public Thread (Runnable Targer)
This construction method takes a subclass instance of runnable, which means that the thread class can be used to start multithreading for runnable implementations. (Start () to coordinate the resources of the system):

[Java] view plaincopy

    1. package  org.runnable.demo;  

    2. import   org.runnable.demo.mythread;  

    3. public   class   threaddemo01 {  

    4. public   static   void  main (String[] args)  {  

    5. mythread mt1= New  mythread ( "Thread A" );   

    6. mythread mt2= new   MyThread ( "thread B" );   

    7. new  thread (MT1). Start ();   

    8. new  thread (MT2). Start ();   

    9. }  

    10. }  


The difference between the two ways of implementation and contact:
In the program development, as long as the multithreading is always to achieve runnable interface-based, because the implementation of runnable interface compared to
Inheriting the thread class has the following benefits:
To avoid the limitations of point inheritance, a class can inherit multiple interfaces.
--Suitable for resource sharing

Take the ticket selling procedure as an example, complete by the thread class:

[Java] view plaincopy

  1. //Use thread to implement threads that cannot implement resource sharing

  2. class MyThread extends Thread

  3. {

  4. Private int ticket=ten;

  5. Private String name;

  6. Public MyThread (String name) {

  7. this. name=name;

  8. }

  9. Public void Run () {

  10. for (int i=0;i<; i++) {

  11. if (ticket>0) {

  12. System.out.println ("thread"+name+"Sell ticket"+ (ticket--));

  13. }

  14. }

  15. }

  16. }

  17. Public class Threadticket

  18. {

  19. Public Static void Main (String args[]) {

  20. MyThread a = new MyThread ("a");

  21. MyThread B = new MyThread ("B");

  22. A.start ();

  23. B.start ();

  24. }

  25. }

  26. //Use runnable to implement threads for resource sharing

  27. class MyThread implements Runnable

  28. {

  29. Private int ticket=ten;

  30. Private String name;

  31. Public MyThread (String name) {

  32. this. name=name;

  33. }

  34. Public void Run () {

  35. for (int i=1; i<=; i++) {

  36. if (ticket>0) {

  37. System.out.println ("thread"+name+"Sell ticket"+ (ticket--));

  38. }

  39. }

  40. }

  41. }

  42. Public class Threadticket

  43. {

  44. Public Static void Main (String args[]) {

  45. MyThread a = new MyThread ("a"); //Instantiate a thread to perform the task

  46. Thread Ta = new thread (A); //Instance two thread object, actually passing a task

  47. Thread Tb = new thread (A); //Because two threads are performing a task, resources are shared

  48. Ta.start ();

  49. Tb.start ();

  50. }

  51. }


Although the program now has two threads, but sold a total of 10 tickets, that is, the use of runnable implementation of multi-threading can achieve the purpose of resource sharing.

runnable The connection between the interface and the thread: Public class Thread extends Object implements Runnable
It is easy to see that the thread class is a subclass of the Runnable interface.



Java multithreaded--thread class and Runable interface

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.