This example show how to create threads using two ways,
  • extending Thread to a subclass
  • using interface Runnable.

The Runnable interface is more robust, however must implement  a method run(). as show in the picture shown below.

Once implemented, the error from eclipse compiler disappears.


import java.io.*;
public class test {
public static int n1 = 1;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("-->Welcome to thread building<--");
System.out.print("type name as worker : ");
String str = br.readLine();
String s = str + " : "+ n1++;
thread_this pt = new thread_this();
pt.process_this(s);
s = str + " : "+ n1++;
thread_that t_that = new thread_that();
t_that.process_that(s);
}
}
class thread_this extends Thread
{
public void process_this(String str)
{
Thread th = new thread_this() ;
th.setName(str);
th.start();
System.out.println("Hello from " + th.getName());
}
public void run() {
System.out.println("Hello from thread subclass!");
}
}
class thread_that implements Runnable
{
public void process_that(String str)
{
Thread th = new Thread(new thread_that()) ;
th.setName(str);
th.start();
System.out.println("Hello from " +th.getName());
}
public void run() {
System.out.println("Hello from thread Runnable Interface!" );
}
}