Aayush: weblog

General-Stuff

Page for logging general stuff, that may be of interest to other JAVA developers.

###################################################

###################################################

——————————————————————————————————-

(A) Socket Programming Demo (Sept 03 2009)

Kickstarting this page with a simple tutorial for JAVA socket programming below.

The code is pretty self explainatory, especially with verbose commenting:

——————————————————————————————————-

import java.io.DataInputStream;

import java.io.DataOutputStream;

import java.io.IOException;

import java.io.ObjectInputStream;

import java.io.ObjectOutputStream;

import java.io.Serializable;

import java.net.InetSocketAddress;

import java.net.ServerSocket;

import java.net.Socket;

/**

* @author aayush
*
* Simple Demo that shows socket programming in JAVA.
*
* This code creates a client socket that connects to a server on port 1919.
* The server accepts the incoming connection from the client.
*
* Now, the client sends across a simple string to the server.
*
* After that, the client sends across a dummy object to the server. Before sending the
* object, it has to be serialized.
*
* The server receives the object and deserializes it and prints its state.
*
*/

public class SocketTester

{

public static void main(String[] args) throws IOException, ClassNotFoundException

{

// My client:

Socket clientsocket = null;

// Output Stream writer for my client:

DataOutputStream out = null;

// My server opening a connection for the client:

ServerSocket receiver = new ServerSocket();

// My server binding to 127.0.0.1:1919:

receiver.bind(new InetSocketAddress(“localhost”,1919));

// Client opening connection to 127.0.0.1:1919:

clientsocket = new Socket(“localhost”,1919);

System.out.println(“Created client socket to connect to 1919”);

// client initializing output stream writer to send data to server:

out = new DataOutputStream(clientsocket.getOutputStream());

// Server accepting the incoming connection:

Socket serverSock = receiver.accept();

System.out.println(“Accept incoming client connection”);

// Server initiailizing the input stream reader to read data sent by the client:

DataInputStream serverin = new DataInputStream(serverSock.getInputStream());

// The client is now sending the actual data string:

System.out.println(“Write characters from client output stream”);

out.writeUTF(“Aayush is here”);

// read the string at the server:

System.out.println(“Server got this : “+ serverin.readUTF()+“\n”);

//—————————————————————————————//

// Now send an object over the socket wire:
// This class has to be serializable

// Inner class:
class DummyObject implements Serializable

{

private static final long serialVersionUID = 2614201039445544576L;
int i;
int j;
char c;

DummyObject()

{

this.i=1;
this.j=2;
this.c=‘c’;

}

}

// Create the object to send:

DummyObject obj = new DummyObject();

// Print its current state here:

System.out.println(“Dummy Object’s instance variable i = “+obj.i);

System.out.println(“Dummy Object’s instance variable j = “+obj.j);

System.out.println(“Dummy Object’s instance variable c = “+obj.c+“\n”);

// Get the object output stream corresponding to the client socket here:

ObjectOutputStream oos = new ObjectOutputStream(clientsocket.getOutputStream());

// Write the object and send it:

oos.writeObject(obj);

oos.flush();

// close the Object output stream:

oos.close();

// Get the object input stream corresponding to the server connection here:

ObjectInputStream ois = new ObjectInputStream(serverSock.getInputStream());

// Read the dummy object sent by the client:

DummyObject objr = (DummyObject) ois.readObject();

//close the object input stream:

ois.close();

// Print the state of the received object:

System.out.println(” Received Dummy Object’s instance variable i = “+objr.i);

System.out.println(” Received Dummy Object’s instance variable j = “+objr.j);

System.out.println(” Received Dummy Object’s instance variable c = “+objr.c+“\n”);

//—————————————————————————————//

// Clean up stuff:
// client closes its output stream writer:

out.close();

// server closes its input stream reader:

serverin.close();

// server closes its socket connection with the client:

System.out.println(

“Closing incoming connection accepted at the server….”+serverSock+“\n”);

serverSock.close();

// client closes its connection with the server:

System.out.println( “Closing socket at client side….”+clientsocket+“\n”);

clientsocket.close();

// ServerSocket is closed finally to give up port 1919:

System.out.println(“Closing server socket that was opened…”+receiver+“\n”);

receiver.close();

}

}

——————————————————————————————————-

(B) Multithreading  Callback Demo (Sept 14 2009)

A code demo for delegating heavy processing intensive tasks to background worker threads.

The worker thread does its stuff and sends an asynchronous callback to the calling thread later.

The calling thread can then take appropriate action. The callback is received in the form of a

Runnable task which is executed by a custom executor.

——————————————————————————————————-

MainLauncher.java:

package com.background.threads;
/**
*
* @author aayush.bhatnagar
*
* Console Output:
* ———————————————————–
* Welcome to the main thread of the JAVA programming language
*
* ##### Continuing with the work in the main thread now..spawned a worker thread ####
*
* #### The main thread is not blocked on the worker thread ####
*
* ### Main thread just keeps on doing its work here ###
*
* Entering the worker thread here
*
* The most processing intensive work is to have a good night’s sleep
*
* Sleeping for 5 seconds here
*
* Woken up here
*
* send a callback here to the UI Thread here
*
* ######### Callback received from the background worker thread to UI Thread #######
*
* time for the worker thread to die out
*
*/
public class MainLauncher{
final Runnable runInMainThread = new Runnable() {
public void run() {
showInMain();
}
private void showInMain() {
System.out.println(“######### Callback received from the background worker thread to UI Thread #######”);
}
};
MainLauncher()
{
new WorkerThread(runInMainThread).start();
}
public static void main(String[] args)
{
System.out.println(“Welcome to the main thread of the JAVA programming language”);
new MainLauncher();
System.out.println(“##### Continuing with the work in the main thread now..spawned a worker thread ####”);
System.out.println(“#### The main thread is not blocked on the worker thread ####”);
System.out.println(“### Main thread just keeps on doing its work here ###”);
}
}
package com.background.threads;

/**
 *
 * @author aayush.bhatnagar
 *
 * Console Output:
 * -----------------------------------------------------------
 * Welcome to the main thread of the JAVA programming language
 *
 * ##### Continuing with the work in the main thread now..spawned a worker thread ####
 *
 * #### The main thread is not blocked on the worker thread ####
 *
 * ### Main thread just keeps on doing its work here ###
 *
 * Entering the worker thread here
 *
 * The most processing intensive work is to have a good night's sleep
 *
 * Sleeping for 5 seconds here
 *
 * Woken up here
 *
 * send a callback here to the UI Thread here
 *
 * ######### Callback received from the background worker thread to UI Thread #######
 *
 * time for the worker thread to die out
 *
 */
public class MainLauncher{

	 final Runnable runInMainThread = new Runnable() {
         public void run() {

           showInMain();
         }
	   private void showInMain() {
	  System.out.println("######### Callback received from the background worker thread to UI Thread #######");
			}
       };

	MainLauncher()
	{
		new WorkerThread(runInMainThread).start();
	}

	public static void main(String[] args)
	{
		System.out.println("Welcome to the main thread of the JAVA programming language");
		new MainLauncher();
		System.out.println("##### Continuing with the work in the main thread now..spawned a worker thread ####");
		System.out.println("#### The main thread is not blocked on the worker thread ####");
		System.out.println("### Main thread just keeps on doing its work here ###");
	}

}

————————————

WorkerThread.java

————————————

package com.background.threads;

/**
 *
 * @author aayush.bhatnagar
 *
 * A background thread that performs all the heavy duty stuff.
 *
 */

public class WorkerThread extends Thread{

	private Runnable task;

	WorkerThread(Runnable task)
	{
     this.task = task;
	}

	public void run()
	{
		System.out.println("Entering the worker thread here");
		System.out.println("The most processing intensive work is to have a good night's sleep");
		System.out.println("Sleeping for 5 seconds here");
		try {
			Thread.sleep(5000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		System.out.println("Woken up here");
		System.out.println("send a callback here to the UI Thread here");
		MyExecutor.executor.execute(task);
		System.out.println("time for the worker thread to die out");
	}
}
—————————-
MyExecutor.java
—————————-
package com.background.threads;

import java.util.concurrent.Executor;

/**
 *
 * @author aayush.bhatnagar
 *
 * Utility Class for executing Runnable type tasks.
 *
 */
public class MyExecutor implements Executor {

	public static final MyExecutor executor = new MyExecutor();

	public void execute(Runnable command) {
		command.run();

	}
}

——————————————————————————————————-

(C) Java Multithreading and Fine grained locking Demo (Sept 24 2009)

Protecting the shared resource using ReentrentLocks and more usage of the
java.util.concurrent package in this code snippet. Moreover, the singleton design
pattern is also demonstrated. Look at the comments for more details.

——————————————————————————————————-

--------------------------
ThreadSafeLockingDemo.java
--------------------------
package com.lock.demo;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * 
 * @author aayush.bhatnagar
 * 
 * This demo demonstrates two APIs:
 * 
 * 1. The JAVA concurrent processing APIs in the java.util.concurrent package.
 * 2. One aspect of JAVA locking schemes to provide thread safety.
 *
 */
public class ThreadSafeLockingDemo {

	public static void main(String[] args) throws InterruptedException
	{	
		    // Demo is starting
		    System.out.println("############## DEMO IS STARTING ##############");
		    
		    // Get the Executor Service:
		    ExecutorService[] executor =  new ExecutorService[2];
            
		    // Create 2 thread pools of size 20 each:
			executor[0] = Executors.newFixedThreadPool(20);
			executor[1] = Executors.newFixedThreadPool(20);
	
			// Submit worker thread tasks to the pool:
			for (int i=0;i<=100;i++)
			{
			executor[0].submit(new WorkerThread());
			executor[1].submit(new WorkerThread());
			}

			//Shutdown the Executor pool services:
            executor[0].shutdown();
            executor[1].shutdown();
            
            // Await termination of all the active worker threads, max waiting time being 60 seconds:
			executor[0].awaitTermination(60L, TimeUnit.SECONDS);
			executor[1].awaitTermination(60L, TimeUnit.SECONDS);

			// Demo has ended
			System.out.println("############## DEMO IS OVER ##############");
	}
}

------------------
WorkerThread.java
------------------
package com.lock.demo;

/**
 * 
 * @author aayush.bhatnagar
 * 
 * This is our worker thread that is submitted to the
 * thread pool. It accesses the singleton instance of 
 * the shared resource and does a counter modification
 *
 */
public class WorkerThread extends Thread{
	

	public void run()
	{              
					try {
						SharedResource.SINGLETON.modifyCounter();
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
	}

	}

---------------------
SharedResource.java
---------------------
package com.lock.demo;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * 
 * @author aayush.bhatnagar
 * 
 * This is a singleton class representing our
 * dummy shared resource. The only shared resource
 * here is a counter. 
 * 
 * This counter is initialized to
 * zero, gets incremented to 100 and then gets decremented
 * to zero again, all operations take place in order.
 * 
 * The ReadWriteLocks take care of concurrency problems that
 * may arise due to so many threads modifying a shared resource.
 * 
 * The Read lock is taken in the getCounter() method and a write lock
 * is taken in the modifyCounter() method.
 * 
 * The code also demonstrates Renetrency, where a thread with a write
 * lock acquires a read lock. 
 *
 */
public class SharedResource {
	
	    private int counter;
	    private boolean flag;
	    public static final SharedResource SINGLETON = new SharedResource();
	
	    private final ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock();
	    private final Lock read = this.readWriteLock.readLock();
	    private final Lock write = this.readWriteLock.writeLock();
	
	// Singleton's private constructor
	private SharedResource()
	{
	 this.counter = 0;
	}

	/**
	 * Modify the counter value
	 * @throws InterruptedException
	 */
	public void modifyCounter() throws InterruptedException
	{
		// Take write lock here:
		System.out.println("##### Taking Write Lock #####");
			write.lock();
			
					if(flag)
					{
						if(getCounter()>0)
						{
						System.out.println("Counter at the start of the thread is ---> "+getCounter());
						counter--;
						System.out.println("RELEASE DECREMENT WRITE LOCK");
						System.out.println("COUNTER at the end of thread IS ---> "+getCounter());
						}
						write.unlock();
					}
							else
							    {
										if(getCounter()==100)
										{
											flag=true;
											System.out.println("#### Time to start the counter decrementing stuff ####");
											System.out.println("Counter at the start of the thread is ---> "+getCounter());
											counter--;
											System.out.println("COUNTER at the end of thread IS ---> "+getCounter());
											write.unlock();
											return;
										}
										    System.out.println("Counter at the start of the thread is ---> "+getCounter());
											counter++;
											System.out.println("RELEASE INCREMENT WRITE LOCK");
											System.out.println("COUNTER at the end of thread IS ---> "+getCounter());
								            write.unlock();
							    }   
	}
	
	/**
	 * 
	 * @return the counter value
	 */
	public int getCounter()
	{
		read.lock();
			try{
				return this.counter;
			}finally{
				read.unlock();
			}
	}
	
}

--------------
OUTPUT
--------------
############## DEMO IS STARTING ##############
##### Taking Write Lock #####
##### Taking Write Lock #####
##### Taking Write Lock #####
Counter at the start of the thread is ---> 0
RELEASE INCREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 1
Counter at the start of the thread is ---> 1
RELEASE INCREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 2
Counter at the start of the thread is ---> 2
##### Taking Write Lock #####
RELEASE INCREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 3
Counter at the start of the thread is ---> 3
##### Taking Write Lock #####
RELEASE INCREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 4
Counter at the start of the thread is ---> 4
##### Taking Write Lock #####
RELEASE INCREMENT WRITE LOCK
##### Taking Write Lock #####
##### Taking Write Lock #####
##### Taking Write Lock #####
##### Taking Write Lock #####
COUNTER at the end of thread IS ---> 5
##### Taking Write Lock #####
##### Taking Write Lock #####
##### Taking Write Lock #####
##### Taking Write Lock #####
##### Taking Write Lock #####
##### Taking Write Lock #####
##### Taking Write Lock #####
Counter at the start of the thread is ---> 5
##### Taking Write Lock #####
##### Taking Write Lock #####
##### Taking Write Lock #####
RELEASE INCREMENT WRITE LOCK
##### Taking Write Lock #####
##### Taking Write Lock #####
##### Taking Write Lock #####
##### Taking Write Lock #####
##### Taking Write Lock #####
##### Taking Write Lock #####
COUNTER at the end of thread IS ---> 6
##### Taking Write Lock #####
##### Taking Write Lock #####
##### Taking Write Lock #####
##### Taking Write Lock #####
##### Taking Write Lock #####
##### Taking Write Lock #####
##### Taking Write Lock #####
##### Taking Write Lock #####
##### Taking Write Lock #####
##### Taking Write Lock #####
##### Taking Write Lock #####
##### Taking Write Lock #####
##### Taking Write Lock #####
##### Taking Write Lock #####
##### Taking Write Lock #####
##### Taking Write Lock #####
##### Taking Write Lock #####
##### Taking Write Lock #####
Counter at the start of the thread is ---> 6
RELEASE INCREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 7
##### Taking Write Lock #####
Counter at the start of the thread is ---> 7
RELEASE INCREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 8
##### Taking Write Lock #####
Counter at the start of the thread is ---> 8
RELEASE INCREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 9
##### Taking Write Lock #####
Counter at the start of the thread is ---> 9
RELEASE INCREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 10
##### Taking Write Lock #####
Counter at the start of the thread is ---> 10
RELEASE INCREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 11
##### Taking Write Lock #####
Counter at the start of the thread is ---> 11
RELEASE INCREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 12
##### Taking Write Lock #####
Counter at the start of the thread is ---> 12
RELEASE INCREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 13
##### Taking Write Lock #####
Counter at the start of the thread is ---> 13
RELEASE INCREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 14
Counter at the start of the thread is ---> 14
RELEASE INCREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 15
##### Taking Write Lock #####
Counter at the start of the thread is ---> 15
RELEASE INCREMENT WRITE LOCK
##### Taking Write Lock #####
COUNTER at the end of thread IS ---> 16
##### Taking Write Lock #####
Counter at the start of the thread is ---> 16
RELEASE INCREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 17
##### Taking Write Lock #####
Counter at the start of the thread is ---> 17
RELEASE INCREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 18
##### Taking Write Lock #####
Counter at the start of the thread is ---> 18
RELEASE INCREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 19
##### Taking Write Lock #####
Counter at the start of the thread is ---> 19
RELEASE INCREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 20
##### Taking Write Lock #####
Counter at the start of the thread is ---> 20
RELEASE INCREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 21
##### Taking Write Lock #####
Counter at the start of the thread is ---> 21
RELEASE INCREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 22
##### Taking Write Lock #####
Counter at the start of the thread is ---> 22
RELEASE INCREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 23
##### Taking Write Lock #####
Counter at the start of the thread is ---> 23
RELEASE INCREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 24
Counter at the start of the thread is ---> 24
##### Taking Write Lock #####
RELEASE INCREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 25
##### Taking Write Lock #####
Counter at the start of the thread is ---> 25
RELEASE INCREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 26
##### Taking Write Lock #####
Counter at the start of the thread is ---> 26
RELEASE INCREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 27
##### Taking Write Lock #####
Counter at the start of the thread is ---> 27
RELEASE INCREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 28
##### Taking Write Lock #####
Counter at the start of the thread is ---> 28
RELEASE INCREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 29
##### Taking Write Lock #####
Counter at the start of the thread is ---> 29
RELEASE INCREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 30
Counter at the start of the thread is ---> 30
RELEASE INCREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 31
##### Taking Write Lock #####
Counter at the start of the thread is ---> 31
##### Taking Write Lock #####
RELEASE INCREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 32
##### Taking Write Lock #####
Counter at the start of the thread is ---> 32
RELEASE INCREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 33
##### Taking Write Lock #####
Counter at the start of the thread is ---> 33
RELEASE INCREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 34
Counter at the start of the thread is ---> 34
RELEASE INCREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 35
##### Taking Write Lock #####
##### Taking Write Lock #####
Counter at the start of the thread is ---> 35
RELEASE INCREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 36
##### Taking Write Lock #####
Counter at the start of the thread is ---> 36
RELEASE INCREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 37
##### Taking Write Lock #####
Counter at the start of the thread is ---> 37
RELEASE INCREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 38
Counter at the start of the thread is ---> 38
RELEASE INCREMENT WRITE LOCK
##### Taking Write Lock #####
COUNTER at the end of thread IS ---> 39
Counter at the start of the thread is ---> 39
RELEASE INCREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 40
##### Taking Write Lock #####
Counter at the start of the thread is ---> 40
RELEASE INCREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 41
##### Taking Write Lock #####
Counter at the start of the thread is ---> 41
RELEASE INCREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 42
##### Taking Write Lock #####
Counter at the start of the thread is ---> 42
RELEASE INCREMENT WRITE LOCK
##### Taking Write Lock #####
COUNTER at the end of thread IS ---> 43
##### Taking Write Lock #####
Counter at the start of the thread is ---> 43
RELEASE INCREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 44
##### Taking Write Lock #####
Counter at the start of the thread is ---> 44
RELEASE INCREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 45
##### Taking Write Lock #####
Counter at the start of the thread is ---> 45
RELEASE INCREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 46
##### Taking Write Lock #####
Counter at the start of the thread is ---> 46
RELEASE INCREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 47
##### Taking Write Lock #####
Counter at the start of the thread is ---> 47
RELEASE INCREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 48
##### Taking Write Lock #####
Counter at the start of the thread is ---> 48
RELEASE INCREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 49
##### Taking Write Lock #####
Counter at the start of the thread is ---> 49
RELEASE INCREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 50
##### Taking Write Lock #####
Counter at the start of the thread is ---> 50
RELEASE INCREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 51
##### Taking Write Lock #####
Counter at the start of the thread is ---> 51
RELEASE INCREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 52
##### Taking Write Lock #####
Counter at the start of the thread is ---> 52
RELEASE INCREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 53
##### Taking Write Lock #####
Counter at the start of the thread is ---> 53
RELEASE INCREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 54
##### Taking Write Lock #####
Counter at the start of the thread is ---> 54
RELEASE INCREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 55
##### Taking Write Lock #####
Counter at the start of the thread is ---> 55
RELEASE INCREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 56
##### Taking Write Lock #####
Counter at the start of the thread is ---> 56
RELEASE INCREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 57
##### Taking Write Lock #####
Counter at the start of the thread is ---> 57
RELEASE INCREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 58
##### Taking Write Lock #####
Counter at the start of the thread is ---> 58
RELEASE INCREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 59
##### Taking Write Lock #####
Counter at the start of the thread is ---> 59
RELEASE INCREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 60
##### Taking Write Lock #####
Counter at the start of the thread is ---> 60
RELEASE INCREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 61
##### Taking Write Lock #####
Counter at the start of the thread is ---> 61
RELEASE INCREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 62
##### Taking Write Lock #####
Counter at the start of the thread is ---> 62
RELEASE INCREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 63
##### Taking Write Lock #####
Counter at the start of the thread is ---> 63
RELEASE INCREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 64
##### Taking Write Lock #####
Counter at the start of the thread is ---> 64
RELEASE INCREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 65
##### Taking Write Lock #####
Counter at the start of the thread is ---> 65
RELEASE INCREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 66
Counter at the start of the thread is ---> 66
RELEASE INCREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 67
##### Taking Write Lock #####
Counter at the start of the thread is ---> 67
RELEASE INCREMENT WRITE LOCK
##### Taking Write Lock #####
COUNTER at the end of thread IS ---> 68
##### Taking Write Lock #####
Counter at the start of the thread is ---> 68
RELEASE INCREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 69
##### Taking Write Lock #####
Counter at the start of the thread is ---> 69
RELEASE INCREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 70
##### Taking Write Lock #####
Counter at the start of the thread is ---> 70
RELEASE INCREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 71
##### Taking Write Lock #####
Counter at the start of the thread is ---> 71
RELEASE INCREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 72
##### Taking Write Lock #####
Counter at the start of the thread is ---> 72
RELEASE INCREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 73
##### Taking Write Lock #####
Counter at the start of the thread is ---> 73
RELEASE INCREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 74
##### Taking Write Lock #####
Counter at the start of the thread is ---> 74
RELEASE INCREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 75
##### Taking Write Lock #####
Counter at the start of the thread is ---> 75
RELEASE INCREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 76
##### Taking Write Lock #####
Counter at the start of the thread is ---> 76
RELEASE INCREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 77
##### Taking Write Lock #####
Counter at the start of the thread is ---> 77
RELEASE INCREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 78
##### Taking Write Lock #####
Counter at the start of the thread is ---> 78
RELEASE INCREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 79
##### Taking Write Lock #####
Counter at the start of the thread is ---> 79
RELEASE INCREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 80
##### Taking Write Lock #####
Counter at the start of the thread is ---> 80
RELEASE INCREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 81
##### Taking Write Lock #####
Counter at the start of the thread is ---> 81
RELEASE INCREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 82
##### Taking Write Lock #####
Counter at the start of the thread is ---> 82
RELEASE INCREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 83
##### Taking Write Lock #####
Counter at the start of the thread is ---> 83
RELEASE INCREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 84
##### Taking Write Lock #####
Counter at the start of the thread is ---> 84
RELEASE INCREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 85
##### Taking Write Lock #####
Counter at the start of the thread is ---> 85
RELEASE INCREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 86
Counter at the start of the thread is ---> 86
##### Taking Write Lock #####
RELEASE INCREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 87
##### Taking Write Lock #####
Counter at the start of the thread is ---> 87
RELEASE INCREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 88
##### Taking Write Lock #####
Counter at the start of the thread is ---> 88
RELEASE INCREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 89
##### Taking Write Lock #####
Counter at the start of the thread is ---> 89
RELEASE INCREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 90
##### Taking Write Lock #####
Counter at the start of the thread is ---> 90
RELEASE INCREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 91
##### Taking Write Lock #####
Counter at the start of the thread is ---> 91
RELEASE INCREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 92
##### Taking Write Lock #####
Counter at the start of the thread is ---> 92
RELEASE INCREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 93
##### Taking Write Lock #####
Counter at the start of the thread is ---> 93
RELEASE INCREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 94
Counter at the start of the thread is ---> 94
RELEASE INCREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 95
##### Taking Write Lock #####
##### Taking Write Lock #####
Counter at the start of the thread is ---> 95
RELEASE INCREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 96
##### Taking Write Lock #####
Counter at the start of the thread is ---> 96
RELEASE INCREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 97
##### Taking Write Lock #####
Counter at the start of the thread is ---> 97
RELEASE INCREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 98
##### Taking Write Lock #####
Counter at the start of the thread is ---> 98
RELEASE INCREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 99
##### Taking Write Lock #####
Counter at the start of the thread is ---> 99
RELEASE INCREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 100
##### Taking Write Lock #####
#### Time to start the counter decrementing stuff ####
Counter at the start of the thread is ---> 100
COUNTER at the end of thread IS ---> 99
##### Taking Write Lock #####
Counter at the start of the thread is ---> 99
RELEASE DECREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 98
##### Taking Write Lock #####
Counter at the start of the thread is ---> 98
RELEASE DECREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 97
##### Taking Write Lock #####
Counter at the start of the thread is ---> 97
RELEASE DECREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 96
##### Taking Write Lock #####
Counter at the start of the thread is ---> 96
RELEASE DECREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 95
##### Taking Write Lock #####
Counter at the start of the thread is ---> 95
RELEASE DECREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 94
##### Taking Write Lock #####
Counter at the start of the thread is ---> 94
RELEASE DECREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 93
##### Taking Write Lock #####
Counter at the start of the thread is ---> 93
RELEASE DECREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 92
##### Taking Write Lock #####
Counter at the start of the thread is ---> 92
RELEASE DECREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 91
##### Taking Write Lock #####
Counter at the start of the thread is ---> 91
RELEASE DECREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 90
##### Taking Write Lock #####
Counter at the start of the thread is ---> 90
RELEASE DECREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 89
##### Taking Write Lock #####
Counter at the start of the thread is ---> 89
RELEASE DECREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 88
##### Taking Write Lock #####
Counter at the start of the thread is ---> 88
RELEASE DECREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 87
##### Taking Write Lock #####
Counter at the start of the thread is ---> 87
RELEASE DECREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 86
##### Taking Write Lock #####
Counter at the start of the thread is ---> 86
RELEASE DECREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 85
##### Taking Write Lock #####
Counter at the start of the thread is ---> 85
RELEASE DECREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 84
##### Taking Write Lock #####
Counter at the start of the thread is ---> 84
RELEASE DECREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 83
##### Taking Write Lock #####
Counter at the start of the thread is ---> 83
RELEASE DECREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 82
##### Taking Write Lock #####
Counter at the start of the thread is ---> 82
RELEASE DECREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 81
##### Taking Write Lock #####
Counter at the start of the thread is ---> 81
RELEASE DECREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 80
##### Taking Write Lock #####
Counter at the start of the thread is ---> 80
RELEASE DECREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 79
##### Taking Write Lock #####
Counter at the start of the thread is ---> 79
RELEASE DECREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 78
##### Taking Write Lock #####
Counter at the start of the thread is ---> 78
RELEASE DECREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 77
##### Taking Write Lock #####
Counter at the start of the thread is ---> 77
RELEASE DECREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 76
##### Taking Write Lock #####
Counter at the start of the thread is ---> 76
RELEASE DECREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 75
##### Taking Write Lock #####
Counter at the start of the thread is ---> 75
RELEASE DECREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 74
##### Taking Write Lock #####
Counter at the start of the thread is ---> 74
RELEASE DECREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 73
##### Taking Write Lock #####
Counter at the start of the thread is ---> 73
RELEASE DECREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 72
##### Taking Write Lock #####
Counter at the start of the thread is ---> 72
RELEASE DECREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 71
##### Taking Write Lock #####
Counter at the start of the thread is ---> 71
RELEASE DECREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 70
##### Taking Write Lock #####
Counter at the start of the thread is ---> 70
RELEASE DECREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 69
##### Taking Write Lock #####
Counter at the start of the thread is ---> 69
RELEASE DECREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 68
##### Taking Write Lock #####
Counter at the start of the thread is ---> 68
RELEASE DECREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 67
##### Taking Write Lock #####
Counter at the start of the thread is ---> 67
RELEASE DECREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 66
##### Taking Write Lock #####
Counter at the start of the thread is ---> 66
RELEASE DECREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 65
##### Taking Write Lock #####
Counter at the start of the thread is ---> 65
RELEASE DECREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 64
##### Taking Write Lock #####
Counter at the start of the thread is ---> 64
RELEASE DECREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 63
##### Taking Write Lock #####
Counter at the start of the thread is ---> 63
RELEASE DECREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 62
##### Taking Write Lock #####
Counter at the start of the thread is ---> 62
RELEASE DECREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 61
##### Taking Write Lock #####
Counter at the start of the thread is ---> 61
RELEASE DECREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 60
##### Taking Write Lock #####
Counter at the start of the thread is ---> 60
RELEASE DECREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 59
##### Taking Write Lock #####
Counter at the start of the thread is ---> 59
RELEASE DECREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 58
##### Taking Write Lock #####
Counter at the start of the thread is ---> 58
RELEASE DECREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 57
##### Taking Write Lock #####
Counter at the start of the thread is ---> 57
RELEASE DECREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 56
##### Taking Write Lock #####
Counter at the start of the thread is ---> 56
RELEASE DECREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 55
Counter at the start of the thread is ---> 55
RELEASE DECREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 54
##### Taking Write Lock #####
Counter at the start of the thread is ---> 54
##### Taking Write Lock #####
RELEASE DECREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 53
##### Taking Write Lock #####
Counter at the start of the thread is ---> 53
RELEASE DECREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 52
##### Taking Write Lock #####
Counter at the start of the thread is ---> 52
RELEASE DECREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 51
##### Taking Write Lock #####
Counter at the start of the thread is ---> 51
RELEASE DECREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 50
##### Taking Write Lock #####
Counter at the start of the thread is ---> 50
RELEASE DECREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 49
##### Taking Write Lock #####
Counter at the start of the thread is ---> 49
RELEASE DECREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 48
##### Taking Write Lock #####
Counter at the start of the thread is ---> 48
RELEASE DECREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 47
##### Taking Write Lock #####
Counter at the start of the thread is ---> 47
RELEASE DECREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 46
Counter at the start of the thread is ---> 46
RELEASE DECREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 45
##### Taking Write Lock #####
Counter at the start of the thread is ---> 45
RELEASE DECREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 44
Counter at the start of the thread is ---> 44
RELEASE DECREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 43
Counter at the start of the thread is ---> 43
RELEASE DECREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 42
Counter at the start of the thread is ---> 42
RELEASE DECREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 41
##### Taking Write Lock #####
Counter at the start of the thread is ---> 41
RELEASE DECREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 40
##### Taking Write Lock #####
Counter at the start of the thread is ---> 40
RELEASE DECREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 39
Counter at the start of the thread is ---> 39
RELEASE DECREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 38
Counter at the start of the thread is ---> 38
RELEASE DECREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 37
Counter at the start of the thread is ---> 37
RELEASE DECREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 36
Counter at the start of the thread is ---> 36
RELEASE DECREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 35
Counter at the start of the thread is ---> 35
RELEASE DECREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 34
##### Taking Write Lock #####
Counter at the start of the thread is ---> 34
RELEASE DECREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 33
Counter at the start of the thread is ---> 33
RELEASE DECREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 32
Counter at the start of the thread is ---> 32
RELEASE DECREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 31
Counter at the start of the thread is ---> 31
RELEASE DECREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 30
##### Taking Write Lock #####
Counter at the start of the thread is ---> 30
RELEASE DECREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 29
##### Taking Write Lock #####
Counter at the start of the thread is ---> 29
RELEASE DECREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 28
##### Taking Write Lock #####
Counter at the start of the thread is ---> 28
RELEASE DECREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 27
##### Taking Write Lock #####
Counter at the start of the thread is ---> 27
RELEASE DECREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 26
##### Taking Write Lock #####
Counter at the start of the thread is ---> 26
RELEASE DECREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 25
Counter at the start of the thread is ---> 25
RELEASE DECREMENT WRITE LOCK
##### Taking Write Lock #####
COUNTER at the end of thread IS ---> 24
Counter at the start of the thread is ---> 24
##### Taking Write Lock #####
RELEASE DECREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 23
Counter at the start of the thread is ---> 23
RELEASE DECREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 22
Counter at the start of the thread is ---> 22
RELEASE DECREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 21
Counter at the start of the thread is ---> 21
RELEASE DECREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 20
Counter at the start of the thread is ---> 20
RELEASE DECREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 19
Counter at the start of the thread is ---> 19
RELEASE DECREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 18
Counter at the start of the thread is ---> 18
RELEASE DECREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 17
Counter at the start of the thread is ---> 17
RELEASE DECREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 16
Counter at the start of the thread is ---> 16
RELEASE DECREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 15
Counter at the start of the thread is ---> 15
RELEASE DECREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 14
Counter at the start of the thread is ---> 14
RELEASE DECREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 13
Counter at the start of the thread is ---> 13
RELEASE DECREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 12
Counter at the start of the thread is ---> 12
RELEASE DECREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 11
Counter at the start of the thread is ---> 11
RELEASE DECREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 10
Counter at the start of the thread is ---> 10
RELEASE DECREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 9
Counter at the start of the thread is ---> 9
RELEASE DECREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 8
Counter at the start of the thread is ---> 8
RELEASE DECREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 7
Counter at the start of the thread is ---> 7
RELEASE DECREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 6
Counter at the start of the thread is ---> 6
RELEASE DECREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 5
Counter at the start of the thread is ---> 5
RELEASE DECREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 4
Counter at the start of the thread is ---> 4
RELEASE DECREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 3
Counter at the start of the thread is ---> 3
RELEASE DECREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 2
Counter at the start of the thread is ---> 2
RELEASE DECREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 1
Counter at the start of the thread is ---> 1
RELEASE DECREMENT WRITE LOCK
COUNTER at the end of thread IS ---> 0
############## DEMO IS OVER ##############

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: