try the following program ...
public class ThreadTestFinal{
public static void main (
String[] args){
Runner r = new Runner(5);
Strider s = new Strider(5);
Thread t1 = new Thread(r, "Thread A");
Thread t2 = new Thread(r, "Thread B");
Thread s1 = new Thread(s, "Thread C");
Thread s2 = new Thread(s, "Thread D");
t1.start();
t2.start();
s1.start();
s2.start();
}
}
class Runner implements Runnable {
private int counter;
private final int MAX_LIMIT;
Runner(int limit) {
MAX_LIMIT = limit;
}
public void run() {
try {
for (int i=0; i<MAX_LIMIT; i++) {
System.out.println(Thread.currentThread().getName() + ": " + counter++);
Thread.sleep(1000);
}
} catch(InterruptedException e) {
e.printStackTrace();
}
}
}
class Strider extends Thread {
private int counter;
private final int MAX_LIMIT;
Strider(int limit) {
MAX_LIMIT = limit;
}
Strider(int limit, String name) {
MAX_LIMIT = limit;
setName(name);
}
public void run() {
try {
for (int i=0; i<MAX_LIMIT; i++) {
System.out.println(Thread.currentThread().getName() + ": " + counter++);
Thread.sleep(1000);
}
} catch(InterruptedException e) {
e.printStackTrace();
}
}
}
the thing is Thread class intern implements Runnable and thus it can also be treated like a Runnable (many threads CAN share the same object instance.. as you said. ).
pls correct me if i am wrong.