Showing posts with label example. Show all posts
Showing posts with label example. Show all posts

01 October 2010

Count the number of CPUs in your system

import java.io.*;

class Test {
       static private boolean isWindows = System.getProperty("os.name").startsWith("Windows");

       static private int n_CPUs = 0;

       static public int getCPUCount() {
              if (n_CPUs > 0) return n_CPUs;
              if (isWindows) return 1; // no clue
              // POSIX systems, attempt to count CPUs from /proc/stat
              try {
                     Runtime runtime = Runtime.getRuntime();
                     Process process = runtime.exec("cat /proc/stat");
                     InputStream is = process.getInputStream();
                     InputStreamReader isr = new InputStreamReader(is);
                     BufferedReader br = new BufferedReader(isr);
                     String line;
                     n_CPUs = 0;
                     // valid cores will print as cpu0, cpu1. cpu2 ...
                     while ((line = br.readLine()) != null) {
                            if (0 == line.indexOf("cpu") && line.length() > 3
                            && Character.isDigit(line.charAt(3))) {
                                   n_CPUs++;
                            }
                     }
                     // fix possible errors
                     if (0 == n_CPUs) n_CPUs = 1;
                     return n_CPUs;
              } catch (Exception e) {
                     //Utils.log(e.toString()); // just one line
                     return 1;
              }
       }

       public static void main(String args[]) {
              System.out.println("No of CPU = "+getCPUCount());
       }
}

24 September 2010

Create a Java source dynamically, compile and call

import java.io.*;
import java.util.*;
import java.lang.reflect.*;

public class MakeTodayClass {
       Date today = new Date();
       String todayMillis = Long.toString(today.getTime());
       String todayClass = "z_" + todayMillis;
       String todaySource = todayClass + ".java";

       public static void main (String args[]){
              MakeTodayClass mtc = new MakeTodayClass();
              mtc.createIt();
              if (mtc.compileIt()) {
                      System.out.println("Running " + mtc.todayClass + ":\n\n");
                      mtc.runIt();
                      }
              else
                      System.out.println(mtc.todaySource + " is bad.");
              }

       public void createIt() {
              try {
                     FileWriter aWriter = new FileWriter(todaySource, true);
                     aWriter.write("public class "+ todayClass + "{");
                     aWriter.write(" public void doit() {");
                     aWriter.write(" System.out.println(\""+todayMillis+"\");");
                     aWriter.write(" }}\n");
                     aWriter.flush();
                     aWriter.close();
                     }
              catch(Exception e){
                     e.printStackTrace();
                     }
              }

       public boolean compileIt() {
              String [] source = { new String(todaySource)};
              ByteArrayOutputStream baos= new ByteArrayOutputStream();

              new sun.tools.javac.Main(baos,source[0]).compile(source);
              // if using JDK >= 1.3 then use
              // public static int com.sun.tools.javac.Main.compile(source);
              return (baos.toString().indexOf("error")==-1);
              }

       public void runIt() {
              try {
                     Class params[] = {};
                     Object paramsObj[] = {};
                     Class thisClass = Class.forName(todayClass);
                     Object iClass = thisClass.newInstance();
                     Method thisMethod = thisClass.getDeclaredMethod("doit", params);
                     thisMethod.invoke(iClass, paramsObj);
                     }
              catch (Exception e) {
                     e.printStackTrace();
                     }
              }
}

23 September 2010

A program to print limits of the primitive types

public class PrintLimits
{

       public PrintLimits(){
       }

       public static void main(String args[]) {

              System.out.println("Min byte value = " + Byte.MIN_VALUE);
              System.out.println("Max byte value = " + Byte.MAX_VALUE);
              System.out.println("Min short value = " + Short.MIN_VALUE);
              System.out.println("Max short value = " + Short.MAX_VALUE);
              System.out.println("Min int value = " + Integer.MIN_VALUE);
              System.out.println("Max int value = " + Integer.MAX_VALUE);
              System.out.println("Min float value = " + Float.MIN_VALUE);
              System.out.println("Max float value = " + Float.MAX_VALUE);
              System.out.println("Min double value = " + Double.MIN_VALUE);
              System.out.println("Max double value = " + Double.MAX_VALUE);
       }
}