Skip to content

Instantly share code, notes, and snippets.

View melin's full-sized avatar

melin melin

  • 杭州
View GitHub Profile
import java.io.*;
import java.util.*;
import sun.jvm.hotspot.memory.*;
import sun.jvm.hotspot.oops.*;
import sun.jvm.hotspot.debugger.*;
import sun.jvm.hotspot.runtime.*;
import sun.jvm.hotspot.tools.*;
import sun.jvm.hotspot.utilities.*;
public class DirectMemorySize extends Tool {
@melin
melin / netty-handler.java
Created December 22, 2013 11:47
netty server client handler
ChannelInboundHandlerAdapter
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
System.out.println(ìServer received: ì + msg);
ctx.write(msg) #2
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) {
ctx.writeAndFlush(Unpooled.EMPTY_BUFFER)
.addListener(ChannelFutureListener.CLOSE); #3
1、ByteBuf支持同时读写操作,包含writerIndex和readerIndex两个变量,如果readerIndex值超过writerIndex,将会抛出IndexOutOfBoundsException错误。
2、ByteBuf支持head buffer(jvm堆中)和direct buffer,堆外分配buffer成本更高。
3、使用ByteBuffer实例:
ByteBuf heapBuf = ...;
if (heapBuf.hasArray()) { #1
byte[] array = heapBuf.array(); #2
int offset = heapBuf.arrayOffset() + heapBuf.position(); #3
int length = heapBuf.readableBytes(); #4
YourImpl.method(array, offset, length); #5
}
@melin
melin / httpclient.java
Last active July 3, 2023 08:08
httpclient
1. Send HTTP GET Request
String url = "http://www.google.com/search?q=httpClient";
HttpClient client = HttpClientBuilder.create().build();
HttpGet request = new HttpGet(url);
// add request header
request.addHeader("User-Agent", USER_AGENT);
HttpResponse response = client.execute(request);
@melin
melin / TestCompletableFuture.java
Created September 24, 2014 13:50
TestCompletableFuture
package com.github.melin.concurrnt.future;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
@melin
melin / queue.java
Created October 4, 2014 02:32
基于信号量、condition、object wait notify三种方式的实现的简易队列(demo)
public class ArrayQueueSemaphore implements Queue {
private final Semaphore notFull = new Semaphore(10);
private final Semaphore notEmpty = new Semaphore(0);
private final ReentrantLock lock = new ReentrantLock();
private final String[] connections = new String[10];
int putIndex, takeIndex, count;
public void put(String e) throws InterruptedException {
notFull.acquire();
@melin
melin / btrace.txt
Created November 21, 2014 01:06
btrace script
/* BTrace Script Template */
import com.sun.btrace.annotations.*;
import static com.sun.btrace.BTraceUtils.*;
import java.lang.reflect.Field;
@BTrace
public class TracingScript {
/*
* 获取方参数、返回值信息;获取方法调用时间
##服务代码
```python
#!/usr/bin/env python2
#coding=utf-8
import tornado.ioloop
import tornado.web
from tornado.escape import json_encode
@melin
melin / docker-registry-mirrors.md
Created June 20, 2024 13:39 — forked from y0ngb1n/docker-registry-mirrors.md
国内的 Docker Hub 镜像加速器,由国内教育机构与各大云服务商提供的镜像加速服务 | Dockerized 实践 https://github.com/y0ngb1n/dockerized