Win a copy of Terraform in Action this week in the Cloud 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
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Thread

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

I have a doubt in the below code. I expect the output to be "In Threader" but the output is "In Pooler". Can someone please help me understand this code



Thanks in advance!!
 
Bartender
Posts: 3733
39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good question,

It is truly expected that you create Thread and pass Runnable via constructor, and then this thread can perform task from Runnable.

Or you code your own task inside run() method

But not both

Look here at default run() method implementation the Thread:



This answers your question: by overriding in Pooler class the run() method you denied Runnabale (a Threader) instance chance to run.

Instead your implementation of the run() executed:



 
Saloon Keeper
Posts: 7181
166
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And to expand on what Mikalai said, you probably should not ever subclass Thread. There are some cases where you need to do that, but they are very rare indeed. I understand this is an exercise, but just keep this in mind for future reference if you ever write concurrent code.
 
Saloon Keeper
Posts: 1672
61
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What both of them said, but I will take the opportunity to advocate for a practice we are allowed to do (not specific to threads) that I think often helps us read code quickly and understand what it does.

If this code in Pooler:


Instead said:


Without it, on a timed test where I was rushing, I might have made the same mistake that you did reading the first one (and I am planning to take the test!)

If I saw the @Override, which never changes the meaning of the code when you are overriding, but shows the reader of your code, "Hey, look over here!!  This is an Override of an interface, abstract or concrete class method!" I would not have gotten it wrong.  This is why I like them.  Long ago, Java didn't have this, but it has for some time, and I find it causes me to make fewer mistakes when reading or writing code quickly under pressure.

 
Bartender
Posts: 4709
183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And to add to all previous repliers: a way to get both 'in Pooler" and "in Threader" printed is to override the run-method in Pooler as follows:
 
Bartender
Posts: 2125
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please refer to the attached diagram. I put on a debugger and found out that t is thread-0, h is thread-1. thread-1 starts and runs its own run method and
thread-0 reference has nothing to do with thread-1 in this case.

Also, if you change your code into:


Then, h thread starts  and executes t thread's run method. This is a typical way to execute a thread which is equivalent to :
image_2021-10-18_212912.png
[Thumbnail for image_2021-10-18_212912.png]
 
reply
    Bookmark Topic Watch Topic
  • New Topic