Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Casting Thread.currentThread()

 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

What are teh implications of casting the result of Thread.currentThread() ?

For instance I have code that looks like this:

class Dispatcher extends Thread {
public void run(){
// does some stuff
}
}

class TestListener extends Listener {

public void onMsg(Message msg){
Dispatcher disp = (Dispatcher) Thread.currentThread();
}


My guess is an instance of Dispatcher takes over the current thread. But does an instance of Dispatcher get created and what happens to the instance variables, local variables etc. ??

Thanks in advance,

J.C

 
Sheriff
Posts: 22649
126
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Output: class Test

So yes, Thread.currentThread() returns the actual Thread object you (or some other library code) created. That can be a Dispatcher thread, but it all depends on the Thread object it is running in. Safest is to use instanceof to check first.
 
Marshal
Posts: 27211
87
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

James Clarke wrote:My guess is an instance of Dispatcher takes over the current thread. But does an instance of Dispatcher get created and what happens to the instance variables, local variables etc. ??



No. That's not how casting works. There's a Thread object (in your example, the current thread). By casting, you simply assert that the object is an instance of the Dispatcher class, and assign it to a variable of that type. That doesn't affect the Thread object in any way at all. No new objects are created. The object is simply assigned to a variable.

And as Rob said, if you don't check that the Thread object is actually an instance of Dispatcher then you might get ClassCastExceptions when you do this.

By the way I just gave you a description of how casting works in general. That of course is also a description of how casting works for Threads, and any other class. It always works the same.
 
James Clarke
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Doesn't answer the question. And your example is different.

Does an instance of Dispatcher get created and what happens to the instance variables, local variables etc. ??
And can you confirm how the current thread gets replaced and the underlying things taking place in the JVM ?

Thanks,

J.C
 
Master Rancher
Posts: 4195
57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

James Clarke wrote:Doesn't answer the question. And your example is different.


Doesn't even bother to tell us which post it's in response to. And your original question was poorly defined, anyway.

James Clarke wrote:Does an instance of Dispatcher get created


You haven't shown enough code for anyone to determine that, really. How (if at all) do you expect the code in your Dispatcher object to be run? It doesn't happen by magic, after all. It will only happen if you do something to start a new thread. And the answer to your question depends on what you do to start that thread. If you explicitly create a new Dispatcher instance, then yes, an instance of Dispatcher will be created. If you don't, it won't. It surely won't happen if you just desire for some thread to execute your new run() method. There are several ways to cause a Runnable to execute. You have to pick one, and do it.

Even if you manage to instantiate a Dispatcher object and call its start() method (thereby creating a new Thread for which Thread.currentThread() would yield a Dispatcher instance) - we have no information about how this Listener class works. If something happens in the Dispatcher thread and the Listener is notified, is the Listener in the same thread as the Dispatcher? Maybe, maybe not. There are several common mechanisms for this, and they each yield different results. So there's no way to answer your question, really. It's far too soft on details to result in useful answers.

If you want to make your question more specific, consider: do you know how to start a new thread? At all? Can you write specific code to do so? And if you know more than one way, can you pick one, and tell us what it is? Similarly: can you write a mechanism for notifying a listener? And can you show us the code? If not (for any of these questions), perhaps it might be more useful if you focused on more fundamental questions. E.g. instead of "how does X (which I haven't really defined) affect Y?", try "how can I accomplish X?". And once you have some code for X, run it, and see what happens. Then you can replace "how does X affect Y?" with something much more specific, like "how come when I implemented X like this [show code], I got this result? [show result].

Basically, show more details of what you're wondering about, and we may well be able to help. Until then, probably not.

 
Rob Spoor
Sheriff
Posts: 22649
126
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thread.currentThread() never creates a new object. It simply returns a Thread object you have previously created. As such, the return value of Thread.currentThread() contains all the values that have been previously assigned to its fields. In this regard, the Thread is just like any other Java object, and Thread.currentThread() simply returns a reference to it.
 
I have a knack for fixing things like this ... um ... sorry ... here is a consilitory tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic