Skip to content

Instantly share code, notes, and snippets.

View searover's full-sized avatar
💭
I may be slow to respond.

XuZhi Lu searover

💭
I may be slow to respond.
View GitHub Profile
class GuardedObject<T> {
// 受保护的对象
T obj;
final Lock lock = new ReentrantLock();
final Condition done = lock.newCondition();
final int timeout = 1;
T get(Predicate<T> p) {
lock.lock();
try{
/**
* @author luxz
* @date 2020/6/27-3:31 AM
* 如果执行乐观读操作期间,存在写操作,会把乐观读升级为悲观读锁
*/
public class Point {
private int x, y;
final StampedLock sl = new StampedLock();
int distanceFromOrigin() {
class CacheData {
Object data;
volatile boolean isCacheValid;
final ReadWriteLock rwl = new ReentrantReadWriteLock();
// 读锁
final Lock r = rwl.readLock();
// 写锁
final Lock w = rwl.writeLock();
void processCacheData() {
class VolatileExample {
int x = 0;
volatile boolean v = false;
void writer() {
x = 42;
v = true;
}
void reader() {
public class LinkedStack<T> {
private static class Node<U> {
U item;
Node<U> next;
Node(){
item = null;
next = null;
}
Node(U item, Node<U> next){
this.item = item;
@searover
searover / TrackCounter.java
Last active September 26, 2015 07:39
Using parameterized advice to count how many times a track is played
package soundsystem;
import java.util.HashMap;
import java.util.Map;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
@Aspect
public class TrackCounter{
package com.searover.hctest;
import org.apache.commons.lang.time.StopWatch;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
@searover
searover / NetworkHelper.java
Created July 22, 2015 11:27
The HTTP Get and Post request method based on Apache HttpClient
package com.anchnet.ddos.report.utils;
import com.anchnet.ddos.report.beans.MessagePack;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import java.io.IOException;
@searover
searover / TypedArray.java
Created April 12, 2015 10:49
TypedArray Class
/**
* Container for an array of values that were retrieved with
* {@link Resources.Theme#obtainStyledAttributes(AttributeSet, int[], int, int)}
* or {@link Resources#obtainAttributes}. Be
* sure to call {@link #recycle} when done with them.
*
* The indices used to retrieve values from this structure correspond to
* the positions of the attributes given to obtainStyledAttributes.
*/
public class TypedArray {
@searover
searover / obtainStyledAttributes.java
Last active August 29, 2015 14:19
Resources obtainStyledAttributes function
public TypedArray obtainStyledAttributes(AttributeSet set,
int[] attrs, int defStyleAttr, int defStyleRes) {
final int len = attrs.length;
final TypedArray array = TypedArray.obtain(Resources.this, len);
// other code .....
return array;
}