This is a simple program for implementing runnable. i'm getting an
import java.util.*;
class NewThread implements Runnable{
Thread t;
NewThread(){
t = new Thread(this, "Demo Thread");
System.out.println("Child Thread "+t);
t.start();
}
}
public void run(){
try{
for(int i=5;i>0;i--){
System.out.println("Child Thread:"+i);
Thread.sleep(1000);
}
} catch(InterruptedException e){
System.out.println("Child Interrupted");
}
System.out.println("Child Thread Exiting\n");
}
public class ThreadDemo{
public static void main(String[] args) {
thread curr = thread.currentThread();
System.out.println("Current Thread"+curr);
new NewThread();
try{
for(int i=0;i>5;i--){
System.out.println("Parent Thread"+i);
Thread.sleep(1000);
}
} catch(InterruptedException e){
System.out.println("Main thread interrupted");
}
System.out.println("Main Thread Exiting");
}
}
i get these errors while compiling
ThreadDemo.java:14: class, interface, or enum expected
public void run(){ ^
ThreadDemo.java:16: class, interface, or enum expected
for(int i=5;i>0;i--){ ^
ThreadDemo.java:16: class, interface, or enum expected for(int i=5;i>0;i--){ ^
ThreadDemo.java:18: class, interface, or enum expected
Thread.sleep(1000); ^
ThreadDemo.java:19: class, interface, or enum expected } ^
ThreadDemo.java:22: class, interface, or enum expected } ^
ThreadDemo.java:25: class, interface, or enum expected
}