Skip to content

Commit b58b3e6

Browse files
committed
Add support for virtual threads with IVMTaskScheduler
Introduce `IVMTaskScheduler` to enable virtual thread scheduling, improving thread management when `swiftmq.jac.active` is enabled. Updated `DefaultPoolManager` to initialize thread pools with `IVMTaskScheduler` when the JAC mode is active.
1 parent 453cedc commit b58b3e6

File tree

2 files changed

+117
-0
lines changed

2 files changed

+117
-0
lines changed

src/main/java/com/swiftmq/client/thread/DefaultPoolManager.java

+11
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.util.concurrent.locks.ReentrantReadWriteLock;
2424

2525
public class DefaultPoolManager extends PoolManager {
26+
public static final String PROP_JAC_ACTIVE = "swiftmq.jac.active";
2627
public static final String PROP_CONNECTOR_POOL_MIN_THREADS = "swiftmq.pool.connector.threads.min";
2728
public static final String PROP_CONNECTOR_POOL_MAX_THREADS = "swiftmq.pool.connector.threads.max";
2829
public static final String PROP_CONNECTOR_POOL_PRIO = "swiftmq.pool.connector.priority";
@@ -45,6 +46,16 @@ public class DefaultPoolManager extends PoolManager {
4546
ThreadPool connectionPool = null;
4647
ThreadPool sessionPool = null;
4748
ThreadPool connectorPool = null;
49+
boolean JAC_ACTIVE = System.getProperty(PROP_JAC_ACTIVE, "false").equals("true");
50+
51+
public DefaultPoolManager() {
52+
if (JAC_ACTIVE) {
53+
ThreadPool pool = new IVMTaskScheduler();
54+
connectionPool = pool;
55+
sessionPool = pool;
56+
connectorPool = pool;
57+
}
58+
}
4859

4960
ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
5061

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package com.swiftmq.client.thread;
2+
3+
import com.swiftmq.swiftlet.SwiftletManager;
4+
import com.swiftmq.swiftlet.threadpool.AsyncTask;
5+
import com.swiftmq.swiftlet.threadpool.ThreadPool;
6+
import com.swiftmq.swiftlet.threadpool.ThreadpoolSwiftlet;
7+
import com.swiftmq.swiftlet.threadpool.event.FreezeCompletionListener;
8+
9+
public class IVMTaskScheduler implements ThreadPool {
10+
11+
ThreadpoolSwiftlet threadpoolSwiftlet = null;
12+
13+
public IVMTaskScheduler() {
14+
threadpoolSwiftlet = (ThreadpoolSwiftlet) SwiftletManager.getInstance().getSwiftlet("sys$threadpool");
15+
if (threadpoolSwiftlet == null)
16+
System.err.println("[" + this + "] Failed to get ThreadpoolSwiftlet");
17+
else
18+
System.out.println("[" + this + "] App uses virtual threads, scheduled in: adhocvirtual");
19+
}
20+
21+
/**
22+
* Closes the pool.
23+
* Internal use only.
24+
*/
25+
@Override
26+
public void close() {
27+
28+
}
29+
30+
/**
31+
* Returns the pool name.
32+
*
33+
* @return pool name.
34+
*/
35+
@Override
36+
public String getPoolName() {
37+
return toString();
38+
}
39+
40+
/**
41+
* Returns the number of currently idling threads.
42+
* Used from management tools only.
43+
*
44+
* @return number of idling threads.
45+
*/
46+
@Override
47+
public int getNumberIdlingThreads() {
48+
return 0;
49+
}
50+
51+
/**
52+
* Returns the number of currently running threads.
53+
* Used from management tools only.
54+
*
55+
* @return number of running threads.
56+
*/
57+
@Override
58+
public int getNumberRunningThreads() {
59+
return 0;
60+
}
61+
62+
/**
63+
* Dispatch a task into the pool.
64+
*
65+
* @param asyncTask the task to dispatch.
66+
*/
67+
@Override
68+
public void dispatchTask(AsyncTask asyncTask) {
69+
threadpoolSwiftlet.runAsync(asyncTask, true);
70+
}
71+
72+
/**
73+
* Freezes this pool. That is, the current running tasks are completed but
74+
* no further tasks will be scheduled until unfreeze() is called. It is possible
75+
* to dispatch tasks during freeze. However, these will be executed after unfreeze()
76+
* is called.
77+
*
78+
* @param listener will be called when the pool is freezed.
79+
*/
80+
@Override
81+
public void freeze(FreezeCompletionListener listener) {
82+
83+
}
84+
85+
/**
86+
* Unfreezes this pool.
87+
*/
88+
@Override
89+
public void unfreeze() {
90+
91+
}
92+
93+
/**
94+
* Stops the pool.
95+
* Internal use only.
96+
*/
97+
@Override
98+
public void stop() {
99+
100+
}
101+
102+
@Override
103+
public String toString() {
104+
return IVMTaskScheduler.class.getSimpleName();
105+
}
106+
}

0 commit comments

Comments
 (0)