Skip to content

Instantly share code, notes, and snippets.

import java.io.IOException;
import java.lang.reflect.Field;
import java.util.HashSet;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
/**
* -XX:+PrintGCDetails
* jcmd PID GC.run 强制来一发GC.
*/
@liufor
liufor / jcstress-tests-index
Last active December 6, 2018 02:02
jcstress tests for index.
# JCStress repo, version 0.5
hg clone http://hg.openjdk.java.net/code-tools/jcstress/ jcstress
cd jcstress
hg up 0.5
# 1) jcstress-sample
# source code: http://hg.openjdk.java.net/code-tools/jcstress/file/3c94746a6abf/jcstress-samples/src/main/java/org/openjdk/jcstress/samples
# JMM & Concurrent samples also on: https://gist.github.com/liufor/f08495f0596ddf98a3542307b72f080f
# maven: https://mvnrepository.com/artifact/org.openjdk.jcstress/jcstress-samples
@liufor
liufor / ConcurrencySample_01_OperationAtomicity.java
Last active May 24, 2020 10:35
JCStress samples (0.5, 包含VarHandle)中包含的例子对理解并发和JMM大有帮助.
/*
* Copyright (c) 2016, Red Hat Inc.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
/**
* 简单例子验证 volatile 的,不用JCStress.
*/
public class SyncDeadLock {
public static void main(String[] args) {
DataRace dataRace = new DataRace();
Thread[] threads = new Thread[100];
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Inherited
@Retention(RetentionPolicy.RUNTIME)
public @interface AnnotationX {
}
@liufor
liufor / Sync.java
Last active November 23, 2018 08:40
Java Thread State 转换和 monitor lock获取/释放 并不是原子的。1)先获取到monitor,再转换线程状态BLOCKED->RUNNABLE. 或者 2)先转换线程状态RUNNABLE,再释放monitor 。 如果jstack执行时机在这之间的话,stack dump就会出现 "monitor已经获取了,但线程状态还是BLOCKED" 的现象.
public class Sync implements Runnable {
int n;
public static void main(String[] args) {
Sync sync = new Sync();
new Thread(sync, "Sync1").start();
new Thread(sync, "Sync2").start();
new Thread(sync, "Sync3").start();
}
import io.undertow.*;
import io.undertow.server.*;
import io.undertow.util.*;
public class HelloWorldServer {
public static void main(final String[] args) {
Undertow server = Undertow.builder()
.addHttpListener(8080, "localhost")
.setHandler(new HttpHandler() {
import jdk.nashorn.internal.runtime.ECMAException;
import sun.misc.Launcher;
import sun.misc.URLClassPath;
import java.lang.reflect.Field;
import java.net.URL;
import java.net.URLClassLoader;
/**
* 简单打印出 Boot、System、Ext 3个 classloader 的 对应的 classpath 的 URLs.
public class LocalVariablesPrimitiveTypeLiteralBytecodeDemo {
public static void main(String[] args) throws InterruptedException {
boolean booltrue = true; //ICONST_1
boolean boolfalse = false; //ICONST_0
//byte、short、char、int 局部变量根据值大小不同而由不同的
//byte 范围 [-128,127], ICONST_X 、BIPUSH
data 1879
database 1245
system 1196
write 976
node 751
page 744
example 663
time 600
need 586
read 566