GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — 1, 单独抽离vt 并且补充单侧 · dreamlike-ocean/UnsafeJava@37392bd

This repository was archived by the owner on Jun 30, 2026. It is now read-only.

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • pom.xml
  • unsafe-jdk/src/test/java
    • ContinuationTest.java
  • unsafe-vt
    • pom.xml
    • src
      • main/java/io/github/dreamlike/unsafe/vthread
        • CarrierThreadLocal.java
        • Continuation.java
        • TerminatingThreadLocal.java
        • VirtualThreadUnsafe.java
      • test/java
        • ContinuationTest.java
  • vertx-ext
    • pom.xml
    • src/main/java/top/dreamlike/unsafe/vertx
      • AsyncScope.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎pom.xml‎

Copy file name to clipboardExpand all lines: pom.xml
+7Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
<module>vertx-ext</module>
4242
<module>unsafe-core</module>
4343
<module>unsafe-jit</module>
44+
<module>unsafe-vt</module>
4445
</modules>
4546

4647
<properties>
@@ -49,6 +50,7 @@
4950
<core.version>1.3</core.version>
5051
<maven.compiler.source>${java.version}</maven.compiler.source>
5152
<maven.compiler.target>${java.version}</maven.compiler.target>
53+
<unsafe-vt.version>1.0.0</unsafe-vt.version>
5254
</properties>
5355

5456
<dependencyManagement>
@@ -63,6 +65,11 @@
6365
<artifactId>unsafe-jdk</artifactId>
6466
<version>1.0.0</version>
6567
</dependency>
68+
<dependency>
69+
<groupId>io.github.dreamlike-ocean</groupId>
70+
<artifactId>unsafe-vt</artifactId>
71+
<version>${unsafe-vt.version}</version>
72+
</dependency>
6673
</dependencies>
6774
</dependencyManagement>
6875

Collapse file

‎unsafe-jdk/src/test/java/ContinuationTest.java‎

Copy file name to clipboardExpand all lines: unsafe-jdk/src/test/java/ContinuationTest.java
-27Lines changed: 0 additions & 27 deletions
This file was deleted.
Collapse file

‎unsafe-vt/pom.xml‎

Copy file name to clipboard
+58Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<parent>
7+
<groupId>io.github.dreamlike-ocean</groupId>
8+
<artifactId>unsafe-java</artifactId>
9+
<version>1.0</version>
10+
</parent>
11+
12+
<artifactId>unsafe-vt</artifactId>
13+
<version>${unsafe-vt.version}</version>
14+
15+
<properties>
16+
<maven.compiler.source>22</maven.compiler.source>
17+
<maven.compiler.target>22</maven.compiler.target>
18+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
19+
</properties>
20+
21+
<dependencies>
22+
23+
<dependency>
24+
<groupId>io.github.dreamlike-ocean</groupId>
25+
<artifactId>unsafe-core</artifactId>
26+
</dependency>
27+
28+
<dependency>
29+
<groupId>junit</groupId>
30+
<artifactId>junit</artifactId>
31+
<version>4.13.2</version>
32+
<scope>test</scope>
33+
</dependency>
34+
</dependencies>
35+
36+
<build>
37+
<plugins>
38+
<plugin>
39+
<groupId>org.apache.maven.plugins</groupId>
40+
<artifactId>maven-compiler-plugin</artifactId>
41+
<configuration>
42+
<source>${java.version}</source>
43+
<target>${java.version}</target>
44+
<compilerArgs>--enable-preview</compilerArgs>
45+
</configuration>
46+
</plugin>
47+
<plugin>
48+
<groupId>org.apache.maven.surefire</groupId>
49+
<artifactId>surefire-junit4</artifactId>
50+
<version>3.1.2</version>
51+
<configuration>
52+
<argLine>--enable-preview</argLine>
53+
</configuration>
54+
</plugin>
55+
</plugins>
56+
</build>
57+
58+
</project>
Collapse file

‎…e/unsafe/vthread/CarrierThreadLocal.java‎ ‎…e/unsafe/vthread/CarrierThreadLocal.java‎unsafe-jdk/src/main/java/top/dreamlike/unsafe/vthread/CarrierThreadLocal.java renamed to unsafe-vt/src/main/java/io/github/dreamlike/unsafe/vthread/CarrierThreadLocal.java unsafe-jdk/src/main/java/top/dreamlike/unsafe/vthread/CarrierThreadLocal.java renamed to unsafe-vt/src/main/java/io/github/dreamlike/unsafe/vthread/CarrierThreadLocal.java

Copy file name to clipboardExpand all lines: unsafe-vt/src/main/java/io/github/dreamlike/unsafe/vthread/CarrierThreadLocal.java
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package top.dreamlike.unsafe.vthread;
1+
package io.github.dreamlike.unsafe.vthread;
22

33

44
import top.dreamlike.unsafe.core.MasterKey;
Collapse file

‎…eamlike/unsafe/vthread/Continuation.java‎ ‎…eamlike/unsafe/vthread/Continuation.java‎unsafe-jdk/src/main/java/top/dreamlike/unsafe/vthread/Continuation.java renamed to unsafe-vt/src/main/java/io/github/dreamlike/unsafe/vthread/Continuation.java unsafe-jdk/src/main/java/top/dreamlike/unsafe/vthread/Continuation.java renamed to unsafe-vt/src/main/java/io/github/dreamlike/unsafe/vthread/Continuation.java

Copy file name to clipboardExpand all lines: unsafe-vt/src/main/java/io/github/dreamlike/unsafe/vthread/Continuation.java
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package top.dreamlike.unsafe.vthread;
1+
package io.github.dreamlike.unsafe.vthread;
22

33

44
import top.dreamlike.unsafe.core.MasterKey;
Collapse file

‎…safe/vthread/TerminatingThreadLocal.java‎ ‎…safe/vthread/TerminatingThreadLocal.java‎unsafe-jdk/src/main/java/top/dreamlike/unsafe/vthread/TerminatingThreadLocal.java renamed to unsafe-vt/src/main/java/io/github/dreamlike/unsafe/vthread/TerminatingThreadLocal.java unsafe-jdk/src/main/java/top/dreamlike/unsafe/vthread/TerminatingThreadLocal.java renamed to unsafe-vt/src/main/java/io/github/dreamlike/unsafe/vthread/TerminatingThreadLocal.java

Copy file name to clipboardExpand all lines: unsafe-vt/src/main/java/io/github/dreamlike/unsafe/vthread/TerminatingThreadLocal.java
+38-22Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,31 @@
1-
package top.dreamlike.unsafe.vthread;
1+
package io.github.dreamlike.unsafe.vthread;
2+
23

3-
import net.bytebuddy.ByteBuddy;
4-
import net.bytebuddy.implementation.MethodDelegation;
5-
import net.bytebuddy.matcher.ElementMatchers;
64
import top.dreamlike.unsafe.core.MasterKey;
75

6+
import java.lang.classfile.ClassFile;
7+
import java.lang.classfile.TypeKind;
8+
import java.lang.constant.ClassDesc;
9+
import java.lang.constant.ConstantDescs;
10+
import java.lang.constant.MethodTypeDesc;
811
import java.lang.invoke.MethodHandle;
9-
import java.lang.invoke.VarHandle;
12+
import java.lang.invoke.MethodHandles;
1013
import java.lang.reflect.Field;
1114
import java.lang.reflect.Method;
15+
import java.lang.reflect.Modifier;
16+
import java.nio.file.Files;
17+
import java.nio.file.Paths;
1218

1319
public class TerminatingThreadLocal<T> extends ThreadLocal<T> {
1420

1521
private final static MethodHandle internalConstructorMH;
1622

17-
private final static VarHandle callBackVH;
1823
private final ThreadLocal<T> internal;
1924

2025
public TerminatingThreadLocal() {
2126
try {
22-
internal = (ThreadLocal<T>) internalConstructorMH.invoke();
2327
CallBack<T> callBack = this::threadTerminated;
24-
callBackVH.set(internal, callBack);
28+
internal = (ThreadLocal<T>) internalConstructorMH.invoke(callBack);
2529
} catch (Throwable e) {
2630
throw new RuntimeException(e);
2731
}
@@ -51,8 +55,7 @@ protected void threadTerminated(T value) {
5155
var customerTerminatingThreadLocalClass = init();
5256

5357
try {
54-
internalConstructorMH = MasterKey.INSTANCE.openTheDoor(customerTerminatingThreadLocalClass.getDeclaredConstructor());
55-
callBackVH = MasterKey.INSTANCE.openTheDoor(customerTerminatingThreadLocalClass.getDeclaredField("callback"));
58+
internalConstructorMH = MasterKey.INSTANCE.openTheDoor(customerTerminatingThreadLocalClass.getDeclaredConstructor(CallBack.class));
5659
}catch (Throwable throwable) {
5760
throw new RuntimeException(throwable);
5861
}
@@ -79,18 +82,31 @@ private static Class init() {
7982
.invoke(o, module, "jdk.internal.misc");
8083

8184

82-
ByteBuddy buddy = new ByteBuddy();
83-
84-
return buddy.subclass(threadLocalClass)
85-
.defineField("callback", CallBack.class)
86-
.method(ElementMatchers.named("threadTerminated"))
87-
// 转发到我们搞得类上面。。。
88-
.intercept(MethodDelegation.toField("callback"))
89-
.make()
90-
.load(TerminatingThreadLocal.class.getClassLoader())
91-
.getLoaded();
92-
93-
85+
ClassFile classFile = ClassFile.of();
86+
ClassDesc thisClassDesc = ClassDesc.of("io.github.dreamlike.unsafe.vthread.CustomerTerminatingThreadLocal");
87+
byte[] bytes = classFile.build(thisClassDesc, it -> {
88+
it.withField("callback", CallBack.class.describeConstable().get(), Modifier.FINAL | Modifier.PRIVATE);
89+
it.withSuperclass(threadLocalClass.describeConstable().get());
90+
it.withMethodBody("threadTerminated", MethodTypeDesc.of(ConstantDescs.CD_void, ConstantDescs.CD_Object),Modifier.PROTECTED, cb -> {
91+
cb.aload(0);
92+
cb.getfield(thisClassDesc, "callback", CallBack.class.describeConstable().get());
93+
cb.loadInstruction(TypeKind.ReferenceType, cb.parameterSlot(0));
94+
cb.invokeinterface(CallBack.class.describeConstable().get(), "threadTerminated", MethodTypeDesc.of(ConstantDescs.CD_void, ConstantDescs.CD_Object));
95+
cb.return_();
96+
});
97+
98+
it.withMethodBody(ConstantDescs.INIT_NAME, MethodTypeDesc.of(ConstantDescs.CD_void, CallBack.class.describeConstable().get()), Modifier.PUBLIC, cb -> {
99+
cb.aload(0);
100+
cb.invokespecial(threadLocalClass.describeConstable().get(), ConstantDescs.INIT_NAME, ConstantDescs.MTD_void);
101+
//this
102+
cb.aload(0);
103+
cb.loadInstruction(TypeKind.ReferenceType, cb.parameterSlot(0));
104+
cb.putfield(thisClassDesc, "callback", CallBack.class.describeConstable().get());
105+
cb.return_();
106+
});
107+
});
108+
Files.write(Paths.get("proxy.class"), bytes);
109+
return MethodHandles.lookup().defineClass(bytes);
94110
}catch (Throwable throwable) {
95111
throw new RuntimeException(throwable);
96112
}
Collapse file

‎…/unsafe/vthread/VirtualThreadUnsafe.java‎ ‎…/unsafe/vthread/VirtualThreadUnsafe.java‎unsafe-jdk/src/main/java/top/dreamlike/unsafe/vthread/VirtualThreadUnsafe.java renamed to unsafe-vt/src/main/java/io/github/dreamlike/unsafe/vthread/VirtualThreadUnsafe.java unsafe-jdk/src/main/java/top/dreamlike/unsafe/vthread/VirtualThreadUnsafe.java renamed to unsafe-vt/src/main/java/io/github/dreamlike/unsafe/vthread/VirtualThreadUnsafe.java

Copy file name to clipboardExpand all lines: unsafe-vt/src/main/java/io/github/dreamlike/unsafe/vthread/VirtualThreadUnsafe.java
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package top.dreamlike.unsafe.vthread;
1+
package io.github.dreamlike.unsafe.vthread;
22

33

44

Collapse file

‎unsafe-vt/src/test/java/ContinuationTest.java‎

Copy file name to clipboard
+75Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import io.github.dreamlike.unsafe.vthread.CarrierThreadLocal;
2+
import io.github.dreamlike.unsafe.vthread.Continuation;
3+
import io.github.dreamlike.unsafe.vthread.TerminatingThreadLocal;
4+
import io.github.dreamlike.unsafe.vthread.VirtualThreadUnsafe;
5+
import org.junit.Assert;
6+
import org.junit.Test;
7+
8+
import java.util.UUID;
9+
import java.util.concurrent.ExecutionException;
10+
import java.util.concurrent.ExecutorService;
11+
import java.util.concurrent.Executors;
12+
import java.util.concurrent.atomic.AtomicBoolean;
13+
import java.util.concurrent.atomic.AtomicInteger;
14+
15+
public class ContinuationTest {
16+
17+
18+
@Test
19+
public void testContinuation() {
20+
AtomicInteger count = new AtomicInteger(0);
21+
Continuation continuation = new Continuation(() -> {
22+
count.incrementAndGet();
23+
Assert.assertNotNull(Continuation.currentContinuation());
24+
Continuation.yield();
25+
count.incrementAndGet();
26+
});
27+
28+
continuation.run();
29+
Assert.assertEquals(count.get(), 1);
30+
Assert.assertFalse(continuation.isDone());
31+
continuation.run();
32+
Assert.assertEquals(count.get(), 2);
33+
Assert.assertTrue(continuation.isDone());
34+
}
35+
36+
@Test
37+
public void testCarrierThreadLocal() throws ExecutionException, InterruptedException {
38+
CarrierThreadLocal<UUID> carrierThreadLocal = new CarrierThreadLocal<>();
39+
UUID uuid = UUID.randomUUID();
40+
try (ExecutorService dispatcher = Executors.newSingleThreadExecutor()) {
41+
dispatcher.submit(() -> {
42+
carrierThreadLocal.set(uuid);
43+
}).get();
44+
dispatcher.submit(() -> {
45+
Assert.assertEquals(uuid, carrierThreadLocal.get());
46+
}).get();
47+
Thread.Builder.OfVirtual virtualBuilder = VirtualThreadUnsafe.VIRTUAL_THREAD_BUILDER.apply(dispatcher);
48+
virtualBuilder.start(() -> {
49+
Assert.assertTrue(Thread.currentThread().isVirtual());
50+
Assert.assertEquals(uuid, carrierThreadLocal.get());
51+
});
52+
dispatcher.shutdown();
53+
}
54+
}
55+
56+
@Test
57+
public void testTerminatingThreadLocal() throws ExecutionException, InterruptedException {
58+
AtomicBoolean atomicBoolean = new AtomicBoolean(false);
59+
TerminatingThreadLocal<Object> threadLocal = new TerminatingThreadLocal() {
60+
@Override
61+
protected void threadTerminated(Object value) {
62+
atomicBoolean.set(true);
63+
}
64+
};
65+
66+
Thread thread = new Thread(() -> {
67+
threadLocal.set(UUID.randomUUID());
68+
}, "TerminatingThread-0");
69+
thread.start();
70+
thread.join();
71+
72+
Assert.assertSame(thread.getState(), Thread.State.TERMINATED);
73+
Assert.assertTrue(atomicBoolean.get());
74+
}
75+
}
Collapse file

‎vertx-ext/pom.xml‎

Copy file name to clipboardExpand all lines: vertx-ext/pom.xml
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
<dependencies>
3333
<dependency>
3434
<groupId>io.github.dreamlike-ocean</groupId>
35-
<artifactId>unsafe-jdk</artifactId>
35+
<artifactId>unsafe-vt</artifactId>
3636
</dependency>
3737
<dependency>
3838
<groupId>io.vertx</groupId>
Collapse file

‎vertx-ext/src/main/java/top/dreamlike/unsafe/vertx/AsyncScope.java‎

Copy file name to clipboardExpand all lines: vertx-ext/src/main/java/top/dreamlike/unsafe/vertx/AsyncScope.java
+1-2Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
package top.dreamlike.unsafe.vertx;
22

3+
import io.github.dreamlike.unsafe.vthread.Continuation;
34
import io.vertx.core.Context;
45
import io.vertx.core.Future;
56
import io.vertx.core.Vertx;
6-
import top.dreamlike.unsafe.vthread.Continuation;
7-
87
public class AsyncScope {
98

109

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.