`

Java Advance

    博客分类:
  • Java
阅读更多

1. A class with a given name can only be loaded once by a given classloader.

 

2. When the JVM is started, three class loaders are used:

    1.Bootstrap class loader - loads the core Java libraries(<JAVA_HOME>/lib directory). This class loader, which is part of the core JVM, is writtern in native code.

    2.Extensions class loader - loads the code in the extensions directories(<JAVA_HOME>/lib/ext or any other directory specified by the java.ext.dirs system property). It's implemented by the sun.misc.Launcher$ExtClassLoader class.

    3.System class loader - loads code found on java.class.path, which maps to the system CLASSPATH variable. This is implemented by the sun.misc.Launcher$AppClassLoader class.

 

2. Thread

Thread.yield(); -- you are suggesting that otehr threads with the same priority might be run.

Thread.sleep(); -- throws InterruptedException

TimeUnit.MILLISECONDS.sleep(100); /j2se5 or 6

Thread.currentThread();

thread1.join(); -- the main thread waits thread1 task finish. The call to join() may be aborted by calling interrupt() on the calling thread.

thread1.interrupt(); -- the main thread will go on process since the thread1 has been interruptted.

wait(),notify(),notifyAll() - should define in 'synchronized' method or block, otherwise, IllegalMonitorStateException will throw in runtime.

 

synchronized(x) { 

  x.notifyAll(); 

}

 

3.ArrayList vs. Vector

Collection

©ÀList     ----- sortable

©¦©ÀLinkedList

©¦©ÀArrayList  --- expand 50%, non-thread-safe

©¦©¸Vector  --- expand 100%, thread-safe

©¦¡¡©¸Stack

©¸Set     ------ no-sortable, no-duplicate

Map

©ÀHashtable

©ÀHashMap

©¸WeakHashMap  --- if the key object is not referenced by outside, then the entry will be GCed.

 

4.Reflection:

Class<String> clz = (Class<String>) Class.forName("java.lang.String");

System.out.println("getTypeParameters():");

for(TypeVariable tv :clz.getTypeParameters()) {

    System.out.println(tv);

}

System.out.println("getSuperClass():");

System.out.println(clz.getSuperclass().getCanonicalName());

 

System.out.println("clz.getModifiers():");

System.out.println(Modifier.toString(clz.getModifiers()));

 

 

System.out.println("getInterfaces():");

for(Class c : clz.getInterfaces()) {

    System.out.println(c.getCanonicalName());

}

 

5.Regex:

jpql.replaceFirst("^(?i)SELECT ((?!FROM).)* FROM", "SELECT COUNT(r.id) FROM");

 

6.Keystore:

    1. gen a keystore file:

        keytool -genkey -alias jetty -keyalg RSA -keystore my.keystore

 

7.Java Locale:

append -J-Duser.country=US to any java command.

or -J-Duser.language=en

 

to find all java system properties:

for(Map.Entry<Object, Object> s : System.getProperties().entrySet()) {

    System.out.println(s.getKey() + "=" + s.getValue());

}

 

e.g.: keytool -genkey -alias jetty -keyalg RSA -keystore .keystore -J-Duser.language=en

 

8. compile/run java in shell:

$javac com/asran/HelloWorld.java

$java com.asran.HelloWorld

 

9. join list as string with comma separated:

org.apache.commons.lang.StringUtils.join(list, ",");

 

10. -javaagent:***-agent.jar

MySimpleTransformer.java

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

package com.asran.javagent;

import java.lang.instrument.ClassFileTransformer;

import java.lang.instrument.IllegalClassFormatException;

import java.security.ProtectionDomain;

 

public class MySimpleTransformer implements ClassFileTransformer {

    public byte[] transform(ClassLoader classloader,

                                String classname,

                                Class redefinedclass,

                                ProtectionDomain protectiondomain,

                               byte b[]) throws IllegalClassFormatException {

        if (!classname.endsWith("HelloWorld"))

            return (null);

 

        String line = "";

        for (int i = 0; i < b.length; i++) {

            line += Byte.toString(b[i]) + " ";

            if (line.length() > 60) {

                System.out.println(line);

                line = "";

            }

            if (b[i] == (byte) '6')

                b[i] = (byte) '7';

        }

        System.out.println(line);

        System.out.println("The number of bytes in HelloWorld: " + b.length);

        return (b);

    }

}

 

MySimpleAgent.java

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

package com.asran.javagent;

import java.lang.instrument.Instrumentation;

 

public class MySimpleAgent {

    public static void premain(String agentArgs, Instrumentation inst) {

        inst.addTransformer(new MySimpleTransformer());

    }

}

 

HelloWorld.java

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

package com.asran.javagent;

 

public class HelloWorld {

    public static void main(String arg[]) {

        System.out.println("The number six is 6");

    }

}

 

MyManifest.mf

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

Premain-Class: com.asran.javagent.MySimpleAgent

(carriage return[CR] is mandantory)

 

Test:

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

>jar cvfm MyAgent.jar MyManifest.mf com/asran/javagent/MySimpleAgent.class com/asran/javagent/MySimpleTransformer.class

>java -javaagent:MyAgent.jar com.asran.javagent.HelloWorld

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics