Sunday, March 4, 2012

Concurency

Context Switches involved in notify notifyAll and conditional notify.

- Conditional Notify () works best.
- A signal from Notify () can be easily missed - see how
- NotifyAll() wakes up all threads and puts them into runnable state. Hence involves context switches (for each thread woken up)


Antipattern

http://en.wikipedia.org/wiki/Anti-pattern#Software_design_anti-patterns

Static Imports (since Java 5.0) 


Static import is a feature introduced in the Java programming language that allows members (fields and methods) defined in a class as public static to be used in Java code without specifying the class in which the field is defined. This feature was introduced into the language in version 5.0.
The feature provides a typesafe mechanism to include constants into code without having to reference the class that originally defined the field. It also helps to deprecate the practice of creating a constant interface: an interface that only defines constants then writing a class implementing that interface, which is considered an inappropriate use of interfaces[1].

import static java.lang.Math.*;

import static java.lang.Math.PI;
For custom constants Create a Constant class 


BINARY semaphore:

DO WAIT : SSC

Synchronize - Spin (while:signal notset to true -> this.wait()) - Clear (set signal to false)

DONOTIFY: SSN
Synchronize - Signal( set signal to true) - Notify


public class QueueObject {

  private boolean isNotified = false;

  public synchronized void doWait() throws InterruptedException {
    while(!isNotified){
        this.wait();
    }
    this.isNotified = false;
  }

  public synchronized void doNotify() {
    this.isNotified = true;
    this.notify();
  }

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


No comments:

Post a Comment