Page 2 of 2 FirstFirst 12
Results 21 to 26 of 26
  1. #21
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,004
    Blog Entries
    7
    Rep Power
    20

    Default Re: Java Thread - Sleep

    That's nasty; shame on you ;-) the garbage collector fires up separate threads for the finalization, so at least one of them will fall asleep for quite some time; I wonder if it will be reaped by the collector and consider the object to be finalized; I'll try to check it ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  2. #22
    notivago is offline Heavy Coffe Drinker
    Join Date
    Feb 2014
    Location
    São Paulo, Brazil
    Posts
    29
    Rep Power
    0

    Default Re: Java Thread - Sleep

    The sample code is littered with bad style, first you should not extend Thread, you should, almost always, implement runnable and then pass it to a Thread, Thread.sleep is a static method and should be referenced thus, in the end the effect is the same but that way it is confusing to anyone reading that will think the method is instance and is operating over the instance,

    There is no guarantee in thread execution order and it is up to the machine scheduler decide which thread gets the spotlight, even if you make some sleep long periods. Declaring class as package is not a good idea, unless you have a good reason otherwise you should have it public.

    And for some fun with garbage collection, inspired Kjkrum's "design":
    https://bitbucket.org/notivago/sampl...il/?at=default

    It is a fast OOM.

  3. #23
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,935
    Rep Power
    4

    Default Re: Java Thread - Sleep

    The OP didn't extend Thread. Those were anonymous classes. In any event the first way shown of creating a thread in the JDK documentation is extending Thread. The other way is as you say, implementing runnable. Both are acceptable. I prefer that latter. Finally, I'm not certain what you mean by declaring class as a package. The OP just showed a class. There was no package statement and no imports as they were not relevant to the question.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  4. #24
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,058
    Rep Power
    5

    Default Re: Java Thread - Sleep

    You're right, it's not the end of the world... but those anonymous classes do extend Thread.
    Get in the habit of using standard Java naming conventions!

  5. #25
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,935
    Rep Power
    4

    Default Re: Java Thread - Sleep

    Yes, they implicitly extend Thread. But the OP didn't do it -- it was done behind the scenes. Usually, IMHO, when one says you should not extend a class they are talking about actually using the extends keyword. However, I probably should have been more careful in my wording. Something like, "well how to you create an anonymous instance of a class without Java extending the class?" So I guess some folks then don't believe you should create an anonymous instance of Thread.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  6. #26
    notivago is offline Heavy Coffe Drinker
    Join Date
    Feb 2014
    Location
    São Paulo, Brazil
    Posts
    29
    Rep Power
    0

    Default Re: Java Thread - Sleep

    Quote Originally Posted by jim829 View Post
    The OP didn't extend Thread.
    No, he didin't, the book people did. I said the code was littered with bad mojo, did not say it was OP fault. Still I do think he should know that the code provided is Bad Style(TM), else he will learn java thinking it is OK to do such a thing.

    Those were anonymous classes.
    Anonymous classes that extend thread. The first problem there is about meaning, when you implement runnable you are saying that you are writing something to be run, and it will run in a thread, whilst when you extend thread you are declaring that you are going to add to the threading mechanism, which you wont, you are using a curve path to make a thread run something.

    Then there is the fact that when you extend thread you are spending the class hierarchy, and you are doing that on something that is not the main concern of your class, the main concern of a trhead is to be the means to paralelize execution of code while your class probably will be concerned with something entirely different which happends to be executed.

    And the fact that all of this is being done on anonymous classes only adds to the bad style as it is killing reuse, documentation, clarity.

    In any event the first way shown of creating a thread in the JDK documentation is extending Thread.
    And it also explains that the Runnable way is the preferred, that extending thread is the quick and dirty way for small app that does not use advanced threading api, just to point one, if you need the code to be run in swing you cant pass thread to invoke later.

    The other way is as you say, implementing runnable. Both are acceptable. I prefer that latter.
    Any form of obscure code is acceptable, depending on who you ask it. Extending thread is bad design.

    Finally, I'm not certain what you mean by declaring class as a package. The OP just showed a class. There was no package statement and no imports as they were not relevant to the question.
    I was refering to the access known as package-private.

    May the code be with you.
    -- notivago

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Thread.Sleep
    By allaudin in forum New To Java
    Replies: 2
    Last Post: 07-17-2013, 07:22 AM
  2. Replies: 1
    Last Post: 09-24-2012, 07:01 PM
  3. Difference between Thread.yield() and Thread.sleep() methods
    By Nageswara Rao Mothukuri in forum New To Java
    Replies: 12
    Last Post: 07-30-2010, 05:37 PM
  4. how to reduce the thread sleep time and wake up the thread
    By baktha.thalapathy in forum Threads and Synchronization
    Replies: 2
    Last Post: 06-24-2010, 07:36 PM
  5. Sleep in thread
    By jithan in forum New To Java
    Replies: 1
    Last Post: 08-27-2008, 02:27 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •