File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Is Thread a static class? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Is Thread a static class?" Watch "Is Thread a static class?" New topic
Author

Is Thread a static class?

Chiran Mathur
Ranch Hand

Joined: Feb 07, 2001
Posts: 63
If it is then why do we instantiate Thread in this manner
Thread t=new Thread().
If it is not, then how can we use Thread in the following manner:
class A implements Runnable{
public void run(){
System.out.println("Hello I am there");

try{
Thread.sleep(500);
}
catch(InterruptedException e){
System.out.println("Hey I caught it!!");
}
}
}
I am confused here...
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Briefly,
sleep() is a static method declared in the Thread class.
The statement
Thread.sleep() is perfectly legal and does not mean that Thread is a static class. Thread is a top-level class and top-level classes cannot be static.


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Mandar Puranik
Ranch Hand

Joined: Jan 08, 2003
Posts: 35
Hi Chiran,
I will try to explain it.
When you do a Thread t = new Thread();
it instantiates a new object t, of type Thread.
But when you are doing a Thread.sleep();
you are telling the program, which itself is a thread, to do a sleep.
i.e you can do a t.sleep(), in your example.
Ranchers, let me know if I am correct.
Thanks
Mandar


To Bug is Human,<br />To Debug Divine... :-))
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
sleep(x) causes the currently executing thread to sleep during x milliseconds. Note that the currently executing thread is not necessarly the "program" as you mentioned.
Invoking a static method on a reference (t.sleep()) is considered a bad practice and should not be done in "good" code (see Java coding conventions: 10.2 Referring to Class Variables and Methods )
[ January 29, 2003: Message edited by: Valentin Crettaz ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is Thread a static class?