Java multithreading inherits the difference between thread and implementation runnable

Source: Internet
Author: User
Tags ticket

1) Inherit thread:

public class ThreadTest extends Thread
{
private int count;
private String name;

Public threadtest (int count,string name) {
This.count = count;
THIS.name = name;
}

public void Run ()
{
while (count>0)
{
System.out.println (name+ "buy tickets" +count--);
}
}

public static void Main (String []args)
{
threadtest T1 = new ThreadTest (10, "Window 1th");
ThreadTest t2 = new ThreadTest (10, "window 2nd");
T1.start ();
T2.start ();
}
}

Operation Result:

Window 1th, buy tickets 10
Window 1th, buy tickets 9
Window 1th, buy tickets 8
Window 2nd, Buy Tickets 10
Window 1th, buy tickets 7
Window 2nd, Buy tickets 9
Window 1th, buy tickets 6
Window 2nd, Buy tickets 8
Window 1th, Buy tickets 5
Window 2nd, Buy tickets 7
Window 1th, buy tickets 4
Window 2nd, Buy tickets 6
Window 1th, buy tickets 3
Window 2nd, Buy tickets 5
Window 1th, buy tickets 2
Window 2nd, Buy Tickets 4
Window 1th, Buy tickets 1
Window 2nd, Buy Tickets 3
Window 2nd, Buy Tickets 2
Window 2nd, Buy tickets 1

2) Implement Runnable interface

public class Hello implements runnable{

private int ticket=19;

@Override
public void Run () {
for (int i=0;i<20;i++) {
if (This.ticket >0) {
System.out.println (Thread.CurrentThread (). GetName () + "buying tickets" +this.ticket--);
}
}
}

public static void Main (string[] args) {
Hello h1 = new Hello ();
New Thread (H1, Window No. 3rd). Start ();
New Thread (H1, Window No. 1th). Start ();
New Thread (H1, Window No. 2nd). Start ();

}

}

Operation Result:

Window 1th is buying tickets 19
Window 2nd is buying tickets 18
Window 3rd is buying tickets 19
Window 2nd is buying tickets 16
Window 1th is buying tickets 17
Window 2nd is buying tickets 14
Window 3rd is buying tickets 15
Window 2nd is buying tickets 12
Window 1th is buying tickets 13
Window 2nd is buying tickets 10
Window 3rd is buying tickets 11
Window 2nd is buying tickets 8
Window 1th is buying tickets 9
Window 2nd is buying tickets 6
Window 3rd is buying tickets 7
Window 2nd is buying tickets 4
Window 1th is buying tickets 5
Window 2nd is buying tickets 2
Window 3rd is buying tickets 3
Window 1th is buying tickets 1

As can be seen from the results above: the pattern of inheriting the thread implementation is to define multiple threads, each of which completes its own task. The pattern of implementing runnable is to define multiple threads and implement 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.

Java multithreading inherits the difference between thread and implementation runnable

Related Article

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.