Sample multi-threaded program using join, synchronized, wait and notify

Thread Illustration

package kunkunur.org.design.threads;

import java.util.LinkedList;
import java.util.Queue;

public class ThreadIllustration {
	private static Queue<Integer> jobQueue = new LinkedList<Integer>();

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		ThreadIllustration monitor = new ThreadIllustration();

		Thread controller = new Thread(new Controller(monitor));

		controller.start();
	}

	private static class Controller implements Runnable {
		private Object monitor;

		public Controller(Object monitor) {
			this.monitor = monitor;
		}

		public void run() {
			try {
				Thread producer = new Thread(new Producer(monitor));
				producer.join();

				Thread consumer = new Thread(new Consumer(monitor));
				consumer.join();

				producer.start();
				consumer.start();

				System.out.println("This will always get printed first");
			} catch (InterruptedException ie) {
				System.out.println("Controller has been interrupted");
			}
		}
	}

	private static class Producer implements Runnable {
		private Object monitor;

		public Producer(Object monitor) {
			this.monitor = monitor;

		}

		public void run() {
			try {
				while (true) {
					synchronized (monitor) {
						if (jobQueue.isEmpty()) {
							System.out
									.println(Thread.currentThread().getName());
							jobQueue.add(1);
							monitor.notify();
						} else {
							monitor.wait();
						}
					}
				}
			} catch (InterruptedException ie) {
				System.out.println("Producer has been interrupted");
			}
		}
	}

	private static class Consumer implements Runnable {
		private Object monitor;

		public Consumer(Object monitor) {
			this.monitor = monitor;
		}

		public void run() {
			try {
				while (true) {
					synchronized (monitor) {
						if (jobQueue.isEmpty()) {
							monitor.wait();
						} else {
							System.out.println(Thread.currentThread().getName() + jobQueue.remove());
							monitor.notify();
						}
					}
				}
			} catch (InterruptedException ie) {
				System.out.println("consumer has been interrupted");
			}
		}
	}

}

Sample Output:

This will always get printed first
Thread-1
Thread-21
Thread-1
Thread-21
Thread-1
Thread-21
Thread-1
Thread-21

Data Structures Visualization

Requirements

  1. List all the data structures ex: Queue, Stack, Tree
  2. Support tagging at each data structure instance. ex: LinkedList may be tagged as list, queue.
  3. Represent the hierarchy of data structures visually using the above tags. e.g. abstract data types —> concrete data types —> to use-case specific/application data types. Concrete example Tree —> Binary Tree –> Binary Search Tree.
  4. List problems with their definitions along with tagging by their types.
  5. List algorithms with their psuedo code along with tagging by their types.
  6. Link data structures to problems and algorithms.
  7. Allow instantiation of a data structure with specified parameters.
  8. Represent the data structure visually. Generate a DOT file on the server side and use GraphViz/Tulip to generate an image dynamically and send the image over to the browser.
  9. Allow LCRUD on all the three – problems, algorithms, datastructures.
  10. Allow REPL to solve the problems within UI.

Implementations

  1. Starting a Ruby on Rails project on this soon on github.

References

  1. Graphviz based UI visualization – http://www.youtube.com/watch?v=IxSSbUrgTBY
  2. Graph gem – https://github.com/seattlerb/graph
  3. Project with  my data structure implementations and some problems with solutions  – https://github.com/sandeepkunkunuru/CodingExercises