Package com.thread.runnable;
/**
* There are two ways to implement multithreading, such as inheriting the thread class and implementing the Runnable interface.
* Which way is better? The way that is implemented because of inheritance.
* Reason: 1. Avoid Java but inheritance limitations
* 2. If multiple threads are working on the same resource, the way to implement the interface is more appropriate
* @author Administrator
*
*/
public class Testrunnable {
public static void Main (string[] args) {
Oddtest odd = new Oddtest ();
Eventtest event = new Eventtest ();
New Thread (Odd, "odd"). Start ();
New Thread (event, "even"). Start ();
Testticket ticket = new Testticket ();
New Thread (Ticket, "window One"). Start ();
New Thread (Ticket, "window two"). Start ();
New Thread (Ticket, "window three"). Start ();
}
}
Class Testticket implements runnable{
int ticket = 100;
@Override
public void Run () {
while (true) {
if (Ticket > 0) {
System.out.println (Thread.CurrentThread (). GetName () + "ticketing:" +ticket-);
}else{
Break
}
}
}
}
Class Oddtest implements runnable{
@Override
public void Run () {
for (int i = 1; i <=; i++) {
if (i% 2 = = 0) {
System.out.println (Thread.CurrentThread (). GetName () + ":" +i);
}
}
}
}
Class Eventtest implements runnable{
@Override
public void Run () {
for (int i = 1; i <=; i++) {
if (i% 2! = 0) {
System.out.println (Thread.CurrentThread (). GetName () + ":" +i);
}
}
}
}
The way to implement multithreading runnable