Search This Blog

Friday, April 10, 2015

AtomicOperations

Atomic operations are singular ( an operation that cannot be stopped in between). Just like a transaction, it happens completely or does not happen at all. The operations that are atomic :
  1. Read and write for reference variables
  2. Read and write for byte, short, int, char, float, and Boolean
  3. Read and write for all variables declared with the volatile qualifier ( this includes all primitive types)
Operations like increment and decrement look like atomic operations, but there is no guarantee that they are atomic. As any increment operation x++ can be a group of three operations
a)      Temporary copy of x will be created
b)      Temporary copy will be incremented
c)       Newly incremented value will be written back to variable x

Variables of datatype double and long are 64-bit and can be accessed by 2 32-bit operations. During concurrent programming, this can compromise the integrity of data. Java.util.concurrent provides atomic APIs to solve this problem. Let us take a look at an example:

package atomicops;

import java.util.concurrent.atomic.AtomicLong;

/**
 *
 * @author Nancy
 */
public class TestAtomicLong {
    public static void main(String[] args) {
        AtomicLong myLong= new AtomicLong(25);
     
        long x = myLong.getAndAdd(20);
        System.out.println("x ="+x);
        x=myLong.get();
        System.out.println("x ="+x);
     
        x = myLong.addAndGet(30);
        System.out.println("x ="+x);
     
        boolean b = myLong.compareAndSet(75,100);
        System.out.println("b ="+b);
        x=myLong.get();
        System.out.println("x ="+x);
     
        x= myLong.incrementAndGet();
        System.out.println("x="+x);
     
         x= myLong.decrementAndGet();
        System.out.println("x="+x);
    }  
}

Output :
x =25
x =45
x =75
b =true
x =100
x=101
x=100

Other Atomic operation classes are : 

©Nancy
11th April, 2015
2:34 am

Thursday, January 1, 2015

SOA Question 4

Web Service contains

a) Contract
b) A mandatory Java class
c) Interface
d) Implementation

Tuesday, December 30, 2014

SOA Question 3

Oracle Mediator is a service component (Oracle SOA Suite) that provides mediation capabilities. Which of the given options is NOT true?

a) Routing
b) Transformation
c) Filtering
d) Cannot modify routing rules at runtime
e) Validation 

SOA Question 2

Web services are loosely coupled and self-contained units of functionality.

a) True
b) False

Sunday, December 28, 2014

SOA Question - 1

Which statement is TRUE about SOA (Service Oriented Architecture)?

a) It is an Oracle product
b) It is a Microsoft product
c) It is an IBM product
d) It is independent of any vendor, technology, or product
e) It is a programming language
f)  It is a modeling language like UML

Tuesday, March 4, 2014

Which Constructors are called?

public class First {
    public First(){
        super();
        System.out.println("First... No Actually Second");
    }
}

public class Second extends First {
    public Second(){
        super();
        System.out.println("Second...No third");
    }
    public static void main(String[] args) {
        Second second= new Second();
    }
}

Will this code run? Answer to that is YES.

OUTPUT : 

First... No Actually Second
Second...No third

Why highlighted statement super() in First class does not give an error?

As we create an object of Subclass'Second', constructors are called in the order of creation of classes. Second() makes call to First() before it executes the instructions in Second(). First() makes a call to its Super class constructor Object()  which is Universal Super class of all Java Objects.

Saturday, February 22, 2014

Date and Time --- Simple Arithmetic

/* Program to add and subtract days and Hours. It can be applied for other fields of date and time too */

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package test;
import java.util.Calendar;


public class DateTimeUtility {
    public static void main(String[] args) {
        int addDays = 4;
        int subtractDays = 10;
        int addHours=3;
        int subtractHours=26;
     
        Calendar cal = Calendar.getInstance();
        //System.out.println(cal);
        //Current date
        System.out.println("Today's date : " + (cal.get(Calendar.MONTH) + 1) +
                "/" + cal.get(Calendar.DATE) + "/" + cal.get(Calendar.YEAR));
   
        //Current Time
        System.out.println("Current Time  : " + (cal.get(Calendar.HOUR) ) +
                ":" + cal.get(Calendar.MINUTE) + ":" + cal.get(Calendar.SECOND));

        //Adding days
        cal.add(Calendar.DATE, addDays);
        System.out.println("Date (after): " + (cal.get(Calendar.MONTH) + 1) +
                "/" + cal.get(Calendar.DATE) + "/" + cal.get(Calendar.YEAR));
        cal = Calendar.getInstance();

        //subtracting days
        cal.add(Calendar.DATE, -subtractDays);
        System.out.println("Date (before): " + (cal.get(Calendar.MONTH) + 1) +
               "/" + cal.get(Calendar.DATE) + "/" + cal.get(Calendar.YEAR));

       //Adding Hours
        cal.add(Calendar.HOUR, addHours);
        System.out.println("New Time after adding " + addHours +" Hours "+
                cal.get(Calendar.HOUR) + ":" + cal.get(Calendar.MINUTE) +
                ":" + cal.get(Calendar.SECOND));
        cal=Calendar.getInstance();

       //Subtracting Hours
        cal.add(Calendar.HOUR, -subtractHours);
        System.out.println("Time before " + subtractHours+" Hours "+
                (cal.get(Calendar.HOUR) ) + ":" + cal.get(Calendar.MINUTE) +
                ":" + cal.get(Calendar.SECOND));
    }
}

OUTPUT :

Today's date : 2/22/2014
Current Time  : 6:22:6
Date (after): 2/26/2014
Date (before): 2/12/2014
New Time after adding 3 Hours 9:22:6
Time before 26 Hours 4:22:6