Jun 30, 2015

What is difference between Semaphore and Mutex in Java - Producer consumer problem with semaphore and mutex

Semaphore :- It is a variable used for controlling access to a common resource by multiple processes/threads in a concurrent system. In other words, semaphore is used to manage how many units of resources are available in multi-process or multi-threaded environment.
Possible value of a semaphore can vary from 0 to N, where is N is maximum units of shared resources allowed in the given system.
There are two type of semaphore - 
1. Counting semaphore (Values varies from 0 to N)
2. Binary semaphore (Values either 0  or  1)
Note:- Java's semaphore is a counting semaphor.
 
Mutex
:- It is basically mutual exclusion. In other words, only one thread is allowed to acquire the resource at a time. When one thread acquires the resource, no other thread is allowed to acquire the resource until the thread owning the resource releases. All threads waiting for acquiring resource would be blocked.
At high level, binary semaphore is similar to Mutex. Binary semaphore can act as Mutex. However, there is fundamental difference between Mutex and Semaphore :-  the concept of "ownership".
Semaphores have no notion of ownership, this means that any thread can release a semaphore, Whereas a mutex does have the concept of ownership The process that locked the mutex is supposed to unlock it.

Mutex primary use is to guard shared resources in multi-threaded environment.

In summary, semaphore provides singling mechanism and mutex provides locking mechanism(to synchronize access to a resource). 

When do we use semaphore and Mutex :- The usefulness of Mutex and Semaphore lies in - how many threads are allowed to acquire the resource at once ?

If answer is one - Mutex is suitable candidate.
Otherwise Semaphore as it allows multiple threads(equal to number of permitted semaphore values) to access shared resources.


Below sample code lines presents solution of producer-consumer problem using semaphore and Mutex(binary semaphore).


package com.dev.thread;

import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.Semaphore;

public class ProducerConsumerUsingSemaphore1 {

 static List<Integer> list = new LinkedList<Integer>(); // shared resource.
 static Semaphore semaphore = new Semaphore(0);
 static Semaphore mutex = new Semaphore(1);// Binary semaphore
 static int value;
 static int N = 0;

 public static void main(String[] args) {
  // Multiple producer and multiple consumer
  Thread tp = new Thread(new Producer("producer-0"));
  Thread tp1 = new Thread(new Producer("producer-1"));
  Thread tc = new Thread(new Consumer("consumer-0"));
  Thread tc1 = new Thread(new Consumer("consumer-1"));
  tp.start();
  tc.start();
  tc1.start();
  tp1.start();
 }

 static class Producer implements Runnable {
  String name;

  public Producer(String name) {
   this.name = name;
  }

  public void run() {
   try {

    while (true) {
     mutex.acquire(); //critical section start 
     System.out.println("Producer \"" + name + "\" Write: " + N);
     list.add(new Integer(N++));
     mutex.release();
     semaphore.release(1);
     Thread.sleep(400);
    }
   } catch (Exception x) {
    x.printStackTrace();
   }
  }

 }

 static class Consumer implements Runnable {
  String name;

  public Consumer(String name) {
   this.name = name;
  }

  public void run() {
   try {
    while (true) {
     semaphore.acquire(1);// Acquires a permit from semaphore
     mutex.acquire(); // Begin critical section guarded by mutex
     System.out.println("Consumer \"" + name + "\" read: "
       + list.remove(0));
     mutex.release();
    }
   } catch (Exception x) {
    x.printStackTrace();
   }

  }

 }
}

In producer class run method, using mutex only one producer thread to enter into critical section and update List. Once acquire is called on a given binary semaphore, it blocks other thread and allows one thread to update List. Finally, semaphore is released for which consumer might be waiting.
Similarly, in consumer class run method, semaphore permit is acquired by a thread and value read/removed from list.

Sample output:-
Producer "producer-0" Write: 0
Consumer "consumer-0" read: 0
Producer "producer-1" Write: 1
Consumer "consumer-1" read: 1
Producer "producer-0" Write: 2
Consumer "consumer-0" read: 2
Producer "producer-1" Write: 3
Consumer "consumer-1" read: 3
Producer "producer-0" Write: 4
.......

Question
:- What are other ways to solve producer consumer problem ?
1. Using synchronized keyword and wait()/notify() methods
2. Using concurrent queue interface BlockingQueue.
3. Using java 5 Lock interface and Condition variable.

Jun 21, 2015

Java I/O - CharArrayReader and CharArrayWriter

Topics covered
1. CharArrayReader
2. CharArrayWriter
In Java FileInputStream, FileReader, FileOutputStream and FileWriter are responsible for supporting byte and character read/write operation in File.ByteArrayInputStream and ByteArrayOutputStream classes deals with byte array and act as an interface for byte array and input/output stream.

CharArrayReder and CharArrayWriter

CharArrayReader(CAR) and CharArrayWriter(CAW) provide I/O with an array of chars. CAR extends Reader and CAW extends Writer. Lets first see the internals of CAR followed by CAW and sample example for the same.
CharArrayReader(CAR) internally maintains a char buffer(char buf[]) and store array of chars that is provided to BIS constructor. Along with this buffer it also maintains two integer (pos and count) , pos keeps track of index of the next character to be read from the input char buffer and count is one greater than the position of the last char within buf. If any instance (pos == count) is true, it indicates end of array has been reached.The constructors of BIS are
//Creates a CharArrayReader from the specified array of chars - buf[].
public CharArrayReader(char buf[], int offset, int length) {
 //offset - index from where first char to read 
 //count - minimum of (offset + length, buf.length)
}
public CharArrayReader(char buf[]) {
 
}
Below sample program displays characters from charArrayReader and FileReader - same method deal with both type of input.
package com.devinline.io;
import java.io.BufferedReader;
import java.io.CharArrayReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;

public class CharArrayReaderExample {

 public static void main(String[] args) throws IOException{
  int chInt = 97;
  char[] chArray = new char[10];
  for (int i = 0; i < 10; i++) {
   chArray[i] = (char) chInt++;
  }
  Reader rd = new CharArrayReader(chArray);
  //Pass charArray
  System.out.println("Chars array output");
  handleReader(rd);
  //Pass File
  FileReader fr = new FileReader(new File("email.properties"));
  System.out.println("\nFile reading and displays characters");
  handleReader(fr);
  
 }
 
 private static void handleReader(Reader rd){
  BufferedReader br = new BufferedReader(rd);
  int readChar;
  try {
   while((readChar = br.read())!=-1){
    System.out.println((char)readChar);
   }
  } catch (IOException e) {
   e.printStackTrace();
  }
 }

}
==========Sample output============
Char array output
a
b
c
d
e
f
g
h
i
j

File reading and displays characters
I
D
=
N
I
K
================================
CharArrayWriter(CAW) internally maintains a buffer(char buf[]) in which chars are written. The default size of this char buffer is 32 and it automatically grows as data is written to it. Initial size of buffer is assigned 32 by constructor if no arg is passed.The constructor definition is :
public CharArrayWriter() {
 //Creates CharArrayWriter of default size 32
} 
public CharArrayWriter(int initialSize) {
 //Exception is thrown if initialSize is < 0  
 buf = new char[initialSize];
}
Below is the sample program illustrating how CAW is used to write characters into chars buffer.
package com.devinline.io;

import java.io.BufferedReader;
import java.io.CharArrayWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import java.io.Reader;
import java.io.Writer;

public class CharArrayWriterExample {
 public static void main(String[] args) throws IOException{
  // created a char[] buffer of default size 32
  Writer wr = new CharArrayWriter();
  Reader ir = new FileReader(new File("email.properties"));
  BufferedReader br = new BufferedReader(ir);
  int readCount = 0;
  // Write is into ByteOutputStream
  while ((readCount = br.read()) != -1) {
   wr.write(readCount);
  }
  br.close();
  wr.close();

  // Now verify data present in ByteOutputStream using
  // toString()/toArray() method
  System.out.println("CharArrayWriter buffer data is :  "
    +wr.toString());
 }

}
=========Sample output=========
CharArrayWriter buffer data is :  ID = NIK
============================
Note:-
  1. Closing CharArrayReader and CharArrayWriter has no effect. The methods of respective classes can be called even after the stream has been closed without throwing any IOException(assuming contents of buffer might still be required, buffer is not released ).
    For example:- In above sample code, after closing Writer(wr.close()), we call wr.toString() and display data present in char buffer.

Jun 14, 2015

Textual description of firstImageUrl

Difference between View and Synonym in Oracle database

It is one of commonly asked interview question from database section. View and Synonym is common concept relevant for all database however, the syntax and semantics discussed in this post is oracle database specific.

View is a virtual entity based on table or other views.View is created using database tables(primary storage of data in relational database) or other views.In other words, view is just a stored SELECT statement (a logical table) based on one or multiple tables or views and view does not have its own data, it relies on underlying base tables for result sets.
Note:- Along with table based views, oracle database also support object view or a relational view that supports LOBs, object types, REF datatypes, nested table, varray. XML view can also be created supporting XML types.

Synonym is alternate name for database objects like Table, views, procedure, function , etc. Synonym provides an alias to a database object and gives an opportunity to mask original name & owner of the object.
Synonym is mostly used for providing access on table to external schema by creating Public synonym and by doing this external schema can access table without specifying schema_name where table is existing.

Syntax & semantics of View and Synonym creation:- 

View creation :- General syntax for table/view based view
CREATE [ OR REPLACE ] [ [ NO ] FORCE ] VIEW
   [ schema. ]view
   [ (alias [ inline_constraint
              [ inline_constraint ]... ]
     | out_of_line_constraint
       [, alias [ inline_constraint
                  [ inline_constraint ]... ]
       | out_of_line_constraint
       ]...
     )
   | object_view_clause
   | XMLType_view_clause
   ]
   AS subquery [ subquery_restriction_clause ] ;

For creating View in our schema we must have CREATE VIEW and for creating view in anothers schema we must have CREATE ANY VIEW system privilege.
Let's create a view based on multiple table in HR schema - Employees, Departments and Jobs
View can be created using following syntax:-
CREATE VIEW EMPLOYEE_DETAILS
AS 
SELECT emp.FIRST_NAME, emp.EMAIL,dept.DEPARTMENT_NAME, job.JOB_TITLE
FROM 
EMPLOYEES emp, DEPARTMENTS dept ,JOBS job 
WHERE 
emp.DEPARTMENT_ID = dept.DEPARTMENT_ID
AND job.JOB_ID = emp.JOB_ID;

when we execute above query it will create a view object "EMPLOYEE_DETAILS" in database and it can be queried as a table like this.
Select * from  EMPLOYEE_DETAILS;

Other clauses supported in View are - OR REPLACE, FORCE, READ ONLY,CHECK OPTION.
1.OR REPLACE :- If appended, it will recreate view and it is used to alter definition of an existing view without doing rework (dropping, applying grants again, etc).
CREATE OR REPLACE VIEW EMPLOYEE_DETAILS
AS 
SELECT 
....
FROM
......

2.FORCE:- If appended, it will forcefully creates view even if base tables or view does not exist or grants to accessing object is not there.
Note:- Before executing any select, insert,update query above structure should be in place,otherwise it will throe error.
CREATE OR REPLACE FORCE VIEW EMPLOYEE_DETAILS
AS 
SELECT 
....
FROM
......

3. READ ONLY:- View created only used for select operation. No insert ,update are allowed on read only views.
CREATE OR REPLACE FORCE VIEW EMPLOYEE_DETAILS
AS SELECT emp.FIRST_NAME, emp.EMAIL,dept.DEPARTMENT_NAME, job.JOB_TITLE
FROM 
EMPLOYEES emp, DEPARTMENTS dept ,JOBS job 
WHERE 
emp.DEPARTMENT_ID = dept.DEPARTMENT_ID
AND job.JOB_ID = emp.JOB_ID WITH READ ONLY;

4. CHECK OPTION:- If check option is enabled, insert or update can be executed but with some restriction.
CREATE OR REPLACE FORCE VIEW EMPLOYEE_DETAILS
AS SELECT emp.FIRST_NAME, emp.EMAIL,dept.DEPARTMENT_NAME, job.JOB_TITLE
FROM 
EMPLOYEES emp, DEPARTMENTS dept ,JOBS job 
WHERE 
emp.DEPARTMENT_ID = dept.DEPARTMENT_ID
AND job.JOB_ID = emp.JOB_ID WITH CHECK OPTION;
Note:- Query should not contain Group By Clause, Group Function ,Distinct Clause,References to any expression,Refrences to any pseudo column rownum. For more detail refer official guide. Read What is difference between view and materialized view ?

Synonym creation :- 
Synonym is created for an database object to hide details of object and it provides  data independence and location transparency too.
Synonym can be of two types: private (default) and public.
For creating private synonym in our own schema we need CREATE SYNONYM privilege and for creating in others schema we need CREATE ANY SYNONYM privilege.For public synonym we need CREATE PUBLIC SYNONYM privilege.
CREATE [ OR REPLACE ] [ PUBLIC ] SYNONYM
   [ schema. ]synonym 
   FOR [ schema. ]object [ @ dblink ] ;

Lets create a synonym for stored procedure and execute it. Use following syntax for creating procedure and synonym:
Create procedure get_employee_names:- 
create or replace procedure get_employee_names(v_deptId_in NUMBER, v_cur OUT SYS_REFCURSOR ) IS
begin
  open v_cur for select FIRST_NAME,LAST_NAME from employees where DEPARTMENT_ID = v_deptId_in;
end get_employee_names;
/

Create a synonym for above stored procedure:-
--GET_EMPLOYEE_NAMES(In NUMBER, OUT REF CURSOR) is a procedure
 CREATE OR REPLACE SYNONYM "HR"."MY_PROCEDURE_SYNONYM" FOR "HR"."GET_EMPLOYEE_NAMES";

Now we can execute the above stored procedure via synonym with appropriate parameter passing. Refer the following diagram and execute command in following order :-

We can see that, we have not executed original stored procedure instead we executed MY_PROCUDURE_SYNONYM. Similarly, synonym for table and other objects.

Advantage of Views and Synonyms:- 
With views: we can hide columns , add predicates (WHERE clause) to restrict rows, rename columns, give a column name to a SQL expression.
With synonym: we can reference objects in other schemas(using public synonym) and databases without qualifying the name.

======End of post======

Read also:
1. How to test stored procedure in SQL*PLUS and SQL developer
2. Internals of PL/SQL execution in database server

Jun 10, 2015

Git commands recap : Working with remote repository (add, rename and delete remote repositories )

Fork a brach from main repo and configure remote as origin(forked/cloned) & upstream(main repo)

Here I have forked from "https://github.com/MaxKHK/Udacity_DeepLearningAssignments" in my and created in my Git a forked branch.

Forked branch is:  https://github.com/zytham/Udacity_DeepLearningAssignments.git

1. Setup origin : Initialise a directory with git features (git init)
➜  devinline-git-recap git init
Initialized empty Git repository in /Users/n0r0082/devinline-git-recap/.git/
➜  devinline-git-recap git:(master) git remote add origin https://github.com/zytham/Udacity_DeepLearningAssignments.git

Note: Alternatively we could have directly cloned the forked repo using command :
  git clone <https://github.com/zytham/Udacity_DeepLearningAssignments.git>

2. List the configured remote repository for fork :
➜  devinline-git-recap git:(master) git remote -v
origin https://github.com/zytham/Udacity_DeepLearningAssignments.git (fetch)
origin https://github.com/zytham/Udacity_DeepLearningAssignments.git (push)

3. Setup upstream and Verify the new upstream repository : Remote upstream repository that will be synced with the fork.
➜  devinline-git-recap git:(master) git remote add upstream https://github.com/MaxKHK/Udacity_DeepLearningAssignments.git
➜  devinline-git-recap git:(master) git remote -v                                                                        
origin https://github.com/zytham/Udacity_DeepLearningAssignments.git (fetch)
origin https://github.com/zytham/Udacity_DeepLearningAssignments.git (push)
upstream https://github.com/MaxKHK/Udacity_DeepLearningAssignments.git (fetch)
upstream https://github.com/MaxKHK/Udacity_DeepLearningAssignments.git (push)

Note: Instead of remote name as origin and upstream we can use any name, but by convention if using just two remote repository we use origin and upstream.
origin — that is the default name Git gives to the server you cloned from.

Renaming and Removing Remote

git remote rename <OLD_NAME> <NEW_NAME>

git remote remove <REMOTE_NAME>

Rename remote
➜  devinline-git-recap git:(master) ✗ git remote -v  
origin https://github.com/zytham/Udacity_DeepLearningAssignments.git (fetch)
origin https://github.com/zytham/Udacity_DeepLearningAssignments.git (push)
upstream https://github.com/MaxKHK/Udacity_DeepLearningAssignments.git (fetch)
upstream https://github.com/MaxKHK/Udacity_DeepLearningAssignments.git (push)
➜  devinline-git-recap git:(master) ✗ git remote rename upstream upstr
➜  devinline-git-recap git:(master) ✗ git remote -v                   
origin https://github.com/zytham/Udacity_DeepLearningAssignments.git (fetch)
origin https://github.com/zytham/Udacity_DeepLearningAssignments.git (push)
upstr https://github.com/MaxKHK/Udacity_DeepLearningAssignments.git (fetch)
upstr https://github.com/MaxKHK/Udacity_DeepLearningAssignments.git (push)
Delete remote
➜  devinline-git-recap git:(master) ✗ git remote -v  
origin https://github.com/zytham/Udacity_DeepLearningAssignments.git (fetch)
origin https://github.com/zytham/Udacity_DeepLearningAssignments.git (push)
upstream https://github.com/MaxKHK/Udacity_DeepLearningAssignments.git (fetch)
upstream https://github.com/MaxKHK/Udacity_DeepLearningAssignments.git (push)
➜  devinline-git-recap git:(master) ✗ git remote remove upstr 
➜  devinline-git-recap git:(master) ✗ git remote -v
origin https://github.com/zytham/Udacity_DeepLearningAssignments.git (fetch)
origin https://github.com/zytham/Udacity_DeepLearningAssignments.git (push)



Reference
https://help.github.com/articles/configuring-a-remote-for-a-fork/

Jun 7, 2015

Java I/O - ByteArrayInputStream and ByteArrayOutputStream

In previous post we discusses about Java I/O classes classification & class hierarchy and its interdependency. Java support Input and Output(I/O) operations with characters/text and binary stream. FileInputStream,FileReader,FileOutputStream and FileWriter are responsible for supporting character and byte read/write operation in File. In continuation of previous post, here we are discussing about Java I/O classes dedicated for bytes array - ByteArrayInputStream, ByteArrayOutputStream. 

ByteArrayInputStream and ByteArrayOutputStream 

Classes ByteArrayInputStream(BIS) and ByteArrayOutputStream(BOS) provide I/O with an array of bytes. BIS extends InputStream and BOS extends OutputStream. Lets first see the internals of BIS followed by BOS and sample example for the same.
ByteArrayInputStream(BIS) internally maintains a buffer(byte buf[]) and store array of bytes that is provided by BIS constructor. Along with this buffer it also maintains two integer (pos and count) , pos kwwp track of index of the next character to read from the input stream buffer and count is one greater than the position of the last byte within buf. If any moment pos == count, it means end of file has reached.The constructors of BIS are
//BIS buf is initialized with byte buffer passed as argument, pos is set 
//to 0 and count is length of  bye bugger.
public ByteArrayInputStream(byte buf[]) {
 this.buf = buf;
 this.pos = 0;
 this.count = buf.length;
    }
//BIS buf is initialized with byte buffer passed, pos is offset and count is minimum 
//of length of  bye bugger or (offset + length). 
public ByteArrayInputStream(byte buf[], int offset, int length) {
 this.buf = buf;
 this.pos = offset;
 this.count = Math.min(offset + length, buf.length);
 this.mark = offset;
}

Lets write sample code illustrating how do we use BIS and create a generic InputStream handling method dealing with FileInputStream and BIS both.
package com.devinline.javaio;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

public class ByteArrayInputStreamExample {

 public static void main(String[] args) {
  byte[] byteArray = { 97, 98, 99, 78, 71, 100 };
  // pass byteArray input to InputStream handler
  InputStream is = new ByteArrayInputStream(byteArray);
  System.out.println("ByteStreamArray output");
  handleInputStream(is);
  is.close();
  // pass File input to InputStream handler
  InputStream is2;
  try {
   is2 = new FileInputStream("email.properties");
   System.out.println("\nFileInputStream output");
   handleInputStream(is2); is2.close();
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  }
 }
 
 //generic method handling both FileInputStream and ByteInputStream
 private static void handleInputStream(InputStream is) {
  BufferedInputStream bis = new BufferedInputStream(is);
  // read each byte
  int readCount = 0;
  try {
   while ((readCount = bis.read()) != -1) {
    System.out.println((char)readCount);
   }
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

 }

}
======Sample Output=====
ByteStreamArray output
a
b
c
N
G
d

FileInputStream output
I
D
=
N
I
K
==========================
ByteArrayOutputStream(BOS) internally maintains a buffer(byte buf[]) in which data is written. The default size of this byte buffer 32 and it automatically grows as data is written to it. Initial size of buffer is assigned 32 by constructor if no arg is passed.The constructor definition is :
public ByteArrayOutputStream(int size) {
 //byte buffer of length = size argument is created 
  buf = new byte[size];
}
public ByteArrayOutputStream() {
 //default size 32 byte buffer is created 
}
Below is sample program illustrating how BOS is used to write bytes into byte buffer from stream.
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.ByteArrayOutputStream;

public class ByteArrayOutputStreamExample {
 public static void main(String[] args) throws IOException {
  // BOS created of default size 32, no arg passed
  OutputStream os = new ByteArrayOutputStream();
  InputStream is = new FileInputStream(new File("email.properties"));
  BufferedInputStream bis = new BufferedInputStream(is);
  int readCount = 0;
  // Write is into ByteOutputStream
  while ((readCount = bis.read()) != -1) {
   os.write(readCount);
  }
  os.close();
  bis.close();
  
  // Now verify data present in ByteOutputStream using
  // toString()/toArray() method
  System.out.println("ByteOutputStream buffer data is " + os.toString());
 }
}
=======Sample output===========
ByteArrayOutputStream buffer data is : ID = NIK
=============================
Note:- 
  1. Closing a ByteArrayInputStream and ByteArrayOutputStream has no effect. The methods of respective classes can be called after the stream has been closed without  throwing an IOException. For example:- In above sample code, after closing OutputStream(os.close()), we calls os.toString() and display data present in byte buffer.
  2. ByteArrayOutputStream has writeTo(OutputStream out) - which writes the complete contents of this byte array output stream to the specified output stream and toByteArray() - creates a newly allocated byte array of same size.