优化client

This commit is contained in:
redkale
2023-03-28 08:12:58 +08:00
parent 94185cc23e
commit a61d515a49

View File

@@ -11,7 +11,6 @@ import java.nio.channels.*;
import java.util.Objects; import java.util.Objects;
import java.util.concurrent.*; import java.util.concurrent.*;
import java.util.concurrent.atomic.*; import java.util.concurrent.atomic.*;
import java.util.function.Supplier;
import org.redkale.annotation.ResourceType; import org.redkale.annotation.ResourceType;
import org.redkale.net.client.*; import org.redkale.net.client.*;
import org.redkale.util.*; import org.redkale.util.*;
@@ -41,9 +40,7 @@ public class AsyncIOGroup extends AsyncGroup {
private final AtomicBoolean connectThreadInited = new AtomicBoolean(); private final AtomicBoolean connectThreadInited = new AtomicBoolean();
private final Supplier<AsyncIOThread> connectThreadSupplier; private final AsyncIOThread connectThread;
private volatile AsyncIOThread connectThread;
final int bufferCapacity; final int bufferCapacity;
@@ -97,18 +94,14 @@ public class AsyncIOGroup extends AsyncGroup {
this.ioReadThreads[i] = createAsyncIOThread(g, String.format(threadNameFormat, indexfix), i, threads, workExecutor, safeBufferPool); this.ioReadThreads[i] = createAsyncIOThread(g, String.format(threadNameFormat, indexfix), i, threads, workExecutor, safeBufferPool);
this.ioWriteThreads[i] = this.ioReadThreads[i]; this.ioWriteThreads[i] = this.ioReadThreads[i];
} }
this.connectThreadSupplier = () -> createConnectIOThread(g, String.format(threadNameFormat, "Connect"), 0, 0, workExecutor, safeBufferPool); this.connectThread = createConnectIOThread(g, String.format(threadNameFormat, "Connect"), 0, 0, workExecutor, safeBufferPool);
} catch (IOException e) { } catch (IOException e) {
throw new RedkaleException(e); throw new RedkaleException(e);
} }
} }
protected AsyncIOThread createConnectIOThread(ThreadGroup g, String name, int index, int threads, ExecutorService workExecutor, ByteBufferPool safeBufferPool) { protected AsyncIOThread createConnectIOThread(ThreadGroup g, String name, int index, int threads, ExecutorService workExecutor, ByteBufferPool safeBufferPool) throws IOException {
try { return new AsyncIOThread(g, name, index, threads, workExecutor, safeBufferPool);
return new AsyncIOThread(g, name, index, threads, workExecutor, safeBufferPool);
} catch (IOException e) {
return null;
}
} }
protected AsyncIOThread createAsyncIOThread(ThreadGroup g, String name, int index, int threads, ExecutorService workExecutor, ByteBufferPool safeBufferPool) throws IOException { protected AsyncIOThread createAsyncIOThread(ThreadGroup g, String name, int index, int threads, ExecutorService workExecutor, ByteBufferPool safeBufferPool) throws IOException {
@@ -125,7 +118,6 @@ public class AsyncIOGroup extends AsyncGroup {
AsyncIOThread connectThread() { AsyncIOThread connectThread() {
if (connectThreadInited.compareAndSet(false, true)) { if (connectThreadInited.compareAndSet(false, true)) {
this.connectThread = connectThreadSupplier.get();
this.connectThread.start(); this.connectThread.start();
} }
return this.connectThread; return this.connectThread;
@@ -145,6 +137,7 @@ public class AsyncIOGroup extends AsyncGroup {
this.ioWriteThreads[i].start(); this.ioWriteThreads[i].start();
} }
} }
//connectThread用时才初始化
started = true; started = true;
return this; return this;
} }