netty client怎么处理超时
快排 O(nLogn)
public static void quickSort(int[] arr, int low, int high) {
if (low < high) {
int pivot = getMiddle(arr, low, high);
quickSort(arr, low, pivot-1);
quickSort(arr, pivot+1, high);
}
}
public static int getMiddle(int[] arr, int low, int high) {
int pivot = arr[low];
while (low < high) {
while ((low<high) && (arr[high]>=pivot)) {
high--;
}
arr[low] = arr[high];
while ((low<high) && (arr[low]<=pivot)) {
low++;
}
arr[high] = arr[low];
}
arr[low] = pivot;
return low;
}
反射实现代理
反射最大的作用就在于我们可以不在编译时知道某个对象的类型,而在运行时得到。同时我们只需要得到我们想得到的类的名字即可(如果不在一个包,必须写完整的名字包括包名)。
代理可以帮助我们进行很好的封装,使底层的代码能够有效的隐藏起来。动态代理是动态的创建代理和动态的处理方法的,这也是反射的一个重要体现之处。
动态代理类:实现InvocationHandler接口
import java.lang.reflect.*;
public class Main {
static void customer(ProxyInterface pi){
pi.say();
}
public static void main(String[] args){
RealObject real = new RealObject();
ProxyInterface proxy = (ProxyInterface)Proxy.newProxyInstance(ProxyInterface.class.getClassLoader(),new Class[]{ProxyInterface.class}, new ProxyObject(real));
customer(proxy);
}
}
interface ProxyInterface{
void say();
}
//被代理类
class RealObject implements ProxyInterface{
public void say(){
System.out.println("i'm talking");
}
}
//代理类,实现InvocationHandler 接口
class ProxyObject implements InvocationHandler{
private Object proxied = null;
public ProxyObject(){
}
public ProxyObject(Object proxied){
this.proxied = proxied;
}
public Object invoke(Object arg0, Method arg1, Object[] arg2) throws Throwable {
System.out.println("hello");
return arg1.invoke(proxied, arg2);
};
}
一致性hash
- 首先求出服务器节点的哈希值,并将其配置到0~232的圆(continuum)上。
- 然后采用同样的方法求出存储数据的键的哈希值,并映射到相同的圆上。
- 然后从数据映射到的位置开始顺时针查找,将数据保存到找到的第一个服务器上。如果超过232仍然找不到服务器,就会保存到第一台服务器上。
挂了一台剩下的怎么hash?
一致性哈希算法对于节点的增减都只需重定位环空间中的一小部分数据,具有较好的容错性和可扩展性。
节点少数据倾斜怎么办?
为了解决这种数据倾斜问题,一致性哈希算法引入了虚拟节点机制,即对每一个服务节点计算多个哈希,每个计算结果位置都放置一个此服务节点,称为虚拟节点。在实际应用中,通常将虚拟节点数设置为32甚至更大,因此即使很少的服务节点也能做到相对均匀的数据分布。
快排
红黑树