The difference between inheriting the thread class and implementing the Runnable interface in Java

Source: Internet
Author: User
Tags ticket

There are two ways to create thread threads in Java:

1. By inheriting the thread class, overriding the thread's run () method, placing the logic in which the threads run

2. Instantiate the thread class by implementing the Runnable interface

In the practical application, we often use multi-threading, such as the station ticketing system, the station's various ticket outlets equal to each thread. When we do this system may think of two ways to achieve, inherit the thread class or implement the Runnable interface, now look at the two ways to achieve the results.


Java code
  1. Package com.threadtest;
  2. Class MyThread extends thread{
  3. private int ticket = 10;
  4. private String name;
  5. Public MyThread (String name) {
  6. THIS.name =name;
  7. }
  8. public void Run () {
  9. for (int i =0;i<500;i++) {
  10. if (this.ticket>0) {
  11. SYSTEM.OUT.PRINTLN (this.name+ "sell ticket---->" + (this.ticket--));
  12. }
  13. }
  14. }
  15. }
  16. public class Threaddemo {
  17. public static void Main (string[] args) {
  18. MyThread mt1= New MyThread ("one Window");
  19. MyThread mt2= New MyThread ("Window No. second");
  20. MyThread mt3= New MyThread ("Window No. third");
  21. Mt1.start ();
  22. Mt2.start ();
  23. Mt3.start ();
  24. }
  25. }

The results of the operation are as follows:

Java code
  1. A window sells tickets---->10
  2. A window sells tickets---->9
  3. Window second sell ticket---->10
  4. A window sells tickets---->8
  5. A window sells tickets---->7
  6. A window sells tickets---->6
  7. Window third sell ticket---->10
  8. A window sells tickets---->5
  9. A window sells tickets---->4
  10. A window sells tickets---->3
  11. A window sells tickets---->2
  12. A window sells tickets---->1
  13. Window second sell ticket---->9
  14. Window second sell ticket---->8
  15. Window third sell ticket---->9
  16. Window third sell ticket---->8
  17. Window third sell ticket---->7
  18. Window third sell ticket---->6
  19. Window third sell ticket---->5
  20. Window third sell ticket---->4
  21. Window third sell ticket---->3
  22. Window third sell ticket---->2
  23. Window third sell ticket---->1
  24. Window second sell ticket---->7
  25. Window second sell ticket---->6
  26. Window second sell ticket---->5
  27. Window second sell ticket---->4
  28. Window second sell ticket---->3
  29. Window second sell ticket---->2
  30. Window second sell ticket---->1

The code for implementing the Runnable interface is as follows:

Java code
  1. Package com.threadtest;
  2. Class MyThread1 implements runnable{
  3. private int ticket = 10;
  4. private String name;
  5. public void Run () {
  6. for (int i =0;i<500;i++) {
  7. if (this.ticket>0) {
  8. System.out.println (Thread.CurrentThread (). GetName () + "sell ticket---->" + (this.ticket--));
  9. }
  10. }
  11. }
  12. }
  13. public class Runnabledemo {
  14. public static void Main (string[] args) {
  15. TODO auto-generated Method Stub
  16. Design of three threads
  17. MyThread1 MT = new MyThread1 ();
  18. thread T1 = new Thread (MT, "one Window");
  19. Thread t2 = new Thread (MT, "window number second");
  20. thread t3 = new Thread (MT, "window number third");
  21. MyThread1 mt2 = new MyThread1 ();
  22. MyThread1 mt3 = new MyThread1 ();
  23. T1.start ();
  24. T2.start ();
  25. T3.start ();
  26. }
  27. }

The results of the operation are as follows:

Java code
    1. A window sells tickets---->10
    2. Window third sell ticket---->9
    3. Window third sell ticket---->7
    4. Window third sell ticket---->5
    5. Window third sell ticket---->4
    6. Window third sell ticket---->3
    7. Window third sell ticket---->2
    8. Window third sell ticket---->1
    9. A window sells tickets---->8
    10. Window second sell ticket---->6

Why this kind of result is present. We might as well make a metaphor, actually just the procedure,

Inherit the thread class, we are equivalent to take out three things namely three tickets to sell 10 of the task divided to three windows, they each do the various things sold each of the tickets to complete each task, because Mythread inherits the thread class, so in new Mythread creates three threads while creating three objects;

Achieve runnable, equivalent to take out a ticket to sell 10 of the task to three people to work together, new mythread equivalent to create a task, and then instantiate three thread, create three threads to arrange three windows to execute.

The diagram shows the following:



We may be confused when we touch. Inherit the thread class and implement the Runnable interface to implement multi-threading, in fact, after contact we will find that this is completely two different implementations of multi-threading, one is multiple threads to complete their own tasks, one is multiple threads together to complete a task.

In fact, in the implementation of a task with multiple threads to do can also be used to inherit the thread class to achieve is just more trouble, generally we use the implementation of runnable interface to achieve, concise and clear.

In most cases, you should use the Runnable interface if you only want to override the run () method without overriding the other Thread methods. This is important because a subclass should not be created for the class (Thread) unless the programmer intends to modify or enhance the basic behavior of the class.

The difference between inheriting the thread class and implementing the Runnable interface in Java

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.