Java resource consumption

So yesterday I fired up Eclipse, I did SVN update on my project and I ran the ant. And it took unusually long time to finish. 10 minutes! At some points, Eclipse occupied so much computing resource that it brought my computer to an unresponsive state. Mouse clicks would not respond. Neither would keyboard presses. I took print screen of the resource usage and it surprised me to see that java (Eclipse) occupied 228% of CPU. How could that be? 228%? Well I have quadcore processor, so does it mean that the maximum CPU percentage would be 400%? I also noticed that the signjar target of the ant was the most resource consuming. Following are the screenshots:


"Run as -> Ant build" disappear from pop up menu!

So.... this is weird. And inconvenient too. I was working on my Eclipse Helios and as I right click on my build.xml file, the "Run as -> Ant build" disappear from pop up menu! The cure is simple: Restart eclipse! But it caused some nerve at first because I had thought something went wrong and I had to do a lot of work to get it back to normal function.

Converting CHM file to html and pdf

From http://www.ubuntugeek.com/how-to-convert-chm-files-to-html-or-pdf-files.html


Microsoft Compiled HTML Help is a proprietary format for online help files, developed by Microsoft and first released in 1997 as a successor to the Microsoft WinHelp format. It was first introduced with the release of Windows 98, and is still supported and distributed through Windows XP platforms.
HTML Help files are made with help authoring tools. Microsoft ships the HTML Help Workshop with supported versions of Microsoft Windows and makes the tool available for free download. There are also a lot of third-party help authoring tools available.
CHM files, known as Microsoft Compressed HTML Help files, are a common format for eBooks and online documentation. They are basically a collection of HTML files stored in a compressed archive with the added benefit of an index.
Under Linux, you can view a CHM file with the xchm viewer. But sometimes that’s not enough. Suppose you want to edit, republish, or convert the CHM file into another format such as the Plucker eBook format for viewing on your Palm. To do so, you first need to extract the original HTML files from the CHM archive.
This can be done with the CHMLIB (CHM library) and its included helper application extract_chmLib.
Install Chmlib in Ubuntu
sudo apt-get install libchm-bin
Convert .chm files in to HTML files
If you want to convert .chm files in to HTML files use the following command
extract_chmLib book.chm outdir
where book.chm is the path to your CHM file and outdir is a new directory that will be created to contain the HTML extracted from the CHM file.
Convert .chm files in to PDF files
First you need to install htmldoc. HTML processor that generates indexed HTML, PS, and PDF.HTMLDOC is a program for writing documentation in HTML and producing indexed HTML, PostScript, or PDF output (with tables of contents). It supports most HTML 3.2 and some HTML 4.0 syntax, as well as GIF, JPEG, and PNG images.
sudo apt-get install htmldoc
If you want to use htmldoc type the following command in terminal
htmldoc

Threads in Java can outlive the thread that spawned it!

I often read in Java blogs that main thread is the last thread to die after all other threads die. This is obviously not what I experience. Main thread dies right after it finishes its business, leaving the threads that main spawned working alone. Moreover, if main spawns ThreadA, for example, and main joins ThreadA using Thread.join() method, then main only waits until ThreadA dies, then main dies. If in the process of running ThreadA spawns some other threads, then when ThreadA and main dies, those other threads will keep running if their business is not finished yet.

Interesting comment from this website:

The main() method must indeed have a void return type. From the Java Language Specification on "Execution - Virtual Machine Start-Up" (§12.1.4): The method main must be declared publicstatic, and void. It must accept a single argument that is an array of strings.



A program terminates all its activity and exits when one of two things happens:
  • All the threads that are not daemon threads terminate.
  • Some thread invokes the exit method of class Runtime or class System and the exit operation is not forbidden by the security manager.
In other words, the program may exit before or after the main method finishes; a return value frommain would therefore be meaningless. Therefore the return value of main is void.

main() method has no control over JVM. When JVM starts, it will run main() method, but when main() finishes, it doesn't mean that JVM terminaes. JVM continues to execute all threads until 1) Runtime.exit() is called OR 2) all normal (not daemon) threads have died. Daemon threads do not count for this second condition. In other words ... if main() method spawns some normal threads, JVM will notterminate when main() finishes. If main() doesn't spawn any threads, JVM will terminate. If main() spawns only daemon threads, JVM will also terminate when main() finishes.



Compile and run the following code:

package test;

import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

public class ThreadTest {

/**
* @param args
* @throws InterruptedException
*/
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub

System.out.println("The current main thread: "
+ Thread.currentThread().getName());

printAllThreads();

ThreadA tc = new ThreadA();
tc.start();

tc.join();
// Thread.sleep(5000);

System.out.println("Exiting main thread.");

}

static private void printAllThreads() {
Map stackTraces = Thread
.getAllStackTraces();
Set< Entry> entrySet = stackTraces
.entrySet();
for (Entry entry : entrySet) {
Thread t = entry.getKey();
System.out.println(">>>>>>>>>> " + t.getName());
}
}
}

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

package test;

import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

public class ThreadA extends java.lang.Thread {

    private volatile int counterA = 0;
    
    public ThreadA(){
        setName("Threadcounter");
    }

    public void run() {

        while (counterA < 10) {
            System.out.println("Printing from within ThreadA: " + counterA++);
            printAllThreads();
            try {
                sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        
        ThreadB pt = new ThreadB();
        pt.start();
        
        System.out.println("Exiting ThreadA.");

    }

    private void printAllThreads() {
        Map stackTraces = Thread
                .getAllStackTraces();
        Set< Entry> entrySet = stackTraces
                .entrySet();
        for (Entry entry : entrySet) {
            Thread t = entry.getKey();
            System.out.println(">>>>>>>>>> Printing From ThreadCounter: "
                    + t.getName());
        }
    }

    private class ThreadB extends Thread {

        private volatile int counterB = 0;
        
        public ThreadB(){
            setName("Privatethread");
        }

        public void run() {
            while (counterB < 100) {
                System.out.println("Printing from within ThreadB: " + counterB++);
                printAllThreads();
                try {
                    sleep(1000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            System.out.println("Exiting ThreadB.");
        }
    }

}

Missing php-xml in standard Fedora installation

So I was developing a small php app which used DOM manipulation classes on my local Ubuntu machine. Everything went fine. Then I uploaded my app to the server which ran Fedora. My app would not work. The DOM classes were not recognized. It turned out that out of the box, php installation in Fedora did not include php-xml package which was needed for DOM manipulation. So, I had to install it.

su
yum install php-xml
yum install php-xmlrpc