62,610
社区成员
发帖
与我相关
我的任务
分享
private static class IntegerCache {
static final int low = -128;
static final int high;
static final Integer cache[];
static {
// high value may be configured by property
int h = 127;
String integerCacheHighPropValue =
sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
if (integerCacheHighPropValue != null) {
int i = parseInt(integerCacheHighPropValue);
i = Math.max(i, 127);
// Maximum array size is Integer.MAX_VALUE
h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
}
high = h;
cache = new Integer[(high - low) + 1];
int j = low;
for(int k = 0; k < cache.length; k++)
cache[k] = new Integer(j++);
}
public static Integer valueOf(int i) {
assert IntegerCache.high >= 127;
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
明白了么?里面是有一个缓存池的,其实如果你愿意,改改源码,大可以存比127大的数。缓存池cache是final的,好吧,有兴趣你可以写一个final类型的数组然后javap一下看看是否他在常量池中。至于下面那个问题,我不明确说为什么信号量可以而CountDownLatch不行,只是因为太长了,多线程的东西不是三行两行就讲清了的,多写写爬虫多线程抓取,用用生产者消费者,还有线程池自然就明白了,只讲是没有用的[/quote]
学习了。。
private static class IntegerCache {
static final int low = -128;
static final int high;
static final Integer cache[];
static {
// high value may be configured by property
int h = 127;
String integerCacheHighPropValue =
sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
if (integerCacheHighPropValue != null) {
int i = parseInt(integerCacheHighPropValue);
i = Math.max(i, 127);
// Maximum array size is Integer.MAX_VALUE
h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
}
high = h;
cache = new Integer[(high - low) + 1];
int j = low;
for(int k = 0; k < cache.length; k++)
cache[k] = new Integer(j++);
}
public static Integer valueOf(int i) {
assert IntegerCache.high >= 127;
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
明白了么?里面是有一个缓存池的,其实如果你愿意,改改源码,大可以存比127大的数。缓存池cache是final的,好吧,有兴趣你可以写一个final类型的数组然后javap一下看看是否他在常量池中。至于下面那个问题,我不明确说为什么信号量可以而CountDownLatch不行,只是因为太长了,多线程的东西不是三行两行就讲清了的,多写写爬虫多线程抓取,用用生产者消费者,还有线程池自然就明白了,只讲是没有用的[/quote]
真不明白,有人欠你钱了吗?
现在的社会不会是一个人能玩的转的,无论你技术的高地。
谦逊是为人之本。从一个团队来讲,我同样不会要那种拽的二五八万似的人物。
贴这几个问题,只是面试的时候还记得的题目,知道答案又愿意回答可以说,不愿意可以不写,没必要去说教。
private static class IntegerCache {
static final int low = -128;
static final int high;
static final Integer cache[];
static {
// high value may be configured by property
int h = 127;
String integerCacheHighPropValue =
sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
if (integerCacheHighPropValue != null) {
int i = parseInt(integerCacheHighPropValue);
i = Math.max(i, 127);
// Maximum array size is Integer.MAX_VALUE
h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
}
high = h;
cache = new Integer[(high - low) + 1];
int j = low;
for(int k = 0; k < cache.length; k++)
cache[k] = new Integer(j++);
}
public static Integer valueOf(int i) {
assert IntegerCache.high >= 127;
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
Object lock = new Object();
int count = 0;
public void fun(Thread t) {
// 访问计数判断与更新
synchronized (lock) {
while (count >= 2) {
try {
lock.wait();
} catch (InterruptedException e) {
// ignore
}
}
count++;
}
// 方法实际操作部分。。。。
System.out.println(t.getName());// 测试时打印
// 更新访问计数并唤醒等待的线程
count--;
lock.notifyAll();
}