w3hJava

What, Why, When and How of Java, JavaFX and related technologies


Published May 16th, 2008

Is Compiler Optimization always good?

Last week, I made a presentation on Multi-threading in Java. Although I had covered this aspect in presentation but still wanted to blog more on the same. In multi-threading world, compiler optimization can cause serious problems. Check out this small piece of code

public class NonVolatileProblem extends Thread {

    ChangeFlag cf;

    public static void main(String[] args) {
        ChangeFlag cf = new ChangeFlag();
        NonVolatileProblem th1 = new NonVolatileProblem(cf);
        NonVolatileProblem th2 = new NonVolatileProblem(cf);
        th1.start();
        th2.start();
    }

    public void run() {
        cf.method1();
        cf.method2();
    }

    public NonVolatileProblem(ChangeFlag cf) {
        this.cf = cf;
    }
}

class ChangeFlag {

    boolean flag = false;

    public void method1() {
        flag = false;
        try {
            Thread.sleep(1000);
        } catch (Exception e) {
            System.out.println(”Don’t want to be here”);
        }
        if (flag) {
            System.out.println(”This can be reached “);
        }
        System.out.println(”Value of flag” + flag);
    }

    public void method2() {
        flag = true;
    }
}

Check out the reason in bold. Now if compiler optimizes the code and remove the part of if(flag), thinking that flag value will always be false. Then we have a situation here(FBI style of speaking :-D), because other thread can change its value and can make the flag value true.

Try to run this code 5-6 may be 10 times, you will be able to see the SOP statement “This can be reached“. Just for te sake of getting that, I have added sleep statement. Here what I got on my 3rd run of the code :)

Value of flag:false
This can be reached
Value of flag:true

Handling such type of situation is not difficult, specification says to add a word volatile before the variable flag which will tell the compiler not to optimize its code just by seeing some initial value or declaration.

Published May 13th, 2008

Listing Process from Java Code

A month back, I wanted to find out all processes running on my machine from java code for some stupid purpose. I tried to write some code and was pretty successful. Java can’t play with system process and hence invoking a runtime is only solution to get all process and here it is:

import java.io.*;
class ListProcess {
    public static void main(String[] args) throws IOException {
        Runtime runtime = Runtime.getRuntime();
        String cmds[] = {”cmd”, “/c”, “tasklist”};
        Process proc = runtime.exec(cmds);
        InputStream inputstream = proc.getInputStream();
        InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
        BufferedReader bufferedreader = new BufferedReader(inputstreamreader);
        String line;
        while ((line = bufferedreader.readLine()) != null) {
            System.out.println(line);
        }
    }
}

Code is written exclusively for Windows Machine :). And one line change in this code will list you only java running process.

String cmds[] = {”cmd”, “/c”, “jps”}; this is nothing but running jps.exe file in bin (jdk6 onwards). Its not all done. Writing Runtime code is not the real solution as there is little of platform dependencies. So, I have decided to write the code for getting List of Java Process. Again, I have checked by OpenJDK code for jps(search on jps.java file :) ) and I got some hint how to do it and here it goes:


import java.util.*;
import sun.jvmstat.monitor.*;
public class ListJavaProcess {
    public static void main(String[] args) throws Exception {
        /* Checking for local Host, one can do for remote machine as well */
        MonitoredHost local = MonitoredHost.getMonitoredHost(”localhost”);
        /* Take all active VM’s on Host, LocalHost here */
        Set vmlist = new HashSet(local.activeVms());
        for (Object id : vmlist) {
            /* 1234 - Specifies the Java Virtual Machine identified by lvmid 1234 on an unnamed host.
            This string is transformed into the absolute form //1234, which must be resolved against
            a HostIdentifier. */
            MonitoredVm vm = local.getMonitoredVm(new VmIdentifier(”//” + id));
            /* take care of class file and jar file both */
            String processname = MonitoredVmUtil.mainClass(vm, true);
            System.out.println(id + ” ——> ” + processname);
        }
    }
}

I have written good amount of comment as it is all together a sun import rather than java or javax import. This import resides in tools.jar, so even running simple javac and java will not work. So, running the program will go here:

E:\Program Files\Java\jdk1.6.0_10\bin>javac -classpath "E:\Program Files\Java\jdk1.6.0_10\libtools.jar" ListJavaProcess.java
E:\Program Files\Java\jdk1.6.0_10\bin>java -classpath .;"E:\Program FilesJava\jdk1.6.0_10lib\tools.jar" ListJavaProcess
3700 ------> ListJavaProcess

Right now only one java process is running. Now in the second code, you can play with some of the java process, but with native process in the above code you can’t do anything except watching it :)

No idea how to do this in JDK 1.5 or backwards(runtime is off course one option).

Published May 9th, 2008

Sorting different Language(Locale) in Java

I have given a little thought what should be my first post. Finally decided not to write anything complex. I am just posting about simple sorting fundas in Java. Sorting is always a tricky game in any programming language and it is responsible for 50-60 percent of the total CPU time for any application. We all have our native language like Hindi, Chinese, Japanese, French and so many. Most of the time world deals with sorting of Alphabets or English words but give a eye on other languages which is growing fast and off course today we are talking about internationalization.

I am showing you a typical sorting of French word and the blunder associated with it. These are some of the common French words:

String[] names = {”fácil”, “facil”, “fast”,”Où”, “êtes-vous”, “spécifique”, “specific”, “ou”};

and here is the typical sorting code:

String[] names = {”fácil”, “facil”, “fast”,”Où”, “êtes-vous”, “spécifique”, “specific”, “ou”};
        List list = Arrays.asList(names);
        Collections.sort(list);
        Iterator itr = list.iterator();
        while(itr.hasNext()) {
            System.out.print(itr.next()+ ”   “);
        }

And the result:
Où facil fast fácil ou specific spécifique êtes-vous

which is completely wrong according to French Rules. Because sorting is simply going via UNICODE rules not by French rules.

Now remedy: Java gives us a class called Collator class in java.text Package which takes care of locale while sorting. Here goes the code:

import java.text.*;
import java.util.*;
class CollatorTest {

    public static void main(String[] args) {
        String[] names = {”fácil”, “facil”, “fast”, “Où”, “êtes-vous”, “spécifique”, “specific”, “ou”};
        List list = Arrays.asList(names);
        Collections.sort(list);
        Iterator itr = list.iterator();
        while (itr.hasNext()) {
            System.out.print(itr.next() + ” “);
        }
        Locale[] loc = Collator.getAvailableLocales();
        Collator myCollator = Collator.getInstance(new Locale(”fr”));
        myCollator.setStrength(Collator.PRIMARY);
        Collections.sort(list, myCollator);
        itr = list.iterator();
        System.out.println(”");
        while (itr.hasNext()) {
            System.out.print(itr.next() + ” “);
        }
        myCollator.setStrength(Collator.TERTIARY);
        Collections.sort(list, myCollator);
        itr = list.iterator();
        System.out.println(”");
        while (itr.hasNext()) {
            System.out.print(itr.next() + ” “);
        }
    }
}

And here is the result:

Où     facil     fast     fácil     ou     specific     spécifique     êtes-vous
êtes-vous     facil     fácil     fast     Où     ou     specific     spécifique
êtes-vous     facil     fácil     fast     ou     Où     specific     spécifique

which is perfectly valid. There are lot of option in Collator class which we will discuss sometimes later here only.