Monday 23 February 2009

Notes on Lecture 7 : synchronized methods

The following notes are extracts from the Sun Java specification document. They are provided here as supplementary notes to the Singleton pattern that uses a synchronized method for ensuring that there is only a single instance of the Singleton object.

"The Java programming language provides multiple mechanisms for communicating between threads. The most basic of these methods is synchronization, which is implemented using monitors. Each object in Java is associated with a monitor, which a thread can lock or unlock. Only one thread at a time may hold a lock on a monitor. Any other threads attempting to lock that monitor are blocked until they can obtain a lock on that monitor. A thread t may lock a particular monitor multiple times; each unlock reverses the effect of one lock operation.

A synchronized method automatically performs a lock action when it is invoked; its body is not executed until the lock action has successfully completed. If the method is an instance method, it locks the monitor associated with the instance for which it was invoked (that is, the object that will be known as this during execution of the body of the method). If the method is static, it locks the monitor associated with the Class object that represents the class in which the method is defined. If execution of the method's body is ever completed, either normally or abruptly, an unlock action is automatically performed on that same monitor. "


http://java.sun.com/docs/books/jls/third_edition/html/memory.html#56318

Notes on Lecture 7 : Singleton Pattern

On the Singleton implementation shown below, there was a question on why the getSpooler() method does not seem to have a return value in the signature, although the singleton instance _spooler is returned at the end of the method implementation:

That was a typo and I have corrected it. The return type in the signature should be PrintSpooler.

There was also a question relating to declaring the constructor as "private" and whether we may have multiple constructors and whether they would all be private. We make the constructor private so an instance can only be created from within the static method of the class. Since we are only interested in having a single instance of PrintSpooler, it is rather unlikely that we would need the flexibility of having more than one constructors. Most likely we are able to define our one instance exactly and assign a single constructor to it. Certainly, the classic version of Singleton has one constructor. However, if we did have more than one constructor, they would all need to be declared "private" for the same reason.

Public class PrintSpooler {

// a prototype for a spooler class,

// such that only one instance can ever exist

private static PrintSpooler _spooler;

private PrintSpooler()

{ /* private constructor */ }

public static synchronized PrintSpooler

getSpooler() {

if (_spooler == null) _spooler = new

PrintSpooler();

return _spooler;

}

}

Rational Rose: system actors


To distinguish between a human actor and a system actor, you may use an Actor Decoration instead of an Actor Icon.

Tuesday 17 February 2009

clone() in Java

Please check the following documentation for the clone() method of the Object() Class in java, re question on how it compares on object equality...

http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Object.html

Thursday 5 February 2009

New Rational Rose Tutorial

Please check Blackboard->Design->Rational Rose->Tutorial 1

Wednesday 4 February 2009

Rational Rose: expressing loops


Loops in interaction diagrams may be expressed using the note construct as follows:

Rational Rose and UML 2.0






The version of Rational Rose that is available through an academic licence, supports only UML 1.4 and not 2.0. Some features that we learned in the lectures belong to UML 2.0 (e.g. conditional and iterative messages and blocks of code represented with a frame). There is no direct way to express these in RR. The specification menu of a message in an interaction diagram shows all the possible types of messages that are supported:



You may use an indirect way to record the features that are not supported. For example, you may associate a note with a message in order to show that it is conditional.

Representing secondary actors in Rational Rose


As far as I know, there is no separate symbol for a secondary actor in Rational Rose, as opposed to a primary actor. You may add this information in the "Documentation" field, in the Specification menu of an actor entity in Rational Rose, as shown here: