CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jan 2006
    Posts
    22

    Question Object's methods

    Hi All

    Help me on following two questions.

    1. Why clone() method available on Object class? and what are the problem if it is declare on clone able interface.
    2. Like above, We need wait, notify , notifyAll methods on case of multithreading, so why these methods are declare on Object Class not on Thread Class?

    Thanks
    sammoh

  2. #2
    Join Date
    Nov 2007
    Location
    Birmingham, England
    Posts
    157

    Re: Object's methods

    I can't give a good answer to 1 but I can answer 2 for you a little.

    Simply being related to threading does not mean that the concept fits well into the thread class. For one thing, how would "notifyAll()" know which threads were waiting?. This function releases all threads waiting on one object. But just because you need to notify all threads on that object doesn't mean you want to notify all threads in your program. Grouping waiting threads around an object is very important.

    "So why not have another class for this purpose?"
    This would make more sense. In fact if you Google for "Semaphore" you'll find heaps about how this works in other languages...

    But java has a very cool trick...

    Semaphores are more difficult to use well than many people realize and a mistake with multithreaded code is the most difficult kind of bug to find and fix.

    The keyword "synchronized" in java makes multi threading very easy. Java grantees that only one thread can call a synchronized function within an object at a time, all other threads are forced to wait!

    To do this every object implicitly must have its own semaphore just to allow the Java "synchronized" syntax to work.

    I guess that when the developers of java realized this there was no need in making another "Semaphore" class.
    Signature
    Please use: [ code ][/ code ] tags and reasonably correct tabbing. They really help us read your code
    End Signature

  3. #3
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Object's methods

    I guess that when the developers of java realized this there was no need in making another "Semaphore" class
    Err apart from the java.util.concurrent.Semaphore class that was introduced in Version 1.5.

    Synchronization works well in certain conditions but does not suit all circumstances for instance if you want to limit access to more than one caller.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  4. #4
    Join Date
    Feb 2008
    Posts
    966

    Re: Object's methods

    Quote Originally Posted by sammoh View Post
    1. Why clone() method available on Object class? and what are the problem if it is declare on clone able interface.
    2. Like above, We need wait, notify , notifyAll methods on case of multithreading, so why these methods are declare on Object Class not on Thread Class?
    Well, if you read the API it states:
    The class Object does not itself implement the interface Cloneable, so calling the clone method on an object whose class is Object will result in throwing an exception at run time. The clone method is implemented by the class Object as a convenient, general utility for subclasses that implement the interface Cloneable, possibly also overriding the clone method, in which case the overriding definition can refer to this utility definition by the call:
    super.clone()
    The Cloneable class is an interface and thus has no actual implementation of the clone() method. Since Object is NOT an interface, it can provide a base implementation. This being said, if you try to use the clone() method in a class that does not implement Cloneable, you will get a run time exception.

    For threading, there are more way to use threads in Java than just using the Thread class. You could implement Runnable instead. Since the Runnable class is a class used for multi threading it needs to have an implementation of the stated methods. Since it is an interface, it cannot implement them in the Runnable class.

  5. #5
    Join Date
    Nov 2007
    Location
    Birmingham, England
    Posts
    157

    Re: Object's methods

    Quote Originally Posted by keang View Post
    Err apart from the java.util.concurrent.Semaphore class that was introduced in Version 1.5.
    Hmmm. I tend to find the uses of integer Semaphores are a little different to those of boolean semaphores. But yes I did neglected to be specific about which type I was referring to.

    Also Java 1.5 came a lot later than wait and notify. The class Semaphore only wraps up code which which was already possible and being implemented by individual developers without it. Though it is of course possible that Sun had a change of heart over their design .
    Signature
    Please use: [ code ][/ code ] tags and reasonably correct tabbing. They really help us read your code
    End Signature

  6. #6
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Object's methods

    Though it is of course possible that Sun had a change of heart over their design
    I suspect it wasn't so much a change of heart, more of a case that in some instances an integer Semaphore class is easier to use and makes the code easier to understand than using synchronization.

    I certainly use both mechanisms, choosing the most appropriate one for the task in hand.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured