• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Instance initializer Block priority vs start() method priority

 
Ranch Hand
Posts: 158
1
Eclipse IDE VI Editor Objective C C++ Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


In this code above I was expecting that instance initializer would execute first setting up a custom name of the thread, but that did not happened
is it because we have included method?

can someone please explain me the flow of controls in detail here?
 
Sheriff
Posts: 28371
99
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
The instance initializer of the Task1 class changes the name of the current thread to "Bingo". It's true that a Task1 object is a Thread, but I don't see how it can be the current thread when its instance initializer is only just running. So that line of code changes the name of some other Thread... presumably the thread which is creating the Task1 object. You could try changing the initializer to and see what happens.

As for your question about t1.join() and whether it makes a difference, I would leave it out and see whether that makes a difference.
 
Master Rancher
Posts: 5112
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I added some more print statements to see when the new Thread was created.  It looks there is a new Thread when run is executed:
 
Paul Clapham
Sheriff
Posts: 28371
99
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
The API documentation for the Thread class contains many, many instances of the phrase "current thread". But it never gets around to saying what that means. Perhaps the writers thought it was too obvious to mention.

It looks like you've found an example which shows why it isn't that obvious. Basically, the documentation of Thread goes on and on about the "current thread" without mentioning that it's a thread, a native resource in the JVM, and not a Thread object. The two concepts are different. Once a Thread's start() method is called, that Thread object is associated with a thread. The start() method calls the run() method which is associated with the Thread object and from then on, every time that code in that Thread calls Thread.currentThread(), it gets a reference to itself.

But before the Thread object becomes associated with a thread by having its start() method called, a call to Thread.currentThread() can't return a reference to that object because it isn't associated with any thread. Some other thread is the current thread. Your example shows this in action.
 
What are your superhero powers? Go ahead and try them on this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic