-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathScopedValueExampleVsThreadLocal.java
92 lines (76 loc) · 2.83 KB
/
ScopedValueExampleVsThreadLocal.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import jdk.incubator.concurrent.*;
import java.util.List;
import java.util.concurrent.Executors;
/**
* Run: `java --source 20 --enable-preview --add-modules jdk.incubator.concurrent ScopedValueExampleVsThreadLocal.java`
*/
public class ScopedValueExampleVsThreadLocal {
// pretty much the same
final static ThreadLocal<String> USER_LOCAL = new ThreadLocal<>();
final static ScopedValue<String> USER_SCOPE = ScopedValue.newInstance();
public static void main(String[] args) {
var users = List.of(
"Neo",
"Trinity",
"Morpheus",
"Switch",
"Dozer"
);
var calculator = new Calculator();
var worker = new UserWorker(calculator);
// now ExecutorService extends AutoCloseable
try (var executor = Executors.newThreadPerTaskExecutor(Thread.ofVirtual().factory())) {
System.out.println("Starting processing");
for (var i = 0; i < 5; i++) {
var user = users.get(i);
System.out.printf("Starting worker user %s%n", user);
executor.submit(() -> {
// ThreadLocal just set
USER_LOCAL.set(user);
// with ScopedValue, we set the value and define the scope in which it will be available
ScopedValue.where(USER_SCOPE, user)
.run(worker::run);
var userLocal = USER_LOCAL.get();
System.out.printf("User is still available in ThreadLocal: %s%n", userLocal);
});
}
}
System.out.println("Processing ended");
}
}
class UserWorker implements Runnable {
private Calculator calculator;
public UserWorker(Calculator calculator) {
this.calculator = calculator;
}
public void run() {
System.out.printf("Getting user for calculation...%n");
// both we retrieve the value in the same way
var user = ScopedValueExampleVsThreadLocal.USER_SCOPE.get();
var userLocal = ScopedValueExampleVsThreadLocal.USER_LOCAL.get();
System.out.printf("Users - ScopedValue: %s - ThreadLocal: %s%n", user, userLocal);
if (!user.equals(userLocal)) {
System.out.printf("Users from ScopedValue and ThreadLocal aren't the same%n", user);
return;
}
// the first difference, in ThreadLocal we can change the value at any time
ScopedValueExampleVsThreadLocal.USER_LOCAL.set(userLocal + " -- was changed");
System.out.printf("User %s - calculating...%n", user);
var answer = this.calculator.calculate();
System.out.printf("User %s - answer: %d%n", user, answer);
}
}
class Calculator {
public int calculate() {
// now the ThreadLocal will see a different value
// (could be change anywhere, it require us to look for the points that is changing it)
var user = ScopedValueExampleVsThreadLocal.USER_SCOPE.get();
var userLocal = ScopedValueExampleVsThreadLocal.USER_LOCAL.get();
System.out.printf("Users - ScopedValue: %s - ThreadLocal: %s%n", user, userLocal);
// pretend to go to DB to sum the rows
try {
Thread.sleep(500);
} catch (InterruptedException ex) {}
return user.length();
}
}