Recent Changes - Search:
Thanks to OZU Thanks to OZU
Linked In

links

Reviews

Admin

edit SideBar

Java

Tracing

Stack trace:

jstack <pid>

Heap dump (if hung)

jmap -dump:format=b <pid>

Threadsafe tips:

  1. Immutable objects are automatically thread-safe, and objects that are not shared across threads do not need to be thread-safe. Therefore share as little as possible and favor immutability wherever possible.
  2. When objects really must be shared and mutable, guard all accesses (both read and write) to shared fields with a lock on the same object, or make appropriate use of volatile variables.
  3. Avoid acquiring new locks when holding an existing lock. As a direct consequence of this rule, we must avoid holding any locks when calling unknown or “foreign” code that might attempt to acquire a lock. This includes calls to services or to OSGi? APIs?, many of which can result in callbacks to other bundles that execute in our thread.

logging

#include org.apache.log4j.Logger;

public class Bla() {
  private Logger log;

  public Bla() {
    log = Logger.getLogger(Bla.class);
  }
  public someMethod() {
    if (log.isDebugEnabled()) {
       log.debug("haha");
    }
    log.warn("warning");
  }
}

starting a java process from java

    private void startInJava(long eventId, boolean wait) {

        String ini = System.getProperty("borland.copyenv.bos.inifile");
        String user = System.getProperty("borland.copyenv.bos.username");
        String pass = System.getProperty("borland.copyenv.bos.password");

        String classPath = System.getProperty("java.class.path");
        String javaHome = System.getProperty("java.home");
        String javacHome = javaHome.replaceAll("Program Files", "PROGRA~1").replaceAll("jre", "bin");

        if (log.isDebugEnabled()) {
            log.debug("Starting the in running manager - Java mode");
            log.debug("ini param = " + ini);
            log.debug("user = " + user);
            log.debug("EventId = " + eventId);
            log.debug("javaHome = " + javaHome);
            log.debug("CLASSPATH = " + classPath);
        }

        String strEventId = "" + eventId;

        /* TODO : check that OS is XP */

        String[] cmd = { "cmd.exe", "/C", javacHome + File.separator + "java", "-verbose", "-classpath", classPath, "-Dborland.copyenv.bos.inifile=" + ini, "-Dborland.copyenv.bos.username=" + user, "-Dborland.copyenv.bos.password=" + pass, "com.vcint.inrunning.gui.InRunningClient", strEventId };

        Runtime r = Runtime.getRuntime();
        Process proc = null;
        try {
            proc = r.exec(cmd);
            startGobblers(proc);
            if (wait) {
                // this logs the processes output
                int exitVal = proc.waitFor();

            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            if (wait) {
                try {
                    proc.getInputStream().close();
                    proc.getOutputStream().close();
                    proc.getErrorStream().close();
                } catch (Exception e) {
                    // ignore
                }
            }
        }
    } // startInJava()

Edit - History - Print - Recent Changes - Search
Page last modified on March 25, 2010, at 04:40 PM