假设我们的最大内存为256M,为什么这段代码有效:
public static void main(String... args) {
for (int i = 0; i < 2; i++)
{
byte[] a1 = new byte[150000000];
}
byte[] a2 = new byte[150000000];
}
Run Code Online (Sandbox Code Playgroud)
但是这个扔了一个OOME?
public static void main(String... args) {
//for (int i = 0; i < 2; i++)
{
byte[] a1 = new byte[150000000];
}
byte[] a2 = new byte[150000000];
}
Run Code Online (Sandbox Code Playgroud) Kotlin中是否有任何特定的语言实现,它与协同程序的另一种语言实现有所区别?
在这里,我将启动100000个协程,这段代码背后会发生什么?
for(i in 0..100000){
async(CommonPool){
//run long running operations
}
}
Run Code Online (Sandbox Code Playgroud) 在下面的代码中,对一个看似不兼容的类型的dowcast传递编译:
public class Item {
List<Item> items() { return asList(new Item()); }
Item m = (Item) items();
}
Run Code Online (Sandbox Code Playgroud)
Item并且List<Item>是完全不同的类型,因此演员阵容永远不会成功.为什么编译器允许这样做?
我偶然发现了一个非常不稳定的性能曲线实例,它对原始数组进行了非常简单的map/reduce操作.这是我的jmh基准代码:
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@BenchmarkMode(Mode.AverageTime)
@OperationsPerInvocation(Measure.ARRAY_SIZE)
@Warmup(iterations = 300, time = 200, timeUnit=MILLISECONDS)
@Measurement(iterations = 1, time = 1000, timeUnit=MILLISECONDS)
@State(Scope.Thread)
@Threads(1)
@Fork(1)
public class Measure
{
static final int ARRAY_SIZE = 1<<20;
final int[] ds = new int[ARRAY_SIZE];
private IntUnaryOperator mapper;
@Setup public void setup() {
setAll(ds, i->(int)(Math.random()*(1<<7)));
final int multiplier = (int)(Math.random()*10);
mapper = d -> multiplier*d;
}
@Benchmark public double multiply() {
return Arrays.stream(ds).map(mapper).sum();
}
}
Run Code Online (Sandbox Code Playgroud)
以下是典型输出的片段:
# VM invoker: /Library/Java/JavaVirtualMachines/jdk1.8.0_20.jdk/Contents/Home/jre/bin/java
# VM options: <none>
# Warmup: 300 iterations, …Run Code Online (Sandbox Code Playgroud) 这个问题不是关于众所周知和记录的事实,HashMap它不是线程安全的,而是关于它在HotSpot和JDK代码上的特定故障模式.我很惊讶这个代码在NPE中失败了:
public static void main(String[] args) {
Map<Integer, Integer> m = new HashMap<>(0, 0.75f);
IntStream.range(0, 5).parallel().peek(i -> m.put(i, i)).map(m::get).count();
}
Run Code Online (Sandbox Code Playgroud)
NPE的来源并不神秘:.map(m::get)尝试拆箱时的步骤null.它在5次运行中大约有4次失败.
在我的机器上Runtime#availableProcessors()报告8,所以假设长度为5的范围被分成5个子任务,每个子任务只有一个成员.我还假设我的代码以解释模式运行.它可能正在调用JIT编译HashMap或Stream方法,但是顶层被解释,因此排除了将HashMap状态加载到线程本地存储器(寄存器/堆栈)中的任何变化,从而延迟了另一个线程对更新的观察.如果五个put操作中的某些操作在同一时间不在不同的核心上执行,我不认为它会破坏HashMap内部结构.鉴于工作量很小,个别任务的时间安排必须非常精确.
它是否真的是精确的时间(commonPool必须取消停放的线程),还是有其他途径导致Oracle/OpenJDK HotSpot失败?我目前的版本是
java version "1.8.0_72"
Java(TM) SE Runtime Environment (build 1.8.0_72-b15)
Java HotSpot(TM) 64-Bit Server VM (build 25.72-b15, mixed mode)
Run Code Online (Sandbox Code Playgroud)
更新:我发现即使只进行两次插入也有类似的高故障率:
IntStream.range(0, 2).parallel().peek(i -> m.put(i, i)).map(m::get).count();
Run Code Online (Sandbox Code Playgroud) 我正在阅读Coroutine Basics,试图理解和学习它.
这段代码有一部分:
fun main() = runBlocking { // this: CoroutineScope
launch {
delay(200L)
println("Task from runBlocking")
}
coroutineScope { // Creates a new coroutine scope
launch {
delay(900L)
println("Task from nested launch")
}
delay(100L)
println("Task from coroutine scope") // This line will be printed before nested launch
}
println("Coroutine scope is over") // This line is not printed until nested launch completes
}
Run Code Online (Sandbox Code Playgroud)
输出如下:
Task from coroutine scope
Task from runBlocking
Task from nested launch
Coroutine scope …Run Code Online (Sandbox Code Playgroud) 术语内部类通常用于表示"需要封闭实例的嵌套类".但是,JLS声明如下:
8.1.3.内部类和封闭实例
[...]
内部类包括本地(§14.3),匿名(§15.9.5)和非静态成员类(§8.5).
[...]
声明在静态上下文中发生的内部类的实例没有词法封闭的实例.
也,
15.9.5.匿名类声明
[...]
匿名类始终是内部类(第8.1.3节); 它永远不会
static(§8.1.1,§8.5.1).
众所周知,匿名类可以在静态上下文中声明:
class A {
int t() { return 1; }
static A a = new A() { int t() { return 2; } };
}
Run Code Online (Sandbox Code Playgroud)
要尖锐地描述它,
new A() {} 是一个没有封闭实例的嵌套类,在静态上下文中定义,但它不是静态嵌套类 - 它是一个内部类.我们是否都在日常使用中为这些条款赋予了不恰当的含义?
作为一个相关的兴趣点,这个历史规范文档将术语顶层定义为内部的反面:
作为
static类成员的类和作为包成员的类都称为顶级类.它们与内部类的不同之处在于顶级类只能直接使用它自己的实例变量.
而在常见用法中,顶层被认为是嵌套的反面.
我试图证明Clojure的性能可以与Java平等.我发现的一个重要用例是Quicksort.我写了一个如下实现:
(set! *unchecked-math* true)
(defn qsort [^longs a]
(let [qs (fn qs [^long low, ^long high]
(when (< low high)
(let [pivot (aget a low)
[i j]
(loop [i low, j high]
(let [i (loop [i i] (if (< (aget a i) pivot)
(recur (inc i)) i))
j (loop [j j] (if (> (aget a j) pivot)
(recur (dec j)) j))
[i j] (if (<= i j)
(let [tmp (aget a i)]
(aset a i (aget a j)) (aset a …Run Code Online (Sandbox Code Playgroud) 我正在使用kotlin coroutines进行网络请求,使用扩展方法在这样的改造中调用类
public suspend fun <T : Any> Call<T>.await(): T {
return suspendCancellableCoroutine { continuation ->
enqueue(object : Callback<T> {
override fun onResponse(call: Call<T>?, response: Response<T?>) {
if (response.isSuccessful) {
val body = response.body()
if (body == null) {
continuation.resumeWithException(
NullPointerException("Response body is null")
)
} else {
continuation.resume(body)
}
} else {
continuation.resumeWithException(HttpException(response))
}
}
override fun onFailure(call: Call<T>, t: Throwable) {
// Don't bother with resuming the continuation if it is already cancelled.
if (continuation.isCancelled) return
continuation.resumeWithException(t)
} …Run Code Online (Sandbox Code Playgroud) 任何人都可以解释以下代码String.java,特别是为什么有三个if语句(我已经标记//1,//2并且//3)?
private static class CaseInsensitiveComparator
implements Comparator<String>, java.io.Serializable {
// use serialVersionUID from JDK 1.2.2 for interoperability
private static final long serialVersionUID = 8575799808933029326L;
public int compare(String s1, String s2) {
int n1=s1.length(), n2=s2.length();
for (int i1=0, i2=0; i1<n1 && i2<n2; i1++, i2++) {
char c1 = s1.charAt(i1);
char c2 = s2.charAt(i2);
if (c1 != c2) {/////////////////////////1
c1 = Character.toUpperCase(c1);
c2 = Character.toUpperCase(c2);
if (c1 != c2) {/////////////////////////2
c1 = …Run Code Online (Sandbox Code Playgroud)