Reactor网络模型

Reactor模型是同步I/O事件处理的一种常见模型,其核心思想:将关注的I/O事件注册到多路复用器上,一旦有I/O事件触发,将事件分发到事件处理器中,执行就绪I/O事件对应的处理函数中。

Algorithm Note

一些算法归纳:

排序

快速排序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void quickSort(int[] a, int start, int end) {
if (start >= end) return;

int low = start, high = end + 1;
int pivot = a[low];
while (low < high) {
while (low < high && a[--high] > pivot) ;
a[low] = a[high];
while (low < high && a[++low] < pivot) ;
a[high] = a[low];
}

a[low] = pivot;

quickSort(a, start, low - 1);
quickSort(a, low + 1, end);
}

归并排序

自顶向下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
mergeSort(int[] a,int low,int high){
if(high<=low) return;

int mid=low+(high-low)/2;
mergeSort(a,low,mid);
mergeSort(a,mid+1,high);
merge(a,low,mid,high);
}

merge(int[] a,int low,int mid,int high){
//归并排序需要一个辅助数组
System.arraycopy(a,low,aux,low,high-low+1);
int i=low,j=mid+1;
for(int k=low;k<=high;k++){
if(i>mid) a[k]=aux[j++];
else if(j>high) a[k]=aux[i++];
else if(aux[j]<aux[i]) a[k]=aux[j++];
else a[k]=aux[i++];
}
}

Netty新连接接入过程

Netty的NioEventLoop线程中的循环可以分为以下几个步骤:

  1. 轮询注册在selector上的IO事件(select)
  2. 处理IO事件(processSelectedKey,unsafe.read() 开启事件传播)
  3. 执行异步task (runAllTasks)

Spring框架的一些理解

Java Annotation

对于注解而言,若没有相应处理器,那它和注释没什么区别。

Java注解示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// 声明注解使用的地方。 这里是方法级。
@Target(ElementType.METHOD)

//声明注解的生命时长。 这里是在运行时被虚拟机获得,所以可以使用反射来读。
@Retention(RetentionPolicy.RUNTIME)
public @interface UseCase{
int id();
String description() default "no description";
}

class A{
@UseCase(id=1,description="......")
void methodA{...}

....
}

class UseCaseTracker{
public static void trackUseCases(List<Integer> useCases, Class<?> cl){
for(Method m: cl.getDeclaredMethods()){
UseCase uc=m.getAnnonation(UseCase.class);
if(uc!=null){
print(uc.id(), uc.description());
}
}
}
}

DynamicProxy

关键代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
interface Interface{
void doSth(String s);
}

class RealObject implments Interface {
void doSth(String s){
//...
}
}

class DynamicProxyHandler implments InvocationHandler {
private Object proxied;
public DynamicProxyHandler(Object proxied) {
this.proxied = proxied;
}

public Object invoke(Object proxy, Method m, Object[] args){
/*
在此插入代理逻辑
*/
return m.invoke(proxied, args);
}
}


public class SimpleDynamicProxy {
Interface proxy = (Interface) Proxy.newInstance(
Interface.class.getClassLoader(), //一般传这个已加载的类加载器就行
new Class[]{Interface.class}, //实现的接口
new DynamicProxyHandler(new RealObject())
);

proxy.doSth();
}

Spark API in Depth

《Spark in Action》Chapter4

个人总结:

  • DStream <- a seq of RDDs <- Partitions
  • key-value RDD = pair RDD manipulated by funtionByKey()

Java8 Learn

行为参数化

  • 行为参数化,就是一个方法接受多个不同的行为作为参数,并在内部使用它们,完成不同行为的能力。
  • 行为参数化让代码更好地适应不断变化的要求,减轻未来的工作量。
  • 传递代码,就是将新行为作为参数传递给方法。Java API中包括排序、线程和GUI处理。