Differences between runnable and thread in Java (2)

Source: Internet
Author: User

However, the start () method is not available in the subclass defined by runnable, and is available only in the Thread class. Observe the Thread class. There is a constructor: Public thread (runnable targer)

This constructor accepts runnable subclass instances, that is, you can use the Thread class to start multiple runnable implementations.

Thread. (START () can coordinate system resources ):

Package org. runnable. Demo;

Import org. runnable. Demo. mythread;

Public class threaddemo01 {

Public static void main (string [] ARGs ){

Mythread MT1 = new mythread ("thread ");

Mythread MT2 = new mythread ("thread B ");

New thread (MT1). Start ();

New thread (MT2). Start ();

}

}

· Differences and links between the two implementation methods:

In program development, as long as it is multi-thread, it will always be dominated by the implementation of the runnable interface, because the implementation of the runnable interface is

Inheriting the Thread class has the following benefits:

-> Avoid the limitations of point inheritance. A class can inherit multiple interfaces.

-> Suitable for Resource Sharing

Taking the ticket selling program as an example, the thread class is used to complete:

Package org. Demo. DFF;

Class mythread extends thread {

Private int ticket = 10;

Public void run (){

For (INT I = 0; I <20; I ++ ){

If (this. Ticket> 0 ){

System. Out. println ("ticket selling: Ticket" + this. Ticket --);

}

}

}

};

The following uses three thread objects to sell tickets at the same time:

Package org. Demo. DFF;

Public class threadticket {

Public static void main (string [] ARGs ){

Mythread MT1 = new mythread ();

Mythread MT2 = new mythread ();

Mythread mt3 = new mythread ();

Mt1.start (); // each thread sells 10 tickets, with a total of 30 tickets.

Mt2.start (); // but there are only 10 tickets, each thread sells its own tickets

Mt3.start (); // resource sharing failed

}

}

If runnable is used, resources can be shared. The following is an example:

Package org. Demo. runnable;

Class mythread implements runnable {

Private int ticket = 10;

Public void run (){

For (INT I = 0; I <20; I ++ ){

If (this. Ticket> 0 ){

System. Out. println ("ticket selling: Ticket" + this. Ticket --);

}

}

}

}

Package org. Demo. runnable;

Public class runnableticket {

Public static void main (string [] ARGs ){

Mythread Mt = new mythread ();

New thread (MT). Start (); // same Mt, but not in thread.

New thread (MT). Start (); // an exception occurs when the object MT is instantiated.

New thread (MT). Start ();

}

};

Although there are currently three threads in the program, a total of 10 tickets are sold. That is to say, using runnable to implement multithreading can achieve resource sharing.

The connection between the runnable interface and thread:

Public class thread extends object implements runnable

The thread class is also a subclass of the runnable interface.

Reference from: http://java.chinaitlab.com/line/820728_2.html

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.