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 ();