Tuesday Jun 03, 2008

Compilation on Fly !

Weeks back, I had a discussion with one of my colleagues on classLoader. Actually his classLoader is not able to perform certain job but then suddenly he asked me about any option in Java from which I can generate class file at runtime. Yes, why not. This is how JSP Compiler,Jasper works. There are some of the older way to do this in previous Java. But from J2SE 6 and onwards, one can do it neatly. 

J2SE 6 has added a package called javax.tools which has some awesome work to do. And the above task is one of it. We are going to use a class call ToolProvider which provides method to locate tools like Compiler :-).

Here I have written a simple code which go ahead and compile all the java files in a given directory at runtime and show the result.

import java.io.\*;
import javax.tools.JavaCompiler;
import javax.tools.ToolProvider;

public class RuntimeCompilation {

    public static void main(String args[]) throws IOException {
        String dirName = "E:\\\\Program Files\\\\Java\\\\jdk1.6.0\\\\bin\\\\compiler";
        File f = new File(dirName);
        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
        if (f.isDirectory() == false) {
            System.out.println("Directory is not correct");
        } else {
            String lists[] = f.list();
            for (int i = 0; i < lists.length; i++) {
                if (lists[i].endsWith("java")) {
                    int results = compiler.run(null, null, null, lists[i]);
                    System.out.println("Compiling ..." + lists[i] + "      " + (results == 0));
                }
            }
        }
    }
}
 

I have 4-5 Java files at this location: E:\\Program Files\\Java\\jdk1.6.0\\bin\\compiler. So, simply checked the directory and then list the file and pass it into the compilation process, thats it.Code is not written cleanly but do the basic job :). javax.tools package is quite amazing, have a look !

Wednesday Apr 30, 2008

Compiler Optimization Can cause problem

Last week, I was created a presentation on Multi-threading in Java.
Though this fact, I have covered in presentation but still wanted to
blog on same. In multi-threading world, compiler optimization can cause
serious problems. Just check my small 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 optimize the code and remove
the part of if(flag), thinking of 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. Just run
this code 5-6 may be 10 times, you will be able to see the SOP
statement "This can be reached". Just for the shake 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.

About

Hi, I am Vaibhav Choudhary working in Oracle with JDK team. This blog is all about simple concept of Java, JVM and JavaFX.

Search

Archives
« July 2015
SunMonTueWedThuFriSat
   
2
3
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
 
       
Today