Big O notation

May 14, 2016

Hello,

Came across a very nice introductory article on Big O notation.

https://rob-bell.net/2009/06/a-beginners-guide-to-big-o-notation/


Polymorphism without Inheritance

October 6, 2010

Can Polymorphism exist without Inheritance?

Abstraction“, “Encapsulation“, “Inheritance” and “Polymorphism” forms the basic building blocks of OOA&D.
We are so much overwhelmed by these facts that an immediate answer is “No“.

In Java there are two types of Polymorphism. One is Runtime(Dynamic) and other is compile time(Static).
In Runtime polymorphism the actual method to be invoked is decided based upon the actual object that the reference type refers at Runtime. This is achieved by OVERRIDING the method in sub classes. Of course this is where “Inheritance” is the KEY which is used to implement “Polymorphism”.

In Compile time or Static polymorphism, the method to be invoked is decided at compile time by diagnosing parameters of the method. This is implemented using Method OVERLOADING. Now this one does not depend on the Inheritance hierarchy because a method can be overloaded in different ways in the same class.

What does this mean?
Even without the Inheritance hierarchy we can have Polymorphism though of a Static kind.

Cheers,

Amit


Multithreading and Locking Strategies

July 30, 2010

Hello,
Concurrency in Java 5 has introduced new ways of developing multi-threaded applications.

The basic difference in these two APIs is Locking Strategy that is implemented for achieving the concurrency.

Java 5 concurrency API use Compare And Swap(CAS) mechanism. In CAS the original value is compared with the value that was read last time, if it matches then the thread modifies the shared variable with new value. The thread in this case is optimistic hoping that no other thread has modified the original value. This is the Optimistic locking strategy.

In Java 4, we use the synchronized keyword for locking the resource. This way we use the Pessimistic locking strategy. Because we lock the shared resource exclusively for the current thread. No other thread can access and modify the variable.

Both approaches have their own advantages and shortcomings. In this entry I just wanted to highlight the implementation difference that appealed me while reading the article.

Cheers !!
Amit


Producer-Consumer using Java Threads – wait,notifyAll

April 27, 2010

Hello,

In Java , threads communicate with each other using “wait,notify and notifyAll” APIs.  Producer-Consumer is the classic example to explain these APIs. The example below tries to show how we can use inter-thread communication to read shared data when it is available. I have also tried to demonstrate use of Thread Interrupt using which we can cancel the running task, in this case exiting the application.

import java.io.BufferedReader;
import java.io.InputStreamReader;
/**
 * @author Amit
 * This Producer-Consumer example tries to read/write one shared String variable.
 * This example also uses Thread interrupt concept to stop the application.
 * The variable in main() are defined final so that we can use them in
 * Anonymous inner class implementation of thread within main() method.
 */
public class ConsumerProducerTest {

	static String data = new String();

	public static void main(String args[]) {

		//Declaring these variables as final so that
		//I can access them inside the Anonymous inner class
		//for user thread.
		final Object lock = new Object();

		final Thread readerThread = new Thread(new Reader(lock));
		final Thread writerThread = new Thread(new Writer(lock));
		writerThread.start();
		readerThread.start();

		//User Input Thread
		final BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		new Thread(new Runnable() {
			public void run() {
				while(true) {
					try {
						String str = in.readLine();
						if("quit".equalsIgnoreCase(str)) {
								readerThread.interrupt();
						}
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}

		}).start();
	}
}
class Reader implements Runnable {

	private Object lock;

	Reader(Object lock) {
		this.lock = lock;
	}

	public void run() {
		System.out.println("Reader::run");
		while(true) {
			synchronized(lock) {
				if("".equals(ConsumerProducerTest.data)) {
					System.out.println("Reader is going to wait");
					try {
						lock.wait();
					} catch (InterruptedException e) {
						System.out.println("Reader thread interrupted !!");
					}
					System.out.println("After call to Reader wait");
				} else {
					System.out.println("Reader got the data" + ConsumerProducerTest.data);
					System.out.println("Reader is notifying");
					ConsumerProducerTest.data = ""; //Clear the data
					//Wait for 10 seconds before notifying other threads.
					try {
						Thread.sleep(10000);
					} catch(InterruptedException ie) {
						System.out.println("Reader is interrupted while sleeping!!");
						System.exit(0);
					}
					lock.notifyAll();
					System.out.println("Reader done notifying");
				}
			}
		}
	}
}

class Writer implements Runnable {

	private Object lock;

	Writer(Object lock) {
		this.lock = lock;
	}

	public void run() {
		System.out.println("Writer::run");
		while(true) {
			synchronized(lock) {
				if("".equals(ConsumerProducerTest.data)) {
					ConsumerProducerTest.data = "data-"+System.currentTimeMillis();
						System.out.println("Writer wrote the data - " + ConsumerProducerTest.data);
						System.out.println("Writer is notifying reader");
						lock.notifyAll();
						System.out.println("Writer done notifying");
				} else {
					try {
						System.out.println("Writer is going to wait");
						lock.wait();
						System.out.println("After call to Writer wait");
					} catch(InterruptedException ie) {
						System.out.println("Writer is interrupted !!");
					}
				}
			}
		}
	}
}

Hope you find this useful.

Cheers !

Amit