Is Java Still the Best Choice Now That Go Is Available?

Introduction

Quick Start

curl https://gosling.alibaba-inc.com/sh/enable-wisp2.sh | sh
ajdk 8.7.12_fp2 rpm
sudo yum install ajdk -b current #
java -XX:+UseWisp2 .... #
  • Coroutine […] indicates a coroutine.
  • Active indicates the number of times a coroutine is scheduled
  • Steal indicates the number of times a coroutine is stolen
  • Preempt indicates the number of times a time slice is preemptible

Overhead of Multithreading

Misunderstanding 1: Context Switching is Caused by Accessing the Kernel

pipe(a);
while (1) {
write(a[1], a, 1);
read(a[0], a, 1);
n += 2;
}

Misunderstanding 2: The Overhead of Context Switching is High

Overhead

Relationship Between Asynchronization and Coroutine

private void writeQuery(Channel ch) {
ch.write(Unpooled.wrappedBuffer("query".getBytes())).sync();
logger.info("write finish");
}
private void writeQuery(Channel ch) {
ch.write(Unpooled.wrappedBuffer("query".getBytes()))
.addListener(f -> {
logger.info("write finish");
});
}
suspend fun Channel.aWrite(msg: Any): Int =
suspendCoroutine { cont ->
write(msg).addListener { cont.resume(0) }
}
suspend fun writeQuery(ch: Channel) {
ch.aWrite(Unpooled.wrappedBuffer("query".toByteArray()))
logger.info("write finish")
}

Performance Comparison: Manual Asynchronous Programming vs WISP Programming

Write a New Application Based on Existing Components

Develop a New Application from Scratch

  • Thread control is more precise. For example, binding the connections of the proxy client and backend connections to the same Netty thread and localizing all thread operations.
  • Almost no coroutine runtime and scheduling overhead (about 1%) is generated.

Adapted Workload

  • In WISP 1, the parameters of connected applications and the implementation of WISP are deeply adapted.
  • In WISP 2, all threads are converted into coroutines, and no adaption is needed.

Project Loom

  • synchronized/Object.wait() occupies threads, so the CPU cannot be fully utilized.
  • Deadlocks may also be generated, which is inevitable in WISP. WISP only subsequently added support for ObectMonitor.
  • Loom is unfriendly to frameworks like Dubbo, and almost all frameworks in the stack contain reflection.

Frequently Asked Questions (FAQs)

Why is the Overhead Small in Case of Scheduled Coroutines?

Why Doesn’t WISP 2 Use ForkJoinPool to Schedule Coroutines?

What Do You Think of Reactive Programming?

What are the Main Constraints at the Moment?

Original Source:

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Alibaba Cloud

Alibaba Cloud

Follow me to keep abreast with the latest technology news, industry insights, and developer trends. Alibaba Cloud website:https://www.alibabacloud.com