VOOZH about

URL: https://dzone.com/users/901641/leolewis.html

⇱ Leo Lewis - DZone Member


Leo Lewis

Lead developer at Electrolux Group

Tokyo, JP

Joined May 2011

About

Leo Leo is a Java Geek and had the opportunity to apply his skills to various industry, from aerospace, e-commerce, and gaming.

Stats

Reputation: 3
Pageviews: 269.7K
Articles: 2
Comments: 18

Articles

Java Collection Performance
Learn more about Java collection performance in this post.
July 18, 2011
· 123,433 Views · 10 Likes
Programmatically Restart a Java Application
Today I'll talk about a famous problem : restarting a Java application. It is especially useful when changing the language of a GUI application, so that we need to restart it to reload the internationalized messages in the new language. Some look and feel also require to relaunch the application to be properly applied. A quick Google search give plenty answers using a simple : Runtime.getRuntime().exec("java -jar myApp.jar"); System.exit(0); This indeed basically works, but this answer that does not convince me for several reasons : 1) What about VM and program arguments ? (this one is secondary in fact, because can be solve it quite easily). 2) What if the main is not a jar (which is usually the case when launching from an IDE) ? 3) Most of all, what about the cleaning of the closing application ? For example if the application save some properties when closing, commit some stuffs etc. 4) We need to change the command line in the code source every time we change a parameter, the name of the jar, etc. Overall, something that works fine for some test, sandbox use, but not a generic and elegant way in my humble opinion. Ok, so my purpose here is to implement a method : public static void restartApplication(Runnable runBeforeRestart) throws IOException { ... } that could be wrapped in some jar library, and could be called, without any code modification, by any Java program, and by solving the 4 points raised previously. Let's start by looking at each point and find a way to answer them in an elegant way (let's say the most elegant way that I found). 1) How to get the program and VM arguments ? Pretty simple, by calling a : ManagementFactory.getRuntimeMXBean().getInputArguments(); Concerning the program arguments, the Java property sun.java.command we'll give us both the main class (or jar) and the program arguments, and both will be useful. String[] mainCommand = System.getProperty("sun.java.command").split(" "); 2) First retrieve the java bin executable given by the java.home property : String java = System.getProperty("java.home") + "/bin/java"; The simple case is when the application is launched from a jar. The jar name is given by a mainCommand[0], and it is in the current path, so we just have to append the application parameters mainCommand[1..n] with a -jar to get the command to execute : String cmd = java + vmArgsOneLine + "-jar " + new File(mainCommand[0]).getPath() + mainCommand[1..n]; We'll suppose here that the Manifest of the jar is well done, and we don't need to specify the main nor the classpath. Second case : when the application is launched from a class. In this case, we'll specify the class path and the main class : String cmd = java + vmArgsOneLine + -cp \"" + System.getProperty("java.class.path") + "\" " + mainCommand[0] + mainCommand[1..n]; 3) Third point, cleaning the old application before launching the new one. To do such a trick, we'll just execute the Runtime.getRuntime().exec(cmd) in a shutdown hook. This way, we'll be sure that everything will be properly clean up before creating the new application instance. Runtime.getRuntime().addShutdownHook(new Thread() { @Override public void run() { ... } }); Run the runBeforeRestart that contains some custom code that we want to be executed before restarting the application : if(runBeforeRestart != null) { runBeforeRestart.run(); } And finally, call the System.exit(0);. And we're done. Here is our generic method : /** * Sun property pointing the main class and its arguments. * Might not be defined on non Hotspot VM implementations. */ public static final String SUN_JAVA_COMMAND = "sun.java.command"; /** * Restart the current Java application * @param runBeforeRestart some custom code to be run before restarting * @throws IOException */ public static void restartApplication(Runnable runBeforeRestart) throws IOException { try { // java binary String java = System.getProperty("java.home") + "/bin/java"; // vm arguments List vmArguments = ManagementFactory.getRuntimeMXBean().getInputArguments(); StringBuffer vmArgsOneLine = new StringBuffer(); for (String arg : vmArguments) { // if it's the agent argument : we ignore it otherwise the // address of the old application and the new one will be in conflict if (!arg.contains("-agentlib")) { vmArgsOneLine.append(arg); vmArgsOneLine.append(" "); } } // init the command to execute, add the vm args final StringBuffer cmd = new StringBuffer("\"" + java + "\" " + vmArgsOneLine); // program main and program arguments String[] mainCommand = System.getProperty(SUN_JAVA_COMMAND).split(" "); // program main is a jar if (mainCommand[0].endsWith(".jar")) { // if it's a jar, add -jar mainJar cmd.append("-jar " + new File(mainCommand[0]).getPath()); } else { // else it's a .class, add the classpath and mainClass cmd.append("-cp \"" + System.getProperty("java.class.path") + "\" " + mainCommand[0]); } // finally add program arguments for (int i = 1; i < mainCommand.length; i++) { cmd.append(" "); cmd.append(mainCommand[i]); } // execute the command in a shutdown hook, to be sure that all the // resources have been disposed before restarting the application Runtime.getRuntime().addShutdownHook(new Thread() { @Override public void run() { try { Runtime.getRuntime().exec(cmd.toString()); } catch (IOException e) { e.printStackTrace(); } } }); // execute some custom code before restarting if (runBeforeRestart!= null) { runBeforeRestart.run(); } // exit System.exit(0); } catch (Exception e) { // something went wrong throw new IOException("Error while trying to restart the application", e); } } From : http://lewisleo.blogspot.jp/2012/08/programmatically-restart-java.html
July 6, 2011
· 135,963 Views · 2 Likes

Comments

Java Collection Performance

Jul 20, 2011 · James Sugrue

Mariusz,

Yes indeed, the equals() and hashCode() methods ot the contained objects will have an impact in the absolute results measured, especially this impact will be visible with a small number of elements in the collection.

But in the end, these two methods have a O(1) complexity, so for collection with let's say 10000+ elements, their impact will be negligeable compared to a loop on a List when searching for an element to remove for example. That's why in such case the HashSet will always have better performances than an ArrayList.

But please feal free to download and modify the benchmark code here.

You can pretty easily replace the String by some more complex object (regarding its equals/hashCode method) and see untill what population size these methods have a visible impact on the relative performances between Set and List for example.

Leo

Java Collection Performance

Jul 20, 2011 · James Sugrue

Mariusz,

Yes indeed, the equals() and hashCode() methods ot the contained objects will have an impact in the absolute results measured, especially this impact will be visible with a small number of elements in the collection.

But in the end, these two methods have a O(1) complexity, so for collection with let's say 10000+ elements, their impact will be negligeable compared to a loop on a List when searching for an element to remove for example. That's why in such case the HashSet will always have better performances than an ArrayList.

But please feal free to download and modify the benchmark code here.

You can pretty easily replace the String by some more complex object (regarding its equals/hashCode method) and see untill what population size these methods have a visible impact on the relative performances between Set and List for example.

Leo

Java Collection Performance

Jul 20, 2011 · James Sugrue

Mariusz,

Yes indeed, the equals() and hashCode() methods ot the contained objects will have an impact in the absolute results measured, especially this impact will be visible with a small number of elements in the collection.

But in the end, these two methods have a O(1) complexity, so for collection with let's say 10000+ elements, their impact will be negligeable compared to a loop on a List when searching for an element to remove for example. That's why in such case the HashSet will always have better performances than an ArrayList.

But please feal free to download and modify the benchmark code here.

You can pretty easily replace the String by some more complex object (regarding its equals/hashCode method) and see untill what population size these methods have a visible impact on the relative performances between Set and List for example.

Leo

Java Collection Performance

Jul 20, 2011 · James Sugrue

Mariusz,

Yes indeed, the equals() and hashCode() methods ot the contained objects will have an impact in the absolute results measured, especially this impact will be visible with a small number of elements in the collection.

But in the end, these two methods have a O(1) complexity, so for collection with let's say 10000+ elements, their impact will be negligeable compared to a loop on a List when searching for an element to remove for example. That's why in such case the HashSet will always have better performances than an ArrayList.

But please feal free to download and modify the benchmark code here.

You can pretty easily replace the String by some more complex object (regarding its equals/hashCode method) and see untill what population size these methods have a visible impact on the relative performances between Set and List for example.

Leo

Java Collection Performance

Jul 18, 2011 · James Sugrue

Artur,

Yes you are absolutely right. This combined list has poor performances with interleaved operations due to the re-population of internal collections.

Well, in fact, I first implemented this CombinedList for the model of a JTable.

In my case, operations were quite done in batch, add a lot of data, then add another set of data but before adding filter data that are already in the list (with the contains), then remove a range of data, set a range of Data, etc. And for this case it was a good compromise.

But for interleaved operations, another collection is a better choice indeed.

Java Collection Performance

Jul 18, 2011 · James Sugrue

Artur,

Yes you are absolutely right. This combined list has poor performances with interleaved operations due to the re-population of internal collections.

Well, in fact, I first implemented this CombinedList for the model of a JTable.

In my case, operations were quite done in batch, add a lot of data, then add another set of data but before adding filter data that are already in the list (with the contains), then remove a range of data, set a range of Data, etc. And for this case it was a good compromise.

But for interleaved operations, another collection is a better choice indeed.

Java Collection Performance

Jul 18, 2011 · James Sugrue

Artur,

Yes you are absolutely right. This combined list has poor performances with interleaved operations due to the re-population of internal collections.

Well, in fact, I first implemented this CombinedList for the model of a JTable.

In my case, operations were quite done in batch, add a lot of data, then add another set of data but before adding filter data that are already in the list (with the contains), then remove a range of data, set a range of Data, etc. And for this case it was a good compromise.

But for interleaved operations, another collection is a better choice indeed.

Java Collection Performance

Jul 18, 2011 · James Sugrue

Artur,

Yes you are absolutely right. This combined list has poor performances with interleaved operations due to the re-population of internal collections.

Well, in fact, I first implemented this CombinedList for the model of a JTable.

In my case, operations were quite done in batch, add a lot of data, then add another set of data but before adding filter data that are already in the list (with the contains), then remove a range of data, set a range of Data, etc. And for this case it was a good compromise.

But for interleaved operations, another collection is a better choice indeed.

Java Collection Performance

Jul 18, 2011 · James Sugrue

Thomas,

Very interesting articles about micro benchmark.

Indeed you are absolutely right about the fact that the use of nanoTime() is not really suitable here, and the fact that this benchmark might not be absolutely corrects, because of several factors that I have ignored.

But in fact, it's not really a big matter in my humble opinion. As I mentioned it, my purpose is not to have a complete benchmark, but just a global idea of collection performances. And that's why I used a timeout, and also why I presented results in charts, to compare them relatively, but not in some table, because the absolute value does not really interest me here.

It's very difficult to implement a good benchmark as it is said in the articles that you pointed, and for this reason it's sometimes difficult to find some benchmark results on the Internet, which is a shame in my opinion.

So here is an incomplete/imperfect/simplistic benchmark of Java collections, but which I hope give some global idea on this topic.

Java Collection Performance

Jul 18, 2011 · James Sugrue

Thomas,

Very interesting articles about micro benchmark.

Indeed you are absolutely right about the fact that the use of nanoTime() is not really suitable here, and the fact that this benchmark might not be absolutely corrects, because of several factors that I have ignored.

But in fact, it's not really a big matter in my humble opinion. As I mentioned it, my purpose is not to have a complete benchmark, but just a global idea of collection performances. And that's why I used a timeout, and also why I presented results in charts, to compare them relatively, but not in some table, because the absolute value does not really interest me here.

It's very difficult to implement a good benchmark as it is said in the articles that you pointed, and for this reason it's sometimes difficult to find some benchmark results on the Internet, which is a shame in my opinion.

So here is an incomplete/imperfect/simplistic benchmark of Java collections, but which I hope give some global idea on this topic.

Java Collection Performance

Jul 18, 2011 · James Sugrue

Thomas,

Very interesting articles about micro benchmark.

Indeed you are absolutely right about the fact that the use of nanoTime() is not really suitable here, and the fact that this benchmark might not be absolutely corrects, because of several factors that I have ignored.

But in fact, it's not really a big matter in my humble opinion. As I mentioned it, my purpose is not to have a complete benchmark, but just a global idea of collection performances. And that's why I used a timeout, and also why I presented results in charts, to compare them relatively, but not in some table, because the absolute value does not really interest me here.

It's very difficult to implement a good benchmark as it is said in the articles that you pointed, and for this reason it's sometimes difficult to find some benchmark results on the Internet, which is a shame in my opinion.

So here is an incomplete/imperfect/simplistic benchmark of Java collections, but which I hope give some global idea on this topic.

Java Collection Performance

Jul 18, 2011 · James Sugrue

Thomas,

Very interesting articles about micro benchmark.

Indeed you are absolutely right about the fact that the use of nanoTime() is not really suitable here, and the fact that this benchmark might not be absolutely corrects, because of several factors that I have ignored.

But in fact, it's not really a big matter in my humble opinion. As I mentioned it, my purpose is not to have a complete benchmark, but just a global idea of collection performances. And that's why I used a timeout, and also why I presented results in charts, to compare them relatively, but not in some table, because the absolute value does not really interest me here.

It's very difficult to implement a good benchmark as it is said in the articles that you pointed, and for this reason it's sometimes difficult to find some benchmark results on the Internet, which is a shame in my opinion.

So here is an incomplete/imperfect/simplistic benchmark of Java collections, but which I hope give some global idea on this topic.

Programmatically Restart a Java Application

Jul 07, 2011 · Leo Lewis

Anant,

Interesting question you have here.

Yes, you can command a JVM1 to restart from another JVM2 (or another process, not necessarily a JVM), by sending some signal to the JVM1 which will process the signal and call the restartApplication method.

We can think of sending this reboot signal by socket, or HTTP etc.

As the launch of the new JVM is done in a shutdownHook, you should not have some trouble of port already used by some SocketServer.

I made this experience with my Tomcat, I command it to restart itself from my web browser by calling this simple servlet (forgive me the fact that usually the SecurityManager should not permit a System.exit() to be called by a servlet).

public class RestartTomcatServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String query = req.getParameter("query"); if ("restart".equals(query)) { restartApplication(new Runnable() { @Override public void run() { System.out.println("Restarting the server ..."); } }); } else { resp.getWriter().println("Server uptime : " + (ManagementFactory.getRuntimeMXBean().getUptime() / 1000) + "s"); } } }

Start tomcat, wait a little bit, call my servlet with no argument :

Server uptime : 57s

Calling my servlet with ?query=restart argument :

Page is loading, refresh the page after a couple of seconds :

Server uptime : 8s

Leo

Programmatically Restart a Java Application

Jul 07, 2011 · Leo Lewis

Anant,

Interesting question you have here.

Yes, you can command a JVM1 to restart from another JVM2 (or another process, not necessarily a JVM), by sending some signal to the JVM1 which will process the signal and call the restartApplication method.

We can think of sending this reboot signal by socket, or HTTP etc.

As the launch of the new JVM is done in a shutdownHook, you should not have some trouble of port already used by some SocketServer.

I made this experience with my Tomcat, I command it to restart itself from my web browser by calling this simple servlet (forgive me the fact that usually the SecurityManager should not permit a System.exit() to be called by a servlet).

public class RestartTomcatServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String query = req.getParameter("query"); if ("restart".equals(query)) { restartApplication(new Runnable() { @Override public void run() { System.out.println("Restarting the server ..."); } }); } else { resp.getWriter().println("Server uptime : " + (ManagementFactory.getRuntimeMXBean().getUptime() / 1000) + "s"); } } }

Start tomcat, wait a little bit, call my servlet with no argument :

Server uptime : 57s

Calling my servlet with ?query=restart argument :

Page is loading, refresh the page after a couple of seconds :

Server uptime : 8s

Leo

Programmatically Restart a Java Application

Jul 06, 2011 · Leo Lewis

Thank you Dmirty,

In fact the quotes are probably not needed on most linux/unix installation, because the path to the java executable usually does not contain spaces " " characters (something like /usr/java/jdk.../bin/java).

But it is usually required on Windows where java is installed by default in C:\Program files\Java\jdk...\bin\java.

In this case, the command will failed if you miss the quotes.

Leo

Programmatically Restart a Java Application

Jul 06, 2011 · Leo Lewis

Thank you Dmirty,

In fact the quotes are probably not needed on most linux/unix installation, because the path to the java executable usually does not contain spaces " " characters (something like /usr/java/jdk.../bin/java).

But it is usually required on Windows where java is installed by default in C:\Program files\Java\jdk...\bin\java.

In this case, the command will failed if you miss the quotes.

Leo

Programmatically Restart a Java Application

Jul 06, 2011 · Leo Lewis

Thank you Andreas,

Indeed, you are totally right, the sun.java.command is the weak point of this method. I updated the article to set it constant in my code, and add some comments about it. It's a shame because it does exactly what I was looking for.

A workaround which would avoid to use it, would be to create a class RestartManager that would contain a singleton, our restartApplication() method and some method :

public void registerMain(Class<?> main, String[] args) { ... }

that would be called in the main method of the application, to keep somewhere in memory the main class and the launch arguments. We lost the simplicity of a single static method that do the all trick, but we gain in portability.

Regards,

Leo

Programmatically Restart a Java Application

Jul 06, 2011 · Leo Lewis

Thank you Andreas,

Indeed, you are totally right, the sun.java.command is the weak point of this method. I updated the article to set it constant in my code, and add some comments about it. It's a shame because it does exactly what I was looking for.

A workaround which would avoid to use it, would be to create a class RestartManager that would contain a singleton, our restartApplication() method and some method :

public void registerMain(Class<?> main, String[] args) { ... }

that would be called in the main method of the application, to keep somewhere in memory the main class and the launch arguments. We lost the simplicity of a single static method that do the all trick, but we gain in portability.

Regards,

Leo

User has been successfully modified

Failed to modify user

Let's be friends: