First, Runnable
Runnable as an interface that contains a run method
1 Public classMyrunnableImplementsrunnable{2@Override//indication: Indicates an overriding method3 Public voidRun () {//implementing the interface Run method4SYSTEM.OUT.PRINTLN ("Thread 0.0\t to implement runnable" +Thread.CurrentThread (). GetName ());5 6 }7 8 Public Static voidMain (string[] args) {9 //Create a Runnable objectTenMyrunnable Run =Newmyrunnable (); One //Create a thread object with the object run as a parameter AThread THR1 =NewThread (Run, "thread-1"); -Thread THR2 =NewThread (Run, "Thread-2"); -Thread THR3 =NewThread (Run, "Thread-3"); theThread THR4 =NewThread (Run, "Thread-4"); -Thread THR5 =NewThread (Run, "Thread-5"); - //Start Thread - Thr1.start (); + Thr2.start (); - Thr3.start (); + Thr4.start (); A Thr5.start (); at } - - -}
Run the result as
Second, Thread
Thread is a class that implements the Runnable
1 Public classThreadsextendsthread{2 //Construction Method3 String name;4 Threads (String name) {5 This. Name =name;6 }7 //overriding the Run method8 Public voidrun () {9SYSTEM.OUT.PRINTLN ("Thread 0.0\t to implement runnable" +Thread.CurrentThread (). GetName ());Ten } One A Public Static voidMain (string[] args) { - //Create a Treads object -Threads THR1 =NewThreads ("Thread-1"); theThreads THR2 =NewThreads ("Thread-2"); -Threads THR3 =NewThreads ("Thread-3"); -Threads THR4 =NewThreads ("Thread-4"); -Threads THR5 =NewThreads ("Thread-5"); + //Start Thread - Thr1.start (); + Thr2.start (); A Thr3.start (); at Thr4.start (); - Thr5.start (); - - } - -}
Run results
Java Multithreading thread and runnable