How to implement the Runnable method creating threads Why pass the subclass object of the Runnable interface to the constructor of the thread

Source: Internet
Author: User
Tags true true ticket

/**
* @author Zhao
* @TIME 0419 22:56 End
* Second method for defining threads: Implementing the Runnable Interface
* Step: 1, define a subclass implement Runnable interface
* 2, overwrite the Run () method in the subclass, and write the code executed by the multithreaded lock into the Run method
* 3, the thread object is established through the thread class;
* 4, the subclass object of the Runnable interface is passed as the actual parameter to the constructor of the thread class.
Why pass the subclass object of the Runnable interface to the constructor of thread.
Because, the object that the custom run method belongs to is the subclass object of the Runnable interface.
So let the thread specify the run method of the specified object. You must specify the object to which the Run method belongs.
Epiphany (combined with the introductory question we just saw today:
1, in the test project java.Iduction.test, String s = "Hello world"; string string =s;
Note: S is not an object, but a string-type reference that points to an object
Similarly: Why pass a subclass object of the Runnable interface to the constructor of thread.

*/
Example: Defining a ticket-selling procedure
Package threaddemoone;
Class Ticket implements Runnable//Implement Interface, note English words
{
private int tick =100;//defines 100 votes
public void Run ()
{
while (true)//true true true to write missed two times. while (True) is the execution loop.
{
if (tick>0)
{
System.out.println ("Conductor number" +thread.currentthread (). GetName () + "This is the first" +tick+ "Ticket");
tick--;
}
}
}
}
public class ThreadDemo4
{
public static void Main (string[] args)
{
Ticket t=new Ticket ();//Declares a "reference" to an object of type Ticket, which points to the Ticket object
Thread t1=new thread (t);//Create a thread that declares a T1 variable of type thread, and the T1 variable points to the variable to which T is pointing.
Thread t2=new thread (t);
Thread t3=new thread (t);
Thread t4=new thread (t);
/*new the T in Thread (t) is not an object, but a variable declared as a ticket type, T points to the object
* Instead of pointing t1~t4 to t,t and pointing to the object in the Run method, thread t1~t4 points to the ticket object
* If you do not point to an object, the thread will not know which run method to execute.
*/
T1.run (); Make a mistake, note that it cannot be the run method, it should be the Start method
T1.start ();
T2.start ();
T3.start ();
T4.start ();



}
}

How to implement the Runnable method creating threads Why pass the subclass object of the Runnable interface to the constructor of the thread

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.