|
Implement Runnable
This example shows creating a simple thread by implementing Runnable interface.
|
package com.bethecoder.tutorials.thread.simple;
public class ImplementRunnable implements Runnable {
public void run() {
for (int i = 0 ; i < 6; i ++ ) {
System.out.println("ImplementRunnable : " + i);
try {
Thread.sleep(10L);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
|
| |
|
package com.bethecoder.tutorials.thread.simple;
public class ImplementRunnableTest {
public static void main (String [] args) throws InterruptedException {
ExtendThread thread = new ExtendThread();
thread.start();
for (int i = 0 ; i < 6 ; i ++) {
System.out.println("Main : " + i);
Thread.sleep(10L);
}
}
}
|
| |
It gives the following output,
Main : 0
ExtendThread : 0
Main : 1
ExtendThread : 1
Main : 2
ExtendThread : 2
Main : 3
ExtendThread : 3
Main : 4
ExtendThread : 4
ExtendThread : 5
Main : 5
|
|