This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
Hi friends, Please see the code given below, itsfrom Marcus Test one. public class Testing extends Thread{ private String sThreadName; public static void main(String argv[]){ Testing h = new Testing(); h.go(); } Testing(){} Testing(String s){ sThreadName = s; } public String getThreadName(){ return sThreadName; } public void go(){ Testing first = new Testing("first"); first.start(); Testing second = new Testing("second"); second.start(); } public void start(){ for(int i = 0; i < 8; i ++){ System.out.println(getThreadName() +i); try{ Thread.sleep(100); } catch(InterruptedException e){System.out.println(e.getMessage());} } } } 1) Compile time error 2) Output of first0, second0, first0, second1 3) Output of first0, first1, second0, second1 4) Runtime error I understand that no run method makes this code to print ans. 3 but my doubt is once we say first.start(); this means thread should look for run method which should override run's method in Thread class, so in the absense of run method, shouldn't it give compiler error or run time error rather than print output of for loop once for String "first" and then for "second". Please clarify. Jyotsna
Dave Vick
Ranch Hand
Joined: May 10, 2001
Posts: 3244
posted
0
Jyotsna The Thread class implements Runnable so it has to provide an implementation for the run() method in Runnable. All it does is create an empty run() method that the subclass inherits. So it does nothing then returns so there would be no errors. If you implemented Runnable, on the other hand, then you'd have to supply a run() or you'd get a compiler error for not providing an implementation for it. Dave
Dave
Jyotsna Umesh
Ranch Hand
Joined: May 09, 2001
Posts: 94
posted
0
Thanks Dave for explanation. Its clear now. Jyotsna
April.Johnson
Ranch Hand
Joined: May 02, 2001
Posts: 48
posted
0
Dave is correct in his post that the default run() method in the Thread class does nothing and that if you want a class that extends Thread to run, you'll have to override the run() method. However, looking at the code posted, I notice that Thread's start() method is overridden in Testing. In that case, the thread will never really run or start. Overriding start() the way it's written basically means that the threads first and second never run. The method start() is essentially just another method. If you still want threads to run, you'll have to explicitely call super.start() in Testing.start() to experience the joins of live threads. Try this: public class Testing extends Thread{ private String sThreadName; public static void main(String argv[]){ Testing h = new Testing(); h.go(); } Testing(){} Testing(String s){ sThreadName = s; } public String getThreadName(){ return sThreadName; } public void go(){ Testing first = new Testing("first"); first.start(); Testing second = new Testing("second"); second.start(); } public void start(){ super.start(); // calling Thread.start() to get Thread functionality for(int i = 0; i < 8; i ++){ System.out.println(getThreadName() +i); try{ Thread.sleep(100); } catch(InterruptedException e){ System.out.println(e.getMessage()); } } } // put this in just to show when the thread is running and // when it's not. Removing this method (while keeping the // call to super.start()) will do what Dave was talking about. public void run(){ System.out.println("in run method of " + getThreadName()); } } Did I miss something? April [This message has been edited by April.Johnson (edited June 27, 2001).]