IBM DB2 Tutorial : Db2 SQL Replication Steps with example . How to replicate data in db2 step by step

One of the very helpful and important feature in db2 is Replication technique. Replication technique allows you to copy data from one location to another location making the second location data identical to the first location. Data can be copied either in the local or remote machine .Replication is useful1. To consolidate data from multiple sources in a distributed environment
2. to support basic load balancing when your server is running many more read queries (SELECT) than write queries (insert / update / delete) .
3. Makes a backup of data in the local / remote which helps for disaster recovery
4. to reduce delay and bring the data closer to the user.

DB2 Universal Database (UDB), supports two types of replication.
SQL Replication can be done using Control center and Scripting language called ansclp .SQL replication capability is included in the base product. From the Control Center you can access the Replication Center, a graphical interface for the setup of replication. There is also a scripting language for replication called ansclp which allows you to create scripts to automate replication setup.

This tutorial overs that how to setup replication using the the Replication Center . You can access the Replication Center using Control Center -> Tools -> Replication Center.

What happens when we do replication.
Two programs are involved. Capture Program and Apply Program .Capture program capture the data changes in the source table to the CD Table . Changes of data in the source tables are captured in a CD Tables . When we insert new rows in the source table , new rows are captured in the CD Tables with flag "I" . When we update data in the source table , the updated rows are captured in the CD Tables with the flag "U" . SImilarly , When we delete data in the source table , the deleted rows are captured in the CD Tables with the flag "D" . Apply Program , replicate the the data changes captured in the CD table to the target tables.

Major Steps for replication :

1. Create control tables for the Capture program

2. Enable the source database for replication (i.e. to enable logretain on for archival log)

3. Register source tables

4. Create control tables for the Apply program

5. Create a subscription set and member

6. Start the operation to capture and apply

7. Testing the Replication


1. Create control tables for the Capture program
To Create table space and control tables do the following step
1. Right click on the Create Control Servers -> Select Create Capture Control Tables -> Select Custom
2. Select Capture control server (Source database) to create Capture Control Tables
Give user name and password and select Run then OK.

Now Capture control tables are created

2. Enable the source database for replication
Right click on the Source database name (Capture control server ) to enable database for replication . that means to enable archive logging .

Press ok button to set the LOGRETAIN value for the database to RECOVERY and initiate offline backup for the db and take full backup of db


3. Register a replication source

a) Before registering source tables , specify the schema and table name and table space to be used for the CD (Capture Data) tables

Right click on the Capture control server and Source database name and select Manage Source Object Profile

You can specify here
i) table schema and table name to be used as the defaults for the CD tables
ii) table space & its properties for the CD tables and naming convention for table spaces
iii) schema and name for the CD table indexes & naming convention for the the index name.


b) To register source tables
1. Select Capture control servers - > Source database -> capture schemas -> schema name.
2. Right click on the schema name and select Register tables
3. Select Retrive All to retrive all source tables and select the table you want to register. Now CD (Capture Data) table name and table space details appeared automatically based on the settings in the previous steps (i.e. 3 a)

4. Then Ok which will run the query .

4. Create control tables for apply program

To create control tables for apply program , Right click on Apply Control Server -> Create Apply Control Tables -> custom

Select the target database where the data to be replicated . Then Ok.

5. Create Subscription Sets
A subscription set defines a relationship between the source database (ORI_DB in our example) and a target database (DUP_DB in our example). A subscription-set member defines a relationship between the source table (SALES) and one or more target tables (TGSALES).

Here you have to specify , set information , source to target mapping , schedule time to replicate data.

Steps to do :
To create subscription sets , Expand Apply control server -> Right click on the newly created Apply control server ( target data base) -> Subscritption sets
Now you have to fill / select all the details like Apply control server , Set Name , Apply Qualifier , Capture control server and target server (Target database)
Check on the activate the subscription set

Now do the source to target mapping , which maps the source colums to target columns to replicate the data .

Now Set the replication schedule (Time based / event based) , Then Ok.

Now let us Add Members

Right click on the newly created set , select the member information tab and add the member using retrive all option . Use change to do column mapping and etc... Please add the column for index for target table using target table table . Then Ok.

Now the target table is created in the target server where the data to be replicated .

6. Finally Let us start the operation to capture and apply

To start Capture , Expand the operations on SQL replication under Replication center

Right click on the capture control servers and select add and give the capture control server already created (target database) , userid and password which adds capture control servers for operations

Right click on the capture control servers which is added now , then select start capture , then select capture schema , then Ok.
Now give user name and password for the database server to access

Similarly you can stop , resume or suspend capture later.

To start Apply Program
Similarly Expand the apply control servers under operations , Right click on the apply control server -> Apply qualifiers , then refresh .

Now right click on the apply qualifiers already created and the select start Apply , give Host Name or IP Address , then Ok. Now give your target server details by clicking Add New System ..


Similarly you can stop apply , if you need ..


7. Now test the replication ....
Insert any record to the source table

Now the new record is captured in the CD Table (In our example : CDSALES) with the flag (IBMSNAP_OPERATION) with following values

'I' - Insert operation tbe done on the target table.
'U' - Update operation to be done
'D' - Delete Operation to be done

Finally the changes captured in the CD table is applied in the target table by the apply program.



Note : I have created Capture control server and Apply control server on the same system.

Converting InputStream to String | Java IO tutorial InputStream to String Conversion Example | Java interview topic


Read full Article before leave thr blog.  It will really helpfull for Java Interview on IO topic 


 If you like the post please do comments 
 
Converting InputStream to String in Java has become very easy after introduction of Scanner class in Java 5 and due to development of several open source libraries like Apache commons IOUtils and Google Open source guava-libraries which provides excellent support to convert InputStream to String in Java program. we often need to convert InputStream to String  while working in Java for example if you are reading XML files from InputStream and later performing XSLT transformation on it or if InputStream is reading data from text file or text input Source and we either want to log  Strings in log file or want to operate on whole String. Before Java 5 you would have to write lots of boiler plate code to read String line by line or byte by byte depending upon whether you are using either BufferedReader or not but as I said since JDK 5 added Scanner for reading input, its fairly easy to convert InputStream into String.


Before Converting any InputStream or ByteStream into String don't forget to provide character encoding or charSet which tells Java Which characters to expect from those streams of bytes. in the absence of correct character encoding you might alter the output because same bytes can be used to represent different character in different encoding. Another thing to keep in mind is that if you don't provide character encoding, Default character encoding in Java will be used which can be specified from System property "file.encoding" or "UTF-8" if file.encoding is not specified. In this Java tutorial we will see 5 different example of converting InputStream to String in Java both by using standard JDK libraries and using open source libraries.

How to convert InputStream to String in Java – 5 Examples
here are different ways to convert InputStream to String in Java, first we will see most simple way of reading InputStream as String.

InputStream to String -  Using Java 5 Scanner
Convert InputStream to String in Java - 5 Example tutorialjava.util.Scanner has constructor which accept an InputStream, a character encoding and a delimiter to read String from InputStream. Here we have used delimiter as "\A" which is boundary match for beginning of  the input as declared in java.util.regex.Pattern and that's why Scanner is returning whole String form InputStream. I frequently use this technique to read input from user in Java using System.in which is most common example of InputStream in Java, but as demonstrated here this can also be used to read text file in Java.

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;

/**
 * Java program example to demonstrate How to convert InputStream into String by using JDK
 * Scanner utility. This program will work Java 5 onwards as Scanner was added in Java 5.
 */       
public class InputStreamTest {

    public static void main(String args[]) throws FileNotFoundException {
        FileInputStream fis = new FileInputStream("c:/sample.txt");
        String inputStreamString = new Scanner(fis,"UTF-8").useDelimiter("\\A").next();
        System.out.println(inputStreamString);
    }
}

Output:
This String is read from InputStream by changing InputStream to String in Java.

If you want to see how an incorrect character encoding completely changes the String just change the character encoding form "UTF-8" to "UTF-16" and you see Chinese(may be) characters instead of English.

Output:
?????????????!???????????!??????^(4)????????

Convert InputStream to String - Plain old JDK4 Example
If you still need to do it on plain old Java on JDK4 without including any additional dependency in your PATH and Classpath than here is quick example using BufferedReader in Java, Remember you can also do this by reading byte by byte from InputStream but that's very slow so consider using BufferedReader for better performance even if you code on JDK4 . For more performance tips see my post 4 JDBC performance tips in Java program. Let’s see example of converting InputStream to String using BufferedReader in Java.

/**
 * Java program to demonstrate How to read InputStream as String
 * using BufferedReader and StringBuilder in Java.
 * This is old and standard way of converting an InputStream into String in Java
 */
public static void main(String args[]) throws FileNotFoundException, UnsupportedEncodingException, IOException {
        FileInputStream fis = new FileInputStream("c:/sample.txt");
        StringBuilder inputStringBuilder = new StringBuilder();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fis, "UTF-8"));
        String line = bufferedReader.readLine();
        while(line != null){
            inputStringBuilder.append(line);inputStringBuilder.append('\n');
            line = bufferedReader.readLine();
        }
        System.out.println(inputStringBuilder.toString());

}
Output:
This String is read from InputStream by changing InputStream to String in Java.
second line


This is good plain old Java method of converting InputStream to String without adding any extra dependency but remember its converting \r to \n because we are reading line by line, which in most cases fine.

Read InputStream to String - Using Apache IOUtils library
As I said earlire there are many open source library in Java which makes coding lot more easier than any other language. Here is code example for How to convert InputStream to String in Java using Apache IOUtils

/**
 * Example of How to read InputStream into String by using Apache IOUtils library.
 * Nice and clean way of getting InputStream as String in Java
 */

FileInputStream fis = new FileInputStream("c:/sample.txt");
String StringFromInputStream = IOUtils.toString(fis, "UTF-8");
System.out.println(StringFromInputStream);

Isn't it a compact way of converting InputStream to String in Java, just one line of code and that does take care of character encoding as well..

InputStream to String - Using Google's guava-libraries
Google has open source its own set of Java libraries they use for Java development inside Google. it has lots of utility function and also complements Apache commons package. Here is a quick way of converting InputStream to String using Google libraries:

String stringFromStream = CharStreams.toString(new InputStreamReader(fis, "UTF-8"));

Regarding dependency, You need to include guava library i.e. guava-11.0.1.jar in your project classpath. here is full code example:

import com.google.common.io.CharStreams;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

/**
 * How to convert InputStream into String in Java using Google's Guava library
 */
public class InputStreamToString{

    public static void main(String args[]) throws UnsupportedEncodingException, IOException {
        FileInputStream fis = new FileInputStream("c:/sample.txt");
        String stringFromStream = CharStreams.toString(new InputStreamReader(fis, "UTF-8"));
        System.out.println(stringFromStream);
    }   
}

How to read InputStream into String with IOUtils.copy and StringWriter class
java.io.StringWriter is another convenient way of reading writing Strings and by using IOUtils.copy() you can copy contents form InputStream to reader, Here is a complete code example of reading InputStream as String using StringWriter:

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import org.apache.commons.io.IOUtils;

/**
  * Java Program to demonstrate reading of InputStream as String
  * using StringWriter and Apache IOUtils
  */
public class InputStreamToStringConversion{

    public static void main(String args[]) throws FileNotFoundException, UnsupportedEncodingException, IOException {
        FileInputStream fis = new FileInputStream("c:/sample.txt");
        StringWriter writer = new StringWriter();
        String encoding = "UTF-8";
        IOUtils.copy(fis, writer, encoding);
        System.out.println(writer.toString());
    }
}

That's all on converting InputStream to String in Java by using standard core java library and by using open source Apache commons IOUtils and Google's guava libraries. Don't forget Character encoding when converting bytes to String which is case here also make a convenient choice as sometime adding additional library for one functionality is not the best option. let us know if you know any other way of converting InputStream to String in Java.

Core concept of Volatile keyword in java multithreading | Java concurrency tutorial

Read full Article before leave thr blog.  It will really helpfull for Java Interview on multithreading topic

First, you have to understand a little something about the Java memory model. I've struggled a bit over the years to explain it briefly and well. As of today, the best way I can think of to describe it is if you imagine it this way:


  •     Each thread in Java takes place in a separate memory space (this is clearly untrue, so bear with me on this one).

  •     You need to use special mechanisms to guarantee that communication happens between these threads, as you would on a message passing system.

  •     Memory writes that happen in one thread can "leak through" and be seen by another thread, but this is by no means guaranteed. Without explicit communication, you can't guarantee which writes get seen by other threads, or even the order in which they get seen.

The Java volatile modifier is an example of a special mechanism to guarantee that communication happens between threads. When one thread writes to a volatile variable, and another thread sees that write, the first thread is telling the second about all of the contents of memory up until it performed the write to that volatile variable.

At this point, I usually rely on a visual aid, which we call the "two cones" diagram, but which my officemate insists on calling the "two trapezoids" diagram, because he is picky. ready is a volatile boolean variable initialized to false, and answer is a non-volatile int variable initialized to 0.




The first thread writes to ready, which is going to be the sender side of the communications. The second thread reads from ready and sees the value the first thread wrote to it. It therefore becomes a receiver. Because this communication occurs, all of the memory contents seen by Thread 1, before it wrote to ready, must be visible to Thread 2, after it reads the value true for ready.

This guarantees that Thread 2 will print "42", if it prints anything at all.

If ready were not volatile, what would happen? Well, there wouldn't be anything explicitly communicating the values known by Thread 1 to Thread 2. As I pointed out before, the value written to the (now non-volatile) ready could "leak through" to Thread 2, so Thread 2 might see ready as true. However, the value for answer might not leak through. If the value for ready does leak through, and the value for answer doesn't leak through, then this execution will print out 0.

We call the communications points "happens-before" relationships, in the language of the Java memory model.

(Minor niggle: The read of ready doesn't just ensure that Thread 2 sees the contents of memory of Thread 1 up until it wrote to ready, it also ensures that Thread 2 sees the contents of memory of any other thread that wrote to ready up until that point.)



With this in mind, let's look at the Double-Checked Locking example again. To refresh your memory, it goes like this:


class Foo {
  private volatile Helper helper = null;
  public Helper getHelper() {
    if (helper == null) {
      synchronized(this) {
        if (helper == null) {
          helper = new Helper();
        }
      }
    }
  return helper;
}

The object of the double-checked locking pattern is to avoid synchronization when reading a lazily constructed singleton that is shared between threads. If you have already constructed the object, the helper field will not be null, so you won't have to perform the synchronization.

However, this is only part of the solution. If one thread creates the object, it has to communicate the contents of its memory to another thread. Otherwise, the object will just sit in the first thread's memory. How do we communicate the contents of memory to another thread? Well, we can use volatile variables. That's why helper has to be volatile -- so that other threads see the fully constructed object.

Locking in Java also forms these "happens-before" communication points. An unlock is the sender side, and a lock on the same variable is the receiver side. The reason that doesn't work for (non-volatile) double-checked locking is that only the writing thread ever performs the locking. The whole point of the idiom is that the reader side doesn't do the locking. Without the explicit communication in the form of the volatile variable, the reading thread will never see the update performed by the writer thread.

Java concurrency tutorial : CountDownLatch Example in Java - Concurrency Tutorial | Java multithreading tutorial

Java CountDownLatch Example
CountDownLatch in Java is a kind of synchronizer which allows one Thread  to wait for one or more Threads before starts processing. This is very crucial requirement and often needed in server side core Java application and having this functionality built-in as CountDownLatch greatly simplifies the development. CountDownLatch in Java is introduced on Java 5 along with other concurrent utilities like CyclicBarrier, Semaphore, ConcurrentHashMap and BlockingQueue in java.util.concurrent package. In this Java concurrency tutorial we will  what is CountDownLatch in Java, How CountDownLatch works in Java, an example of CountDownLatch in Java and finally some worth noting points about this concurrent utility. You can also implement same functionality using  wait and notify mechanism in Java but it requires lot of code and getting it write in first attempt is tricky,  With CountDownLatch this can  be done in just few lines. CountDownLatch also allows flexibility on number of thread for which main thread should wait, It can wait for one thread or n number of thread, there is not much change on code.  Key point is that you need to figure out where to use CountDownLatch in Java application which is not difficult if you understand What CountDownLatch do and How CountDownLatch works.


How CountDownLatch works in Java
CountDownLatch Example in Java 5 6 7CountDownLatch works in latch principle,  main thread will wait until Gate is open. One thread waits for n number of threads specified while creating CountDownLatch in Java. Any thread, usually main thread of application,  which calls CountDownLatch.await() will wait until count reaches zero or its interrupted by another Thread. All other thread are required to do count down by calling CountDownLatch.countDown() once they are completed or ready to the job. as soon as count reaches zero, Thread awaiting starts running. One of the disadvantage of CountDownLatch is that its not reusable once count reaches to zero you can not use CountDownLatch any more, but don't worry Java concurrency API has another concurrent utility called CyclicBarrier for such requirements.

CountDownLatch Exmaple in Java
In this section we will see a full featured real world example of using CountDownLatch in Java. In following CountDownLatch example, Java program requires 3 services namely CacheService, AlertService  and ValidationService  to be started and ready before application can handle any request and this is achieved by using CountDownLatch in Java.

import java.util.Date;
import java.util.concurrent.CountDownLatch;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Java program to demonstrate How to use CountDownLatch in Java. CountDownLatch is
 * useful if you want to start main processing thread once its dependency is completed
 * as illustrated in this CountDownLatch Example
 *
 *
 */
public class CountDownLatchDemo {

    public static void main(String args[]) {
       final CountDownLatch latch = new CountDownLatch(3);
       Thread cacheService = new Thread(new Service("CacheService", 1000, latch));
       Thread alertService = new Thread(new Service("AlertService", 1000, latch));
       Thread validationService = new Thread(new Service("ValidationService", 1000, latch));
    
       cacheService.start(); //separate thread will initialize CacheService
       alertService.start(); //another thread for AlertService initialization
       validationService.start();
    
       // application should not start processing any thread until all service is up
       // and ready to do there job.
       // Countdown latch is idle choice here, main thread will start with count 3
       // and wait until count reaches zero. each thread once up and read will do
       // a count down. this will ensure that main thread is not started processing
       // until all services is up.
    
       //count is 3 since we have 3 Threads (Services)
    
       try{
            latch.await();  //main thread is waiting on CountDownLatch to finish
            System.out.println("All services are up, Application is starting now");
       }catch(InterruptedException ie){
           ie.printStackTrace();
       }
    
    }
 
}

/**
 * Service class which will be executed by Thread using CountDownLatch synchronizer.
 */
class Service implements Runnable{
    private final String name;
    private final int timeToStart;
    private final CountDownLatch latch;
 
    public Service(String name, int timeToStart, CountDownLatch latch){
        this.name = name;
        this.timeToStart = timeToStart;
        this.latch = latch;
    }
 
    @Override
    public void run() {
        try {
            Thread.sleep(timeToStart);
        } catch (InterruptedException ex) {
            Logger.getLogger(Service.class.getName()).log(Level.SEVERE, null, ex);
        }
        System.out.println( name + " is Up");
        latch.countDown(); //reduce count of CountDownLatch by 1
    }
 
}

Output:
ValidationService is Up
AlertService is Up
CacheService is Up
All services are up, Application is starting now

By looking at output of this CountDownLatch example in Java, you can see that Application is not started until all services started by individual Threads are completed.
When should we use CountDownLatch in Java :
Use CountDownLatch when one of Thread like main thread, require to wait for one or more thread to complete, before its start doing processing. Classical example of using CountDownLatch in Java  is any server side core Java application which uses services architecture,  where multiple services is provided by multiple threads and application can not start processing  until all services have started successfully as shown in our CountDownLatch example.


CountDownLatch in Java – Things to remember
Few points about Java CountDownLatch which is worth remembering:

1) You can not reuse CountDownLatch once count is reaches to zero, this is the main difference between CountDownLatch and CyclicBarrier, which is frequently asked in core Java interviews and multi-threading  interviews.

2) Main Thread wait on Latch by calling CountDownLatch.await() method while other thread calls CountDownLatch.countDown() to inform that they have completed.

That’s all on CountDownLatch example in Java. This is a very useful concurrency utility and if you master when to use CountDownLatch and how to use CountDownLatch you will be able to reduce good amount of complex concurrency control code written using wait and notify in Java.

Java Concurrency Tutorial : CyclicBarrier Example in Java 5 – Concurrency Tutorial | Jdk 1.5 java concurrency

Java CyclicBarrier Tutorial and Example
CyclicBarrier in Java is a synchronizer introduced in JDK 5 on java.util.Concurrent package along with other concurrent utility like Counting Semaphore, BlockingQueue, ConcurrentHashMap etc. CyclicBarrier is similar to CountDownLatch which we have seen in last article and allows multiple threads to wait for each other (barrier) before proceeding. Also difference between CoundDownLatch and CyclicBarrier is a also very popular multi-threading interview question in Java. CyclicBarrier is a natural requirement for concurrent program because it can be used to perform final part of task once individual tasks  are completed. All threads which wait for each other to reach barrier are called parties, CyclicBarrier is initialized with number of parties to be wait and threads wait for each other by calling CyclicBarrier.await() method which is a blocking method in Java and  blocks until all Thread or parties call await(). In general calling await() is shout out that Thread is waiting on barrier. await() is a blocking call but can be timed out or Interrupted by other thread. In this Java concurrency tutorial we will see simple example of CyclicBarrier on which three Threads will wait for each other before proceeding further.

Difference between CountDownLatch and CyclicBarrier in Java
In our last article we have see how CountDownLatch can be used to implement multiple threads waiting for each other. If you look at CyclicBarrier it also the does the same thing but there is a different you can not reuse CountDownLatch once count reaches zero while you can reuse CyclicBarrier by calling reset() method which resets Barrier to its initial State. What it implies that CountDownLatch is good for one time event like application start-up time and CyclicBarrier can be used to in case of recurrent event e.g. concurrently calculating solution of big problem etc. If you like to learn more about threading and concurrency in Java you can also check my post on When to use Volatile variable in Java  and How Synchronization works in Java.
CyclicBarrier in Java – Example
Java CyclicBarrier Example and Tutorial programHere is a simple example of CyclicBarrier in Java on which we initialize CyclicBarrier with 3 parties, means in order to cross barrier, 3 thread needs to call await() method. each thread calls await method in short duration but they don't proceed until all 3 threads reached barrier, once all thread reach barrier, barrier gets broker and each thread started there execution from that point. Its much clear with the output of following example of CyclicBarrier in Java:
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Java program to demonstrate how to use CyclicBarrier in Java. CyclicBarrier is a
 * new Concurrency Utility added in Java 5 Concurrent package.
 *
 *
 */
public class CyclicBarrierExample {

    //Runnable task for each thread
    private static class Task implements Runnable {

        private CyclicBarrier barrier;

        public Task(CyclicBarrier barrier) {
            this.barrier = barrier;
        }

        @Override
        public void run() {
            try {
                System.out.println(Thread.currentThread().getName() + " is waiting on barrier");
                barrier.await();
                System.out.println(Thread.currentThread().getName() + " has crossed the barrier");
            } catch (InterruptedException ex) {
                Logger.getLogger(CyclicBarrierExample.class.getName()).log(Level.SEVERE, null, ex);
            } catch (BrokenBarrierException ex) {
                Logger.getLogger(CyclicBarrierExample.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public static void main(String args[]) {

        //creating CyclicBarrier with 3 parties i.e. 3 Threads needs to call await()
        final CyclicBarrier cb = new CyclicBarrier(3, new Runnable(){
            @Override
            public void run(){
                //This task will be executed once all thread reaches barrier
                System.out.println("All parties are arrived at barrier, lets play");
            }
        });

        //starting each of thread
        Thread t1 = new Thread(new Task(cb), "Thread 1");
        Thread t2 = new Thread(new Task(cb), "Thread 2");
        Thread t3 = new Thread(new Task(cb), "Thread 3");

        t1.start();
        t2.start();
        t3.start();
    
    }
}
Output:
Thread 1 is waiting on barrier
Thread 3 is waiting on barrier
Thread 2 is waiting on barrier
All parties are arrived at barrier, lets play
Thread 3 has crossed the barrier
Thread 1 has crossed the barrier
Thread 2 has crossed the barrier


When to use CyclicBarrier in Java
Given the nature of CyclicBarrier it can be very handy to implement map reduce kind of task similar to fork-join framework of Java 7, where a big task is broker down into smaller pieces and to complete the task you need output from individual small task e.g. to count population of India you can have 4 threads which counts population from North, South, East and West and once complete they can wait for each other, When last thread completed there task, Main thread or any other thread can add result from each zone and print total population. You can use CyclicBarrier in Java :

1) To implement multi player game which can not begin until all player has joined.
2) Perform lengthy calculation by breaking it into smaller individual tasks, In general to implement Map reduce technique.

Important point of CyclicBarrier in Java
1. CyclicBarrier can perform a completion task once all thread reaches to barrier, This can be provided while creating CyclicBarrier.

2. If CyclicBarrier is initialized with 3 parties means 3 thread needs to call await method to break the barrier.
3. Thread will block on await() until all parties reaches to barrier, another thread interrupt or await timed out.
4. If another thread interrupt the thread which is waiting on barrier it will throw BrokernBarrierException as shown below:

java.util.concurrent.BrokenBarrierException
        at java.util.concurrent.CyclicBarrier.dowait(CyclicBarrier.java:172)
        at java.util.concurrent.CyclicBarrier.await(CyclicBarrier.java:327)

5.CyclicBarrier.reset() put Barrier on its initial state, other thread which is waiting or not yet reached barrier will terminate with java.util.concurrent.BrokenBarrierException.

That's all on CyclicBarrier in Java with Example. We have also seen difference between CountDownLatch and CyclicBarrier in Java and got some idea where we can use CyclicBarrier in Java Concurrent code.

Java hot concept : How class loading work in JVM | Class loading concept in Java

 When Class is loaded in Java
Class loading is done by ClassLoaders in Java which can be implemented to eagerly load a class as soon as another class references it or lazy load the class until a need of class initialization occurs. If Class is loaded before its actually being used it can sit inside before being initialized. I believe this may vary from JVM to JVM. While its guaranteed by JLS that a class will be loaded when there is a need of static initialization.

When a Class is initialized in Java
After class loading, initialization of class takes place which means initializing all static members of class. A Class is initialized in Java when :

1) an Instance of class is created using either new() keyword or using reflection using class.forName(), which may throw ClassNotFoundException in Java.

2) an static method of Class is invoked.
3) an static field of Class is assigned.
4) an static field of class is used which is not a constant variable.
5) if Class is a top level class and an assert statement lexically nested within class is executed.

Reflection can also cause initialization of class. Some methods of java.lang.reflect package may cause class to be initialized. JLS Strictly says that a class should not be initialized by any reason other than above.

How Class is initialized in Java
class loading and initialization in Java - When exampleNow we know what triggers initialization of a class in Java, which is precisely documented in Java language specification. Its also important to know in which order various fields (static and non static), block (static an non static), various classes (sub class and super class) and various interfaces (sub interface, implementation class and super interface) is initialized in Java. Infact many Core Java interview question and SCJP question based on this concept because it affect final value of any variable if its initialized on multiple places. Here are some of the rules of class initialization in Java:

1) Classes are initialized from top to bottom so field declared on top initialized before field declared in bottom
2) Super Class is initialized before Sub Class or derived class in Java
3) If Class initialization is triggered due to access of static field, only Class which has declared static field is initialized and it doesn't trigger initialization of super class or sub class even if static field is referenced by Type  of Sub Class, Sub Interface or by implementation class of interface.

4) interface initialization in Java doesn't cause super interfaces to be initialized.
5) static fields are initialized during static initialization of class while non static fields are initialized when instance of class is created. It means static fields are initialized before non static fields in Java.

6)non static fields are initialized by constructors in Java. sub class constructor implicitly call super class constructor before doing any initialization, which guarantees that non static or instance variables of super class is initialized before sub class.

Examples of  class initialization in Java:
Here is an example of when class is initialized in Java. In this example we will see which classes are initialized in Java.

/**
 * Java program to demonstrate class loading and initialization in Java.
 */
public class ClassInitializationTest {

    public static void main(String args[]) throws InterruptedException {

        NotUsed o = null; //this class is not used, should not be initialized
        Child t = new Child(); //initializing sub class, should trigger super class initialization
        System.out.println((Object)o == (Object)t);
    }
}

/**
 * Super class to demonstrate that Super class is loaded and initialized before Subclass.
 */
class Parent {
    static { System.out.println("static block of Super class is initialized"); }
    {System.out.println("non static blocks in super class is initialized");}
}

/**
 * Java class which is not used in this program, consequently not loaded by JVM
 */
class NotUsed {
    static { System.out.println("NotUsed Class is initialized "); }
}

/**
 * Sub class of Parent, demonstrate when exactly sub class loading and initialization occurs.
 */
class Child extends Parent {
    static { System.out.println("static block of Sub class is initialized in Java "); }
    {System.out.println("non static blocks in sub class is initialized");}
}

Output:
static block of Super class is initialized
static block of Sub class is initialized in Java
non static blocks in super class is initialized
non static blocks in sub class is initialized
false


Observation:
1) Super class is initialized before sub class in Java.
2) Static variables or blocks are initialized before non static blocks or fields.
3) Not used class is not initialized at all because its not been used, none of the cases mentioned on JLS or above which triggers initialization of class is not happened here.

Let's have a look on another example of class initialization in Java:

/**
 * Another Java program example to demonstrate class initialization and loading in Java.
 */

public class ClassInitializationTest {

    public static void main(String args[]) throws InterruptedException {

       //accessing static field of Parent through child, should only initialize Parent
       System.out.println(Child.familyName);
    }
}

class Parent {
    //compile time constant, accessing this will not trigger class initialization
    //protected static final String familyName = "Lawson";

    protected static String familyName = "Lawson";

    static { System.out.println("static block of Super class is initialized"); }
    {System.out.println("non static blocks in super class is initialized");}
}

Output:
static block of Super class is initialized
Lawson



Observation
1. Here class initialization occurs because static field is accessed which is not a compile time constant. had you declare "familyName" compile time constant using final keyword in Java (as shown in commented section) class initialization of super class would not have occurred.

2) Only super class is initialized even though static field is referenced using sub type.

There is another example of class initialization related to interface on JLS which explains clearly that initialization of sub interfaces does not trigger initialization of super interface. I highly recommend reading JLS 14.4 for understating  class loading and initialization in more detail.

That's all on When a class is initialized and loaded in Java. We have seen clear guidelines form JLS regarding class initialization. We have also seen the order on which super type and sub type are initialized and order of initialization for both static and non static fields and blocks in Java.

How synchronization works in java | synchorization working concept | point to remember during java synchronization interview question

Java Synchronization concept :

Every object has a intrinsic lock or monitor lock or simply we can say monitor . A thread can enter a sychronized method only if the thread can aquire the Object's lock. Once the thread aquires the lock automatically for a synchronized method , the thread enters into that method . As only one thread can own a lock of the object at a time, no other thread can own the same lock and enter into same synchronized method and other synchronized methods in the same object. The lock is released when the thread exits the synchronized method. The lock release occurs even if the return was caused by an uncaught exception. Now other thread can acquire the same lock to execute other synchronized methods or same method. When a thread releases an intrinsic lock, a happens-before relationship is established between that action and any subsequent acquistion of the same lock to ensure the reliable communication between threads.

There are two ways in which you can use synchronization to manage your threads of execution.
1. Synchronizing methods - Managing code at the method level .
eg. public synchronized void withdraw(int amount)
{code in method.}
2. Synchronizing statements(block of code) -Managing code at the block level.
eg. synchronized (object/class) { block of code }

Synchronized Methods

For protecting shared data / resource , no need to lock the data itself, we need to synchronize the methods that access that data / resource. You can make methods of a class mutual exclusive by declaring them using the keyword synchronized . You can make all the methods or some of the methods of a class synchronized depending upon the requirements. You have to synchronize those methods only that access / modify the shared data (eg. instance variables) or resources. All reads or writes to that object's variables by a thread are visible to other threads through synchronized methods.

Consider the above sample code for bank account.

class Account
{
private int balance;
private int accountNumber;
.........
public synchronized int getBalance()
{ return balance;
}

public synchronized void withdraw(int amount)
{
........
if (balance<amount)
{
throw new RuntimeException("Amount not available");
} balance -= amount;}

public void method3()
{
// code for the method3
}
}

 The above account class has three methods . Both withdraw() and getBalance() are synchronized and method3 is not synchronized. Now only one of the synchronized methods in a class object can execute at any one time . That means either withdraw() method or getBalance() method can be executed at any one time . method3 can always be executed by a thread. Unnecessary syncronization may reduce the performance of the program

Synchronizing Statement(s) (Blocks of code)

You can synchronize a statement or a block of code without synchronizing the whole method using the syntax given below . Synchronized statements must specify the object that provides the intrinsic lock. This is more useful because you can specify particular object that benefits from synchronization of the staments . When a thread enters the block that is synchronized on any given object , no other thread can enter other synchronized blocks or methods on the same object . Syntax is
synchronized(obj)
 {
 Block of code
 }

List list = Collections.synchronizedList(new ArrayList());
The above line returns a thread-safe list backed by the specified list . If one thread changes the list while another thread is traversing it through an Iterator, it will throw ConcurrentModificationException. To stop this exceptioin, Lock the entire List when you are iterating by using the synchronized block that synchronizes on the the returned list .
//block of code synchronized on list. So when the list is modified , it can't be traversed. When the list is iterated , this list can't be modified.

 synchronized(list) {
 Iterator iter = list.iterator();
while (iter.hasNext())
 doSomething(iter.next());
 }

You can also use , private lock object that is inaccessible outside, with synchronized block instead of using synchronized methods
private final Object lock1 = new Object();
 public void inc1() {
 synchronized(lock1) {
 c1++;
 }
 }

Synchronization can be considered expensive , because

1. Synchronization leads to poor performance.
a) The JVM takes time to make ordering of synchronization actions executing in different threads.
b) When a thread enters synchronized method, it has to acquire object's lock and releases when exits the method.
c) JMM (Java Memory Model) requires that the cached values to be invalidated immediately after the lock is acquired, and writing cache values back to main memory (flushing the cache) before it is released which ensures the proper inter-thread communication . Flushing the cache frequently can be expensive
2. Synchronized method can slow down the program execution , because synchronization does not allow concurrency . Only one thread is allowed at a time to enter into synchronized methods.
3. Synchronized methods may lead to deadlock

Summary points to remember on synchronization for Interview


1. The main objective of synchronization is to ensure that when several threads want access to a single resource , only one thread can access it at any given time.
2. Each object has only one lock.
3. Instance method gets lock on the Object where as static method get locks the Class in which the method is defined
4. Final fields and Immutable objects are thread-safe that requires no synchronization.
5. Constructors cannot be synchronized
6. If an object is visible to more than one thread, all reads or writes to that object's variables need to be done through synchronized methods.
6. Reentrant Synchronization : A thread can acquire a lock that it already owns which helps to invoke a method that also contains synchronized code
7. Alternative ways of synchronization are the use of classes in the java.util.concurrent package , declaring volatile variables that ensures all threads see a consistent value for the variable
8. The volatile modifier helps for reliable communication between threads but does not suuport for mutual exclusion
9. wait() method of object that can be called from synchronized methods or synchronized block to give up the lock i.e. to suspend the current thread until the notify() or notifyAll() method is called.

Complete program to test with synchronization and without Synchronization

import java.util.Random;
class Account {
private int balance;
private int accountNumber;
public Account(int accountNumber, int balance)
{
this.accountNumber=accountNumber;
this.balance=balance;
}
public synchronized int getBalance()
{
return balance;
}

public synchronized void withdraw(int amount) {
System.out.println("Accout Number = " + accountNumber + " " + Thread.currentThread().getName() + " needs " + amount);
try
{
Thread.sleep(30);
}catch(Exception e){ }

if (balance<amount)
{
System.out.println("Available Balance is = " + balance + " which is less than required amount " + amount);
throw new RuntimeException("Amount not available");
}
balance -= amount;
System.out.println(Thread.currentThread().getName() + " Withdraws " + amount );
System.out.println( " Current balance = " + balance);
}
}

public class BankOperation1 implements Runnable {
private Account account = new Account(21120, 10000);
public static void main (String [] args) {
BankOperation1 wd = new BankOperation1();
Thread person1 = new Thread (wd) ;
Thread person2 = new Thread (wd) ;
person1.setName ("Johnson") ;
person2.setName ("Jacob") ;
person2.start () ;
person1.start () ;
}
public void run ( ) {
Random rndObj = new Random();
System.out.println("Available Balance= " + account.getBalance());
//for (int i= 1; i <= 100; i++)
//{
account.withdraw(7500) ;
//}
}
}


When I run the above code without synchronized methods , i got the below output rarely. Remove synchronized keyword from the above code for testing


The above output shows , both threads (Mr. Jacob & Mr. Jhonson) enters into withdraw () method . Both withdraws money Rs. 7500/- . and the changes made to balance variable by one thread are not communicated to other thread.

When I run the above code with synchronized methods, i got the below output every time


The above output shows , only one thread (either Mr. Jacob or Mr. Jhonson) enters into withdraw () method . The first thread is able to withdraw money Rs. 7500/- . and the changes made to balance variable by one thread is communicated to other thread.