Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

About Thread

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
SwetaSweta Singh
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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();
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[Deleted as unrelated to thread title]
 
Nicola Garofalo
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[Deleted as not relevant to thread title. Copy of this post as "quote" in my second post on this thread.]
 
Ads Nct
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the above question has been posted to another thread named as"Overriding of Member variables "
Sorry for the inconvinience caused.
 
Marshal
Posts: 74775
336
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 74775
336
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
reply
    Bookmark Topic Watch Topic
  • New Topic