| Author |
Threads - Runnable vs. Extending Thread
|
Zack Green
Greenhorn
Joined: Mar 02, 2002
Posts: 2
|
|
I am a little confused on the way the Runnable interface is working. If I had some code: I don't understand why I have to use t.sleep() within the run() method, but only sleep() within the synchronized method. Could anyone explain why it is so?
|
 |
Rajinder Yadav
Ranch Hand
Joined: Jan 18, 2002
Posts: 178
|
|
When you implement a Runnable interface you are not subclass from the Thread class so you don't get the sleep() method for free, that's why you need to make a call to Thread.sleep(); which is a public static method! Take a look an the sample code below, look at how the Runner and Worker classes are declared, note how I am making a call to sleep inside the run method of each class. [ March 03, 2002: Message edited by: Rajinder Yadav ]
|
<a href="http://www.rajindery.com" target="_blank" rel="nofollow">Rajinder Yadav</a><p>Each problem that I solved became a rule which served afterwards to solve other problems. --Rene Descartes
|
 |
Rajinder Yadav
Ranch Hand
Joined: Jan 18, 2002
Posts: 178
|
|
Valentin, isn't this when you come in and enforce the naming policy for our new member
|
 |
ersin eser
Ranch Hand
Joined: Feb 22, 2001
Posts: 1072
|
|
Didn't you do it ? I know that we can enforce the policy but also Any member can remind the policy. So zmg501 please read our naming policy and re register. Thanks www.javaranch.com/name.jsp
|
 |
Zack Green
Greenhorn
Joined: Mar 02, 2002
Posts: 2
|
|
Thanks for the replies, that clears up some stuff for me, but I still have a question. Where are the wait() and notify() methods located? I'm just wondering why, when I implement Runnable, I use: I'm wondering why I don't have to use the statement t.wait() ??? This code is from within a synchronized method. Thanks.
|
 |
Marilyn de Queiroz
Sheriff
Joined: Jul 22, 2000
Posts: 9033
|
|
|
Look in class Object for wait() and notify()
|
JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
|
 |
Rajinder Yadav
Ranch Hand
Joined: Jan 18, 2002
Posts: 178
|
|
Zack, I made a mistake in my previous code post. I used 'Thread.sleep()' in the Worker class, but I've changed it to 'sleep()', so take a look at the code sample again please The idea was to show that in class Runner you need to use the static method 'Thread.sleep(), while in the Worker class you can simply use the class's 'sleep()' method. The code shows the difference between subclassing and implementing an interface. To follow up on your latest reply, there are a few concepts in work here that you should clear up! 1. Know what subclassing does for you 2. Understand the difference between subclassing and implementing an interface in Java 3. Know what a class static method is and how they are used, in other words, no instance reference is required! 4. Look at class Object, it defines the methods wait() and notify()....all java class are subclass of class Object (that's a given) The following code will output a value of 'true', this proves that class A 'is-a' a subclass of class Object. class A {} A a = new A(); System.out.println(a instanceof Object); (*) javac converts the definition 'class A' to 'class A extends Object' on your behalf This reply is somewhat terse, and I'm sure you already know the first 3 things I listed, but it's gives you some things to think about  [ March 03, 2002: Message edited by: Rajinder Yadav ]
|
 |
 |
|
|
subject: Threads - Runnable vs. Extending Thread
|
|
|