Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Threads - Runnable vs. Extending Thread

 
Zack Green
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Rajinder Yadav
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Valentin, isn't this when you come in and enforce the naming policy for our new member
 
ersin eser
Ranch Hand
Posts: 1072
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9065
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look in class Object for wait() and notify()
 
Rajinder Yadav
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic