Thread uses the Runnable interface to simulate four window ticket sales, threadrunnable

Source: Internet
Author: User

Thread uses the Runnable interface to simulate four window ticket sales, threadrunnable

// Copyrightliupengcheng
// Http://www.cnblogs.com/liupengcheng

/**
* Created by Administrator on 2014/10/23.
* The following example shows how to sell 100 tickets in four windows.
*/

// Another way to create a thread is to declare a class that implements the Runnable interface. This class then implements the run method. Then, you can allocate instances of this class and pass and start them as a parameter when creating a Thread.

// Copyrightliupengcheng
// Http://www.cnblogs.com/liupengcheng

Public class ThreadDemo5 {
Public static void main (String [] args ){
TestThread t = new TestThread ();
New Thread (t). start ();
New Thread (t). start ();
New Thread (t). start ();
New Thread (t). start ();
}
}

// Copyrightliupengcheng
// Http://www.cnblogs.com/liupengcheng


Class TestThread implements Runnable {// class that calls the Runnable interface
Private int ticket= 100;
Public void run ()
{
While (true)
{
If (ticket> 0)
System. out. println (Thread. currentThread (). getName () + "is sailing ticket" + ticket --);
}
}
}

// Copyrightliupengcheng
// Http://www.cnblogs.com/liupengcheng


Why do we need to implement the Runnable interface to implement multithreading? Can I use Thread as the parent class?

Of course you can. This is two ways to do the thread.
There are two ways to create a new execution thread. One way is to declare the class as a subclass of Thread. This subclass should override the run method of the Thread class. Next, you can allocate and start instances of this subclass. For example, a thread that calculates the prime number greater than a specified value can be written as follows:

--------------------------------------------------------------------------------

Class PrimeThread extends Thread {
Long minPrime;
PrimeThread (long minPrime ){
This. minPrime = minPrime;
}

Public void run (){
// Compute primes larger than minPrime
...
}
}

--------------------------------------------------------------------------------

Then, the following code creates and starts a thread:

PrimeThread p = new PrimeThread (1, 143 );
P. start ();
Another way to create a thread is to declare a class that implements the Runnable interface. This class then implements the run method. Then, you can allocate instances of this class and pass and start them as a parameter when creating a Thread. The same example using this style is as follows:

--------------------------------------------------------------------------------

Class PrimeRun implements Runnable {
Long minPrime;
PrimeRun (long minPrime ){
This. minPrime = minPrime;
}

Public void run (){
// Compute primes larger than minPrime
...
}
}

--------------------------------------------------------------------------------

Then, the following code creates and starts a thread:

PrimeRun p = new PrimeRun (143 );
New Thread (p). start ();
Each thread has a Identification name, and multiple threads can have the same name. If no identification name is specified during thread creation, a new name is generated .... Remaining full text>

Why can java directly declare a Thread object without inheriting the Thread or implementing the Runnable interface?

This is not in the "declare Thread object"
This is when the static method currentThread () of the Thread class is called. This method returns a Thread object.

/**
258 * Returns a reference to the currently executing thread object.
259 *
260 * @ return the currently executing thread.
261 */
262 public static native Thread currentThread ();

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.