gpt4 book ai didi

java - 非阻塞 I/O 与使用线程(上下文切换有多糟糕?)

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:55:23 24 4
gpt4 key购买 nike

我们在我工作的一个程序中经常使用套接字,我们有时同时处理来自多达大约 100 台机器的连接。我们有一个非阻塞的组合 I/O与状态表一起使用来管理它和使用线程的传统 Java 套接字。

我们在非阻塞套接字方面遇到了很多问题,我个人更喜欢使用线程来更好地处理套接字。所以我的问题是:

在单个线程上使用非阻塞套接字可以节省多少?使用线程涉及的上下文切换有多糟糕?您可以扩展多少个并发连接以使用 Java 中的线程模型?

最佳答案

I/O 和非阻塞 I/O 选择取决于您的服务器 Activity 配置文件。例如。如果您使用长期连接和数千个客户端,则 I/O 可能会因为系统资源耗尽而变得过于昂贵。但是,不会挤占 CPU 缓存的直接 I/O 比非阻塞 I/O 更快。有一篇关于此的好文章 - Writing Java Multithreaded Servers - whats old is new .

关于上下文切换成本——这是芯片操作。考虑下面的简单测试:

package com;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.ConcurrentSkipListSet;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;

public class AAA {

private static final long DURATION = TimeUnit.NANOSECONDS.convert(30, TimeUnit.SECONDS);
private static final int THREADS_NUMBER = 2;
private static final ThreadLocal<AtomicLong> COUNTER = new ThreadLocal<AtomicLong>() {
@Override
protected AtomicLong initialValue() {
return new AtomicLong();
}
};
private static final ThreadLocal<AtomicLong> DUMMY_DATA = new ThreadLocal<AtomicLong>() {
@Override
protected AtomicLong initialValue() {
return new AtomicLong();
}
};
private static final AtomicLong DUMMY_COUNTER = new AtomicLong();
private static final AtomicLong END_TIME = new AtomicLong(System.nanoTime() + DURATION);

private static final List<ThreadLocal<CharSequence>> DUMMY_SOURCE = new ArrayList<ThreadLocal<CharSequence>>();
static {
for (int i = 0; i < 40; ++i) {
DUMMY_SOURCE.add(new ThreadLocal<CharSequence>());
}
}

private static final Set<Long> COUNTERS = new ConcurrentSkipListSet<Long>();

public static void main(String[] args) throws Exception {
final CountDownLatch startLatch = new CountDownLatch(THREADS_NUMBER);
final CountDownLatch endLatch = new CountDownLatch(THREADS_NUMBER);

for (int i = 0; i < THREADS_NUMBER; i++) {
new Thread() {
@Override
public void run() {
initDummyData();
startLatch.countDown();
try {
startLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
while (System.nanoTime() < END_TIME.get()) {
doJob();
}
COUNTERS.add(COUNTER.get().get());
DUMMY_COUNTER.addAndGet(DUMMY_DATA.get().get());
endLatch.countDown();
}
}.start();
}
startLatch.await();
END_TIME.set(System.nanoTime() + DURATION);

endLatch.await();
printStatistics();
}

private static void initDummyData() {
for (ThreadLocal<CharSequence> threadLocal : DUMMY_SOURCE) {
threadLocal.set(getRandomString());
}
}

private static CharSequence getRandomString() {
StringBuilder result = new StringBuilder();
Random random = new Random();
for (int i = 0; i < 127; ++i) {
result.append((char)random.nextInt(0xFF));
}
return result;
}

private static void doJob() {
Random random = new Random();
for (ThreadLocal<CharSequence> threadLocal : DUMMY_SOURCE) {
for (int i = 0; i < threadLocal.get().length(); ++i) {
DUMMY_DATA.get().addAndGet(threadLocal.get().charAt(i) << random.nextInt(31));
}
}
COUNTER.get().incrementAndGet();
}

private static void printStatistics() {
long total = 0L;
for (Long counter : COUNTERS) {
total += counter;
}
System.out.printf("Total iterations number: %d, dummy data: %d, distribution:%n", total, DUMMY_COUNTER.get());
for (Long counter : COUNTERS) {
System.out.printf("%f%%%n", counter * 100d / total);
}
}
}

我对两个线程和十个线程的场景进行了四次测试,结果表明性能损失约为 2.5%(两个线程迭代 78626 次,十个线程迭代 76754 次),线程使用的系统资源大致相等。

此外,“java.util.concurrent” 作者假设上下文切换时间约为 2000-4000 个 CPU 周期:

public class Exchanger<V> {
...
private static final int NCPU = Runtime.getRuntime().availableProcessors();
....
/**
* The number of times to spin (doing nothing except polling a
* memory location) before blocking or giving up while waiting to
* be fulfilled. Should be zero on uniprocessors. On
* multiprocessors, this value should be large enough so that two
* threads exchanging items as fast as possible block only when
* one of them is stalled (due to GC or preemption), but not much
* longer, to avoid wasting CPU resources. Seen differently, this
* value is a little over half the number of cycles of an average
* context switch time on most systems. The value here is
* approximately the average of those across a range of tested
* systems.
*/
private static final int SPINS = (NCPU == 1) ? 0 : 2000;

关于java - 非阻塞 I/O 与使用线程(上下文切换有多糟糕?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1749018/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com