Showing posts with label object. Show all posts
Showing posts with label object. Show all posts

Monday, 23 February 2009

Thinking like Eckel: Objects are Like Trains

Basics First: The wait/notify dynamic

One fine day, an instance of the java.lang.Object class had its wait() method invoked and application X came to a standstill. Why and wherefore did this happen?

void wait()

wait throws an InterruptedException. It causes the current thread to WAIT until another thread calls notify or notifyAll on the same object.

wait() == wait(0) == wait(0,0)

which means invoke wait with a zero millisecond timeout (i.e. never timeout). The current thread must own the object's monitor. Think of it as a UNIX process that sleeps until some event occurs or a bus that stops at traffic lights and must be "notified" either by the traffic lights or by a car honking from behind it! An object, to put it in baby terms, is like a "choo-choo train"! A "choo-choo" train with locks that can only be manipulated by synchronized methods! Tampering illegaly with the locks results in a vandalism-detection mechanism being triggered.

Some notes on wait/notify

There's a little more to wait/notify than meets the eye and some of this "extra insight" can be obtained from reading good Java books such as Bruce Eckel's Thinking in Java (Bruce Eckel is both a "Java Black Belt" and also founding member of the C++ Standards Committee, the two facts being not entirely unrelated). One key point he makes is that synchronized methods of an object can be called on an object in a wait state, precisely because calling wait releases the lock on the object. Because wait does jiggery-pokery with the object, calling wait outside of a synchronized method results in an IllegalMonitorStateException (present since JDK 1.0 so don't act all surprised!)

Wednesday, 30 July 2008

What is object equality in Java?

The Object class in Java has two methods that refer to equality: equals() and hashcode(). In general, if you override one of these methods, you must override both, as there are important relationships between the two.

Specifically? If two objects are equal according to equals() they must have the same hashcode() value (reverse not necessarily true).

if o1.equals(o2) then hashcode(o1) == hashcode(o2)

The default implementation of equals() provided by Object is reference equality:

public boolean equals(Object obj) {
return (this==obj);
}

This is a neater way of saying if(this==object) return true; return false;.

By this definition, 2 refs are equal only if they refer to the exact same object.

Friday, 25 July 2008

Getting to know java.lang.Object

This is the fundamental class in Java, every Java programmer ought to know it backwards! The Object class has concepts of multithreading, hashing, copy construction and RTTI embedded into it.

An example of RTTI built into the Object class is the getClass method, which returns the runtime class of an object. An example of the copy construction concept built into Object is the clone method.