Java thread series-differences between runnable and thread

Source: Internet
Author: User

Http://blog.csdn.net/wwww1988600/article/details/7309070

 

 

In Java, You can implement multithreading in two ways. 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. A class can implement multi-threaded operations as long as it inherits the Thread class and overwrites the run () method in this class,

However, a class can only inherit one parent class, which is a limitation of this method,

The following is an example:

Package org. thread. Demo;

Class mythread extends thread

{

Private string name;

Public mythread (string name)

{

Super ();

This. Name = Name;

}

Public void run ()

{

For (INT I = 0; I <10; I ++)

{

System. Out. println ("thread start:" + this. Name + ", I =" + I );

}

}

}

Package org. thread. Demo;

Public class threaddemo01 {

Public static void main (string [] ARGs ){

Mythread MT1 = new mythread ("thread ");

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

Mt1.run ();

Mt2.run ();

}

}

However, the results are regular at this time. The first object is executed first, and then the second object is executed without running each other.

In the JDK documentation, we can find that once the START () method is called, The run () method is found through JVM.

Start the thread by starting start:

Package org. thread. Demo;

Public class threaddemo01

{

Public static void main (string [] ARGs)

{

Mythread MT1 = new mythread ("thread ");

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

Mt1.start ();

Mt2.start ();

}

};

In this way, the program can run interactively.

So why do we have to use the START (); Method to start multithreading?

In the installation environment of JDK, src.zip is the full Java source program. By finding the definition of the START () method in thread, we can find that private native void start0 () is used in this method ();

Among them, the native keyword indicates that the underlying functions of the operating system can be called. This technology becomes a Java Native interface. In actual development, the runnable interface rarely uses the Thread class for multi-thread operations, it is done through the runnable interface.

Public interface runnable {

Public void run ();} example:

 

 

 

 

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 the multi-thread implemented by runnable. (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 ("A =" "mythread = ""

MT2 = "new" mythread = "" B = "" New = "" thread = "" MT1 = "" Start = "" New = "" thread = "" MT2 =" "Start =" "runnable =" "runnable =" "thread =" "-=" "> avoid the limitations of point inheritance, A class can inherit multiple interfaces. -> Suitable for resource sharing. The ticket selling program is used as an example and completed through the Thread class:

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.

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.