4

I have used -

Object o = new Object(); 

for thread synchronizations and this is helpful because making the lock object private encapsulates the lock so that client code cannot acquire it, but don't know any other use of this.
What are the other reasons that Object class is not abstract? In which other situation I can use above code?

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
Premraj
  • 7,802
  • 8
  • 45
  • 66
  • 6
    "Denial of service attacks"? What do you mean by that? – Adam Paynter Mar 20 '11 at 10:35
  • 4
    "protect the object from denial of service attacks" but how? – CloudyMarble Mar 20 '11 at 10:36
  • I usually just use it to demonstrate how to create new objects to Java/OOP newbies. – BoltClock Mar 20 '11 at 10:39
  • 1
    I just mean to say - If class A uses intrinsic locks for synchronization and if some client takes lock on class A, then A instance won't be able to cater the the client.. couldn't found the another way to describe this, if you have another technical name then pls edit this. – Premraj Mar 20 '11 at 10:41
  • @Falcon: "Denial of service" is a security term, nothing to do with thread synchronization. – skaffman Mar 20 '11 at 11:06
  • @skaffman - Isn't this security of code, if another code locks the object – Premraj Mar 20 '11 at 11:09
  • 2
    @Falcon: No, it's nothing to do with security, it's about safety. Two different things. – skaffman Mar 20 '11 at 11:13
  • 1
    @Falcon Maby you are right, but the term DOS - "Denial Of Service" has another meaning, so it can be very confusing trying to figure out what you mean when you write it in the wrong context. – CloudyMarble Mar 20 '11 at 11:15
  • 1
    @skaffman - hmm.. got it.. removing the line.. thanks :) – Premraj Mar 20 '11 at 11:19
  • If you are going to the trouble of creating your own lock, you would use `Lock` IMHO. Even using an Object for synchronizaton isn't so useful these days. – Peter Lawrey Mar 20 '11 at 11:29
  • @Falcon: I can appreciate what you meant. I suppose I didn't think of that as security, considering security has effectively failed if rogue code is now running in your JVM and it has access to your `ClassLoader` and it has access to your objects. – Adam Paynter Mar 20 '11 at 11:29
  • @Adam Paynte - Thanks and agreed. – Premraj Mar 20 '11 at 11:32
  • 1
    @Peter, there is one overlooked aspect of `synhcronized(object)` it survives thread.stop(), so it the code is truly critical library code, synchronized is a bit more robust. – bestsss Mar 20 '11 at 13:40
  • 2
    possible duplicate of [Why java.lang.Object is not abstract?](http://stackoverflow.com/questions/2117689/why-java-lang-object-is-not-abstract) – meriton Mar 20 '11 at 14:46

1 Answers1

3

The main practical utility of just creating a generic object would be to leverage its locking capabilities (e.g., wait() and notify()). But this may be what you are referring to by "denial of service", since use of these methods can help manage threads and potentially help in a defense of DoS. (but that is really app specific, and not inherent to the purpose of these methods within Object)

The reason(s) why Object is not abstract is already discussed at length here:

Why java.lang.Object is not abstract?

Community
  • 1
  • 1
kvista
  • 5,039
  • 1
  • 23
  • 25