Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Threads - Basic question

  Asked By: Donna    Date: Mar 21    Category: Java    Views: 602
  

Please consider the following simple program culled from a book on
Java :

Class CurrentThreadDemo
{
public static void main(String args[])
{
Thread t = Thread.currentThread();
t.setname("My Thread");
Thread.sleep(1000);
}
}

We see here use of the following 3 methods :
1. Thread.currentThread()
2. t.setname() and
3. Thread.sleep()

Is there are any particular reason why Methods 1 and 3 should be
Thread.something
whereas Method 2 should be t.something ?
What is the underlying concept here ? I'm confused about this.


I have seen another example where the following code has been used :

ob1.t.join();

where ob1 is a reference variable to a thread.

Can anyone explain this syntax and exactly why this syntax is so ?
(Note : I understand the concept of join() - it's just the other
syntax I'm confused about and don't understand.)

Share: 

 

2 Answers Found

 
Answer #1    Answered By: Bethany Hughes     Answered On: Mar 21

I think at the first and third function called are static  methods and they are
called with tha class  name.
The second function is a non-static function which needs to be called with an
object.

Please correct me if I am wrong.

 
Answer #2    Answered By: Herbert Weaver     Answered On: Mar 21

when you read the javadocs you will see that method  1 und 3 are static. So
you should access them in a static  manner. This means:
Thread.sleep(xx)
The setName is not static so you have to instantiate or get the correct
object before you call this method.

 
Didn't find what you were looking for? Find more on Threads - Basic question Or get search suggestion and latest updates.




Tagged: