-
November 10th, 2010, 03:01 PM
#1
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
-
November 12th, 2010, 11:30 AM
#2
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
-
November 12th, 2010, 12:25 PM
#3
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.
-
November 12th, 2010, 01:42 PM
#4
Re: Object's methods
Originally Posted by sammoh
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.
-
November 15th, 2010, 08:27 AM
#5
Re: Object's methods
Originally Posted by keang
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
-
November 15th, 2010, 11:31 AM
#6
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 Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|