Skip to content

Instantly share code, notes, and snippets.

View hengyunabc's full-sized avatar

hengyunabc hengyunabc

View GitHub Profile
@hengyunabc
hengyunabc / ArthasSecurityManager.java
Created August 14, 2019 07:48
打印所有的权限的SecurityManager
package aa;
import java.io.FileDescriptor;
import java.security.Permission;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ArthasSecurityManager extends SecurityManager {
@hengyunabc
hengyunabc / info.md
Last active May 31, 2018 04:39
ConcurrentHashMap NoSuchMethodError

当用jdk8编绎,生成target 为 jdk6/7 的代码时,如果有使用到了类似的代码,就会抛出异常

ConcurrentHashMap<String, String> map = new ConcurrentHashMap<String, String>();
map.keySet();

抛出

@hengyunabc
hengyunabc / TestBenchMarks.java
Last active May 3, 2018 07:39
测试利用if + switch来提高分支预测准确性,对比纯switch的效率
import java.util.Date;
import java.util.Random;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Scope;
@hengyunabc
hengyunabc / Test.java
Created April 3, 2018 06:38
jdk8里的 Repeatable annotation的实现,反编绎可以看到
import java.lang.annotation.Annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.Arrays;
import abc.Test.AAA;
@hengyunabc
hengyunabc / ThreadId.java
Created February 6, 2018 08:24
一个ThreadLocal 在不同ClassLoader下失效的问题
import java.util.concurrent.atomic.AtomicInteger;
public class ThreadId {
// Atomic integer containing the next thread ID to be assigned
private static final AtomicInteger nextId = new AtomicInteger(0);
// Thread local variable containing each thread's ID
private static final ThreadLocal<Integer> threadId = new ThreadLocal<Integer>() {
@Override
protected Integer initialValue() {
@hengyunabc
hengyunabc / CDump.java
Created January 16, 2018 09:32
java asm bytecode. jsr instruction sample.
import java.util.*;
import org.objectweb.asm.*;
public class CDump implements Opcodes {
public static byte[] dump() throws Exception {
ClassWriter cw = new ClassWriter(0);
FieldVisitor fv;
MethodVisitor mv;
@hengyunabc
hengyunabc / ClassLoaderUtils.java
Created October 12, 2017 12:02
Get SystemClassLoader/ClassLoader urls in java9
/**
*
* @author hengyunabc 2017-10-12
*
*/
public class ClassLoaderUtils {
@SuppressWarnings({ "restriction", "unchecked" })
public static URL[] getUrls(ClassLoader classLoader) {
if (classLoader instanceof URLClassLoader) {
return ((URLClassLoader) classLoader).getURLs();
@hengyunabc
hengyunabc / Test.java
Created August 22, 2017 06:10
java get fd count. openFileDescriptorCount / maxFileDescriptorCount
import java.lang.management.ManagementFactory;
import java.lang.management.OperatingSystemMXBean;
import com.sun.management.UnixOperatingSystemMXBean;
public class Test {
public static void main(String[] args) {
OperatingSystemMXBean operatingSystemMXBean = ManagementFactory.getOperatingSystemMXBean();
if (operatingSystemMXBean instanceof UnixOperatingSystemMXBean) {
import java.security.SecureRandom;
public class PenneyGame {
static SecureRandom random = new SecureRandom();
public static void main(String[] args) {
int count = 100;
int winCount = 0;
for (int i = 0; i < 100; ++i) {
@hengyunabc
hengyunabc / MatchUtils.java
Created February 16, 2016 06:41
Java wildcard matching utils. Support ? * .
import java.util.ArrayList;
import java.util.Stack;
/**
* from org.apache.commons.io.FilenameUtils
*
* @author hengyunabc
*
*/
public class MatchUtils {