Skip to content

Commit f15dbe9

Browse files
authoredJul 18, 2023
feat: show multi-threading in example (#246)
Show multi-threading in example
1 parent 6d25405 commit f15dbe9

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
 

‎Examples/runtimes/java/DynamoDbEncryption/src/main/java/software/amazon/cryptography/examples/searchableencryption/CompoundBeaconSearchableEncryptionExample.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
import java.util.List;
55
import java.util.HashMap;
66
import java.util.Map;
7+
import java.util.concurrent.ExecutorService;
8+
import java.util.concurrent.Executors;
9+
import java.util.concurrent.TimeUnit;
710

811
import software.amazon.awssdk.core.client.config.ClientOverrideConfiguration;
912
import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
@@ -71,6 +74,7 @@ This table must have a Global Secondary Index (GSI) configured named "last4UnitC
7174
public class CompoundBeaconSearchableEncryptionExample {
7275

7376
static String GSI_NAME = "last4UnitCompound-index";
77+
static int MAX_CONCURRENT_QUERY_THREADS = 10;
7478

7579
public static void PutItemQueryItemWithCompoundBeacon(String ddbTableName, String branchKeyId, String branchKeyWrappingKmsKeyArn, String branchKeyDdbTableName) {
7680

@@ -232,6 +236,26 @@ public static void PutItemQueryItemWithCompoundBeacon(String ddbTableName, Strin
232236
.build())
233237
.build();
234238

239+
// Perform PutItem and Query
240+
PutAndQueryItemWithCompoundBeacon(ddb, ddbTableName);
241+
242+
// If instead you were working in a multi-threaded context
243+
// it might look like this
244+
Runnable myThread = () -> {
245+
for(int i = 0; i < 20; ++i) {
246+
PutAndQueryItemWithCompoundBeacon(ddb, ddbTableName);
247+
}
248+
};
249+
ExecutorService pool = Executors.newFixedThreadPool(MAX_CONCURRENT_QUERY_THREADS);
250+
for(int i = 0; i < (2*MAX_CONCURRENT_QUERY_THREADS); i++) {
251+
pool.execute(myThread);
252+
}
253+
pool.shutdown();
254+
try {pool.awaitTermination(30, TimeUnit.SECONDS);} catch (Exception e) {}
255+
}
256+
257+
public static void PutAndQueryItemWithCompoundBeacon(DynamoDbClient ddb, String ddbTableName) {
258+
235259
// 11. Put an item with both attributes used in the compound beacon.
236260
final HashMap<String, AttributeValue> item = new HashMap<>();
237261
item.put("work_id", AttributeValue.builder().s("9ce39272-8068-4efd-a211-cd162ad65d4c").build());

0 commit comments

Comments
 (0)
Please sign in to comment.