Forum : error in threads
Brief description  about Online courses   join in Online courses
View Priti m naik 's Profile

error in threads

class RunnableThread implements Runnable {

Thread RunningThread;
public RunnableThread() {
}
public RunnableThread(String ThreadName) {
RunningThread = new Thread(this, ThreadName); // (1) Create a new thread.
System.out.println(RunningThread.getName());
RunningThread.start(); // (2) Start the thread.
}
public void run() {
//Display info about this particular thread
System.out.println(Thread.currentThread());
}
}

public class RunnableExample {
public static void main(String[] args) {
Thread thread1 = new Thread(new RunnableThread(), "thread1");
Thread thread2 = new Thread(new RunnableThread(), "thread2");
RunnableThread thread3 = new RunnableThread("thread3");
//Start the threads
thread1.start();
thread2.start();
try {
//delay for one second
Thread.currentThread().sleep(1000);
} catch (InterruptedException e) {
System.out.println("There has been an Exception, Take care of it!");
}
//Display info about the main thread
System.out.println(Thread.currentThread());
}
}


this is thre tutorial program itself its giving me 3 errors

class Runnable Example :5:cannt find symbol
symbol: constructor Thread(Runnable Thread,java.lang.String)...........


plz help



Asked by Priti m naik | Oct 15, 2010 |  Reply now
Replies (1)
View arun kumar das 's Profile
HI

This code is working fine on my PC.
You should make sure save the java file with RunnableExample.java because this public class and than run it. I think it should be work fine.

class RunnableThread implements Runnable {

Thread RunningThread;
public RunnableThread()
{

}
public RunnableThread(String ThreadName)
{
RunningThread = new Thread(this, ThreadName); // (1) Create a new thread.
System.out.println(RunningThread.getName());
RunningThread.start(); // (2) Start the thread.
}
public void run()
{
//Display info about this particular thread
System.out.println(Thread.currentThread());
}
}

public class RunnableExample
{
public static void main(String[] args)
{
Thread thread1 = new Thread(new RunnableThread(), "thread1");
Thread thread2 = new Thread(new RunnableThread(), "thread2");
RunnableThread thread3 = new RunnableThread("thread3");
//Start the threads
thread1.start();
thread2.start();
try
{
//delay for one second
Thread.currentThread().sleep(1000);
} catch (InterruptedException e) {
System.out.println("There has been an Exception, Take care of it!");
}
//Display info about the main thread
System.out.println(Thread.currentThread());
}
}

Regards
Java Teacher
Oct 28, 2010