aspose file tools*
The moose likes Beginning Java and the fly likes About Thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "About Thread" Watch "About Thread" New topic
Author

About Thread

SwetaSweta Singh
Greenhorn

Joined: Dec 12, 2008
Posts: 6
I request somebody to explain this. I am finding difficulty in understanding this.
Please explain each part of the statement.


Thread t=Thread.currentThread();

What does each part of the statement stand for especially Thread t? Why is it required.?

What does Thread.currentThread(); stand for and why is it required.


Thanks in advancee
Nicola Garofalo
Ranch Hand

Joined: Apr 10, 2010
Posts: 308
currentThread() is a static method of class Thread.
It returns the reference to the current executing object of class Thread.
How do you want to call this reference to the current executing object of class Thread? Let's say t.

So

Thread t = Thread.currentThread();

After this statement t holds a reference to the current executing thread (i.e. the current executing object of class Thread).

Do you want to change the name to the reference? No problem. Let's say iAmTheCurrentThread

Thread iAmTheCurrentThread = Thread.currentThread();

ah..you asked why is required.
it's required because in multithreading programming you want to know what is the current thread executing, because you want interact with it, for example for changing its state, or getting its name,.


Bye


Bye,
Nicola
SwetaSweta Singh
Greenhorn

Joined: Dec 12, 2008
Posts: 6
Thank you very much for the explaination.


Thread t=Thread.currentThread();

Is there any alternate easy way to write the same.
How does it differ from
Thread t=new Thread();
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

How does it differ from Thread t=new Thread();

This creates a totally new thread. Thread.currentThread() returns the current running thread, not a new one.


[My Blog]
All roads lead to JavaRanch
Ads Nct
Greenhorn

Joined: Apr 13, 2010
Posts: 5
[Deleted as unrelated to thread title]
Nicola Garofalo
Ranch Hand

Joined: Apr 10, 2010
Posts: 308
[Deleted as not relevant to thread title. Copy of this post as "quote" in my second post on this thread.]
Ads Nct
Greenhorn

Joined: Apr 13, 2010
Posts: 5
the above question has been posted to another thread named as"Overriding of Member variables "
Sorry for the inconvinience caused.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38363
    
  23
Ads Nct wrote:the above question has been posted to another thread named as"Overriding of Member variables "
Sorry for the inconvinience caused.
It will also be deleted very soon. Don't ask unrelated questions and hi-jack somebody else's thread.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38363
    
  23
Nicola Garofalo wrote:I think someone should move this post. (and my reply eheh )

Anyway, you override methods not member variables.

After:

A a = new B();

you have an object of class A in hand (its name is a) , with overridden methods by B (none in this case)

The int value i belongs to A then and 10 is what you get on the output.

I hope it's more clear now.
Bye

It isn't possible to move posts independent of threads, I am afraid. this is a reply to the question here.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: About Thread