Sunday, October 19, 2014

what is a volatile

Generally JVM copies frequently accessed variable in to the register memory for speedy access.Volatile emphasis that always the variables are read from the main memory to avoid
duplicate copies.

Tuesday, April 15, 2014

Java is passes by value or reference and what about passing object to a method.

Java is strictly is a pass by value Objects, however, work a bit differently. When you pass a Java object or array as a parameter, an object reference or array reference is passed into a method. 


Java is always pass-by-value. The difficult thing can be to understand that Java passes objects as references and those references are passed by value.
It goes like this:
public void foo(Dog d) {
  d.getName().equals("Max"); // true
  d = new Dog("Fifi");
  d.getName().equals("Fifi"); // true
}

Dog aDog = new Dog("Max");
foo(aDog);
aDog.getName().equals("Max"); // true
In this example aDog.getName() will still return "Max"d is not overwritten in the function as the object reference is passed by value
Likewise:
public void foo(Dog d) {
  d.getName().equals("Max"); // true
  d.setName("Fifi");
}

Dog aDog = new Dog("Max");
foo(aDog);
aDog.getName().equals("Fifi"); // true
http://stackoverflow.com/questions/9404625/java-pass-by-reference

Tuesday, March 18, 2014

Get the ProcessList

Below code gives all the processors of your machine.
It will create a ProcessList.txt file for you,with all the processor listed.

package General;

import java.io.*;
import java.util.StringTokenizer;

public class GetProcessList
{

 private String GetProcessListData()
 {
 Process p;
 Runtime runTime;
 String process = null;
 try {
 System.out.println("Processes Reading is started...");

 //Get Runtime environment of System
 runTime = Runtime.getRuntime();

 //Execute command thru Runtime
 p = runTime.exec("tasklist");      // For Windows
 //p=r.exec("ps ux");              //For Linux

 //Create Inputstream for Read Processes
 InputStream inputStream = p.getInputStream();
 InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
 BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

 //Read the processes from sysrtem and add & as delimeter for tokenize the output
 String line = bufferedReader.readLine();
 process = "&";
 while (line != null) {
 line = bufferedReader.readLine();
 process += line + "&";
 }

 //Close the Streams
 bufferedReader.close();
 inputStreamReader.close();
 inputStream.close();

 System.out.println("Processes are read.");
 } catch (IOException e) {
 System.out.println("Exception arise during the read Processes");
 e.printStackTrace();
 }
 return process;
 }

 private void showProcessData()
 {
 try {

 //Call the method For Read the process
 String proc = GetProcessListData();

 //Create Streams for write processes
 //Given the filepath which you need.Its store the file at where your java file.
 OutputStreamWriter outputStreamWriter =
 new OutputStreamWriter(new FileOutputStream("ProcessList.txt"));
 BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);

 //Tokenize the output for write the processes
 StringTokenizer st = new StringTokenizer(proc, "&");

 while (st.hasMoreTokens()) {
 bufferedWriter.write(st.nextToken());  //Write the data in file
 bufferedWriter.newLine();               //Allocate new line for next line
 }

 //Close the outputStreams
 bufferedWriter.close();
 outputStreamWriter.close();

 } catch (IOException ioe) {
 ioe.printStackTrace();
 }

 }

 public static void main(String[] args)
 {
 GetProcessList gpl = new GetProcessList();
 gpl.showProcessData();

 }
}


Starting Windows Services from the java code(Tomcat)

in order to start a particular service you need to confirm that the service is there in the services Tab of the Window Task Manager.Here i will be starting tomcat.

Copy and paste the following code

package General;

import java.io.*;
import java.util.*;

public class Service {
  public static void main(String args[]) {

 String arg1="start";
 String arg2="stop";
    String[] command = {"cmd.exe", "/c", "sc", arg1, "Tomcat7"};
    try {
      Process process = new ProcessBuilder(command).start();
      InputStream inputStream = process.getInputStream();
      InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
      BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
      String line;
      while ((line = bufferedReader.readLine()) != null) {
         System.out.println(line);
      }
    } catch(Exception ex) {
      System.out.println("Exception : "+ex);
    }
  }
}


java-8-streams-map-examples

package com.mkyong.java8; import java.math.BigDecimal; import java.util.ArrayList; import java.util.Arrays; import java.util.List; im...