Monday, July 9, 2018

volatile usage --problem

import java.util.ArrayList;



public class ThreadTest {

public static void main(String[] args) {

ArrayList arr = new ArrayList();

TestTwo bobj = new TestTwo();

for (int  i =0; i<10 font="" i="">
arr.add(new MyTh(i,bobj));


for (int  i =0; i<10 font="" i="">
{
((Thread ) arr.get(i)).start();


}

}

}



class MyTh extends Thread{


int  num ;

static int  age ;


volatile StringBuffer buff = new StringBuffer("0");

static  StringBuffer buff2 = new StringBuffer("0");

TestTwo obj ;

MyTh (int  k, TestTwo cobj )

{this.num=k; 
this.obj=cobj;} 

@Override
public void run() {

if(this.num==6){
//setAge(40);
this.obj.count.append("hi");
this.buff.append("hi");
this.buff2.append("hi");
this.age =40;
    }
System.out.println(Thread.currentThread().getName() + " :"+this.num + ":" + this.age +":"+
" :[" +this.buff2 +":"+ this.buff2.hashCode() + 
    "] : [" + this.buff + " :"+ this.buff.hashCode() +"] :[ "+ this.obj.count+ ":"+this.obj.count.hashCode() +":]");

}

int   getAge(){
return age;
}

void  setAge(int  k){
this.age =k;
}

}


class TestTwo {

volatile StringBuffer count = new StringBuffer("0");


}


------------



Thread-0 :0:0: :[0:1740525517] : [0 :1769581898] :[ 0:1833313469:]
Thread-1 :1:0: :[0:1740525517] : [0 :857628581] :[ 0:1833313469:]
Thread-2 :2:0: :[0:1740525517] : [0 :1498557318] :[ 0:1833313469:]
Thread-3 :3:0: :[0:1740525517] : [0 :1419206900] :[ 0:1833313469:]
Thread-4 :4:0: :[0:1740525517] : [0 :313434985] :[ 0:1833313469:]
Thread-5 :5:0: :[0:1740525517] : [0 :1644604984] :[ 0:1833313469:]
Thread-6 :6:40: :[0hi:1740525517] : [0hi :1055613344] :[ 0hi:1833313469:]
Thread-7 :7:40: :[0hi:1740525517] : [0 :1151033700] :[ 0hi:1833313469:]
Thread-8 :8:40: :[0hi:1740525517] : [0 :1472482392] :[ 0hi:1833313469:]
Thread-9 :9:40: :[0hi:1740525517] : [0 :1908885604] :[ 0hi:1833313469:]

Saturday, June 9, 2018

Java Tidbits .........(char vs byte stream )

A stream is a way of sequentially accessing a file. In Streams you can process the data one at a time as bulk operations are unavailable with them. But, streams supports a huge range of source and destinations including disk file, arrays, other devices, other programs etc. In Java, a byte is not the same thing as a char . Therefore a byte stream is different from a character stream. So, Java defines two types of streams: Byte Streams and Character Streams .
Byte Streams
A byte stream access the file byte by byte. Java programs use byte streams to perform input and output of 8-bit bytes. It is suitable for any kind of file, however not quite appropriate for text files. For example, if the file is using a unicode encoding and a character is represented with two bytes, the byte stream will treat these separately and you will need to do the conversion yourself. Byte oriented streams do not use any encoding scheme while Character oriented streams use character encoding scheme(UNICODE). All byte stream classes are descended from InputStream and OutputStream .

Monday, May 21, 2018

Obtain the count of total rows from SQL query bounded by rownum




Below query i used in a  project where  TOTAL count was obtained  even when Query is bounded by rownum on both ends .


select * from (select PART_NR,CTRY_CD,CUST_TRF_CD,VAL_FROM_DT,CHG_DT,NAT_HS_CD_2 , ROW_NUMBER() OVER(ORDER BY  CHG_DT,PART_NR) AS ID,
 COUNT(1) OVER ( ORDER BY 1 RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) ROW_COUNT from
((select TRIM(A.PART_NR) as PART_NR, TRIM(A.CTRY_CD) as CTRY_CD, TRIM(A.CUST_TRF_CD) as CUST_TRF_CD ,TO_CHAR(A.VAL_FROM_DT ,'yyyymmddhh24miss') as VAL_FROM_DT, TO_CHAR(A.CHG_DT ,'YYYYMMDDhh24miss') as CHG_DT,(case when (A.NAT_HS_CD is null  and A.CUST_TRF_CD is not null)  then
(select TRIM(C.NAT_HS_CD) from (select * from  (select CUST_TRF_CD, CTRY_CD, NAT_HS_CD ,VAL_FROM_DT from CLA_NAT_HS_DETAIL where CTRY_CD =421
 and INVALID_CD=0) AA where  VAL_FROM_DT= (select max(VAL_FROM_DT) from (select CUST_TRF_CD, CTRY_CD, NAT_HS_CD ,VAL_FROM_DT from CLA_NAT_HS_DETAIL
 where CTRY_CD  =421   and INVALID_CD=0) TT  where AA.CUST_TRF_CD=TT.CUST_TRF_CD and AA.CTRY_CD=TT.CTRY_CD) ) C where  C.CUST_TRF_CD=A.CUST_TRF_CD and
 C.CTRY_CD=A.CTRY_CD )  else TRIM(A.NAT_HS_CD)  end) as NAT_HS_CD_2 from cla_cust_part_ctry A where
 A.ctry_cd =421  and  A.CHG_DT between TO_DATE( '20110713000000', 'yyyymmddhh24miss') and TO_DATE( '20110714000000', 'YYYYMMDDhh24miss') and
 TRIM(A.PART_NR) not like '% %' ))  where  NAT_HS_CD_2 is not null) where ID > 20 and ID < 46 order by ID asc