final class WeakCache {
//引用队列,这个结构体一般是和弱引用相关的,在这里就不详细说明了
private final ReferenceQueue refQueue = new ReferenceQueue<>();
// the key type is Object for supporting null key
//可以看出,一个Map嵌套了另一个Map,我们可以将其看作是二级缓存
//就是说可以通过一个key获得一个Map对象,然后再利用subkey获得我们真正想要的对象
//ConcurrentMap是一个线程安全的Map,关于其介绍可以参考xxx(还未写,以后补上)
private final ConcurrentMap
private final ConcurrentMap, Boolean> reverseMap
= new ConcurrentHashMap<>();
//BiFunction 这是一个函数式接口, 只有一个apply方法,用于接收两个参数,返回一个值
private final BiFunction subKeyFactory;
private final BiFunction valueFactory;
//要求传入的参数是BiFunction引用类型的,传入的实参是 new KeyFactory(), new ProxyClassFactory();我们将在后面介绍这两个类
public WeakCache(BiFunction subKeyFactory,
BiFunction valueFactory) {
//非空要求
this.subKeyFactory = Objects.requireNonNull(subKeyFactory);
this.valueFactory = Objects.requireNonNull(valueFactory);
}
//关键就在于这个方法,我们说的一级缓存的key其实是ClassLoader,二级缓存的subkey是实现的接口的Class对象数组,需要的值为代理类的Class对象
//getProxyClass0调用语句为return proxyClassCache.get(loader, interfaces),可以看到key为ClassLoader,parameter为接口的Class对象数组
public V get(K key, P parameter) {
Objects.requireNonNull(parameter);
expungeStaleEntries();
//将key也就是ClassLoader用弱引用缓存起来,得到一级缓存的cacheKey
Object cacheKey = CacheKey.valueOf(key, refQueue);
// lazily install the 2nd level valuesMap for the particular cacheKey
//由一级缓存的cacheKey来获得二级缓存的对象(又是一个Map对象,其中有subkey和我们需要的代理类的Classd对象)
//Supplier也是一个函数式接口,有一个get方法,表示不需要参数,返回一个V类型的值,其实就是对代理类的Classd对象的包装
ConcurrentMap
private final class Factory implements Supplier {
private final K key;
private final P parameter;
private final Object subKey;
private final ConcurrentMap> valuesMap;
Factory(K key, P parameter, Object subKey,
ConcurrentMap> valuesMap) {
this.key = key; //是传入的loader
this.parameter = parameter; //是传入的接口的Class对象数组
this.subKey = subKey; //与传入的接口关联的弱引用对象
this.valuesMap = valuesMap; //二级缓存对象
}
@Override
public synchronized V get() { // serialize access
// re-check
//再次检查与subkey关联的Supplier是否为空,防止在多线程的情况下发生错误
Supplier supplier = valuesMap.get(subKey);
if (supplier != this) {
// something changed while we were waiting:
// might be that we were replaced by a CacheValue
// or were removed because of failure ->
// return null to signal WeakCache.get() to retry
// the loop
return null;
}
// else still us (supplier == this)
// create new value
V value = null;
try {
//关键:调用了valueFactory的apply方法,这个方法会返回我们想要的代理类的Class对象
value = Objects.requireNonNull(valueFactory.apply(key, parameter));
} finally {
if (value == null) { // remove us on failure
//创建失败就从二级缓存中移除
valuesMap.remove(subKey, this);
}
}
// the only path to reach here is with non-null value
assert value != null;
// wrap value with CacheValue (WeakReference)
CacheValue cacheValue = new CacheValue<>(value);
// put into reverseMap
reverseMap.put(cacheValue, Boolean.TRUE);
// try replacing us with CacheValue (this should always succeed)
if (!valuesMap.replace(subKey, this, cacheValue)) {
throw new AssertionError("Should not reach here");
}
// successfully replaced us with new CacheValue -> return the value
// wrapped by it
return value;
}
}
private static final class ProxyClassFactory
implements BiFunction[], Class>>
{
// 生成代理类前缀
private static final String proxyClassNamePrefix = "$Proxy";
// next number to use for generation of unique proxy class names
private static final AtomicLong nextUniqueNumber = new AtomicLong();
@Override
public Class> apply(ClassLoader loader, Class>[] interfaces) {
//IdentityHashMap是一种用==而不是用equals比较键值的映射表
Map, Boolean> interfaceSet = new IdentityHashMap<>(interfaces.length);
//循环处理给出的接口
for (Class> intf : interfaces) {
/*
* Verify that the class loader resolves the name of this
* interface to the same Class object.
*/
//验证用给定的类加载器通过接口名字解析给出的Class对象与传入的接口Class对象是否相同
Class> interfaceClass = null;
try {
interfaceClass = Class.forName(intf.getName(), false, loader);
} catch (ClassNotFoundException e) {
}
if (interfaceClass != intf) {
throw new IllegalArgumentException(
intf + " is not visible from class loader");
}
/*
* Verify that the Class object actually represents an
* interface.
*/
//验证给出的接口Class对象数组中的每个元素确实是接口的Class对象
if (!interfaceClass.isInterface()) {
throw new IllegalArgumentException(
interfaceClass.getName() + " is not an interface");
}
/*
* Verify that this interface is not a duplicate.
*/
//验证给出的接口Class对象不重复
if (interfaceSet.put(interfaceClass, Boolean.TRUE) != null) {
throw new IllegalArgumentException(
"repeated interface: " + interfaceClass.getName());
}
}
//定义代理对象所在的包
String proxyPkg = null; // package to define proxy class in
//重要:可以看到修饰符被设置为public和final
int accessFlags = Modifier.PUBLIC | Modifier.FINAL;
/*
* Record the package of a non-public proxy interface so that the
* proxy class will be defined in the same package. Verify that
* all non-public proxy interfaces are in the same package.
*/
//重要:记录下修饰符不是public的接口所在的包并且验证是否所有的修饰符不为public的接口都处于同一个包中,
for (Class> intf : interfaces) {
int flags = intf.getModifiers();
if (!Modifier.isPublic(flags)) {
accessFlags = Modifier.FINAL;
String name = intf.getName();
int n = name.lastIndexOf('.');
String pkg = ((n == -1) ? "" : name.substring(0, n + 1));
if (proxyPkg == null) {
proxyPkg = pkg;
} else if (!pkg.equals(proxyPkg)) {
throw new IllegalArgumentException(
"non-public interfaces from different packages");
}
}
}
//如果所有的接口都是public修饰符,则代理类所在的包为com.sun.proxy
if (proxyPkg == null) {
// if no non-public proxy interfaces, use com.sun.proxy package
proxyPkg = ReflectUtil.PROXY_PACKAGE + ".";
}
/*
* Choose a name for the proxy class to generate.
*/
//生成代理类的完整名字
long num = nextUniqueNumber.getAndIncrement();
String proxyName = proxyPkg + proxyClassNamePrefix + num;
/*
* Generate the specified proxy class.
*/
//关键:生成表示代理类的Class对象的二进制byte[] proxyClassFile = ProxyGenerator.generateProxyClass(
proxyName, interfaces, accessFlags);
try {
//defineClass0是一个本地方法,可以看出它是将字节数组生成代理类的Class对象,返回值就是我们需要的代理类Class对象
return defineClass0(loader, proxyName,
proxyClassFile, 0, proxyClassFile.length);
} catch (ClassFormatError e) {
/*
* A ClassFormatError here means that (barring bugs in the
* proxy class generation code) there was some other
* invalid aspect of the arguments supplied to the proxy
* class creation (such as virtual machine limitations
* exceeded).
*/
throw new IllegalArgumentException(e.toString());
}
}
}
private static final class KeyFactory
implements BiFunction[], Object>
{
@Override
public Object apply(ClassLoader classLoader, Class>[] interfaces) {
switch (interfaces.length) {
case 1: return new Key1(interfaces[0]); // the most frequent
case 2: return new Key2(interfaces[0], interfaces[1]);
case 0: return key0;
default: return new KeyX(interfaces);
}
}
}
我们选取最常用的Key1看看里面是什么
/*
* a key used for proxy class with 1 implemented interface
*/
private static final class Key1 extends WeakReference> {
private final int hash;
Key1(Class> intf) {
super(intf);
this.hash = intf.hashCode();
}
@Override
public int hashCode() {
return hash;
}
@Override
public boolean equals(Object obj) {
Class> intf;
return this == obj ||
obj != null &&
obj.getClass() == Key1.class &&
(intf = get()) != null &&
intf == ((Key1) obj).get();
}
}
现实生活中,有些工作是需要团队中成员依次完成的,这就涉及到了一个顺序问题。现在有T1、T2、T3三个工人,如何保证T2在T1执行完后执行,T3在T2执行完后执行?问题分析:首先问题中有三个实体,T1、T2、T3, 因为是多线程编程,所以都要设计成线程类。关键是怎么保证线程能依次执行完呢?
Java实现过程如下:
public class T1 implements Runnabl
hive在使用having count()是,不支持去重计数
hive (default)> select imei from t_test_phonenum where ds=20150701 group by imei having count(distinct phone_num)>1 limit 10;
FAILED: SemanticExcep
转载源:http://blog.sina.com.cn/s/blog_4f925fc30100rx5l.html
mysql -uroot -p
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
[root@centos var]# service mysql
#mq
xdr.mq.url=tcp://192.168.100.15:61618;
import java.io.IOException;
import java.util.Properties;
public class Test {
String conf = "log4j.properties";
private static final
有一个工程,本来运行是正常的,我想把它移植到另一台PC上,结果报:
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.mobovip.bgr/com.mobovip.bgr.MainActivity}: java.lang.ClassNotFoundException: Didn't f
// 报错如下:
$ git pull origin master
fatal: unable to access 'https://git.xxx.com/': SSL certificate problem: unable to get local issuer ce
rtificate
// 原因:
由于git最新版默认使用ssl安全验证,但是我们是使用的git未设