Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Comments on this code?

  1. #1
    Junior Member
    Join Date
    Jul 2019
    Posts
    22
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Comments on this code?

    My predecessor (a very experienced Java programmer who has left) has written Java code and I don't understand why he did it that way. Does anyone have an idea when you would like to do something like that? Do you have other comments about the code?
    @SuppressWarnings({"ClassWithoutLogger", "FinalClass"})
    public final class ThreadUtil {
     
        static public boolean sleep(long pS, final Object pO) {
            try {
                synchronized (pO) {https://www.javaprogrammingforums.com/editpost.php?p=167774&do=editpost
                    pO.wait(pS);
                }
            } catch (InterruptedException e) {
                return true;
            }
            return false;
        }
     
        private ThreadUtil() {
        }
     
    }
    Last edited by jenniferruurs; December 10th, 2019 at 06:40 PM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    24,343
    Thanks
    54
    Thanked 2,636 Times in 2,586 Posts

    Default Re: Comments on this code?

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.

    How is that method used in other parts of the code?
    What does its caller do when true is returned?
    What does it do when false is returned?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    24,343
    Thanks
    54
    Thanked 2,636 Times in 2,586 Posts

    Default Re: Comments on this code?

    How is that method used in other parts of the code?
    What does its caller do when true is returned?
    What does it do when false is returned?
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Junior Member
    Join Date
    Jul 2019
    Posts
    22
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Comments on this code?

    It is isolated code.
    It is not used, but i don't understand the code itself and why someone would write it like this.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    24,343
    Thanks
    54
    Thanked 2,636 Times in 2,586 Posts

    Default Re: Comments on this code?

    It is not used,
    Ok, then it could be deleted without any problems.

    Read the API doc for the Object class's wait method to see what it does: http://docs.oracle.com/javase/8/docs/api/index.html
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Member
    Join Date
    Apr 2014
    Posts
    93
    Thanks
    3
    Thanked 7 Times in 7 Posts

    Default Re: Comments on this code?

    It seems your predecessor wanted to have an alternative to Thread.sleep(), but I'm not sure why that would be helpful. To fully understand HOW it works (not necessarily WHY), have a look at the API for Object.wait() and Object.notify(). It's a cool mechanism, although utilized in a seemingly unorthodox manner here.

Similar Threads

  1. Comments In Java
    By Flying216 in forum The Cafe
    Replies: 2
    Last Post: February 2nd, 2014, 07:45 AM
  2. How do you feel about my javadoc comments?
    By bdennin in forum Java Theory & Questions
    Replies: 1
    Last Post: January 31st, 2013, 08:38 PM
  3. putting comments in for code
    By BLUEJ in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 7th, 2013, 07:52 PM
  4. Comments or Suggestions on Some Code
    By mike_p in forum Object Oriented Programming
    Replies: 2
    Last Post: March 8th, 2012, 05:51 PM
  5. Comments
    By igkarthi in forum Java Theory & Questions
    Replies: 1
    Last Post: January 27th, 2012, 07:52 AM