|
Extend Thread
This example shows creating a simple thread by extending Thread class.
|
package com.bethecoder.tutorials.thread.simple;
public class ExtendThread extends Thread {
public void run() {
for (int i = 0 ; i < 6; i ++ ) {
System.out.println("ExtendThread : " + i);
try {
Thread.sleep(10L);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
|
| |
|
package com.bethecoder.tutorials.thread.simple;
public class ExtendThreadTest {
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,
ExtendThread : 0
Main : 0
Main : 1
ExtendThread : 1
Main : 2
ExtendThread : 2
Main : 3
ExtendThread : 3
ExtendThread : 4
Main : 4
Main : 5
ExtendThread : 5
|
|