Skip to content

feat: show multi-threading in example #246

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jul 18, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,23 @@ public static void PutItemQueryItemWithCompoundBeacon(String ddbTableName, Strin
.build())
.build();

// Perform PutItem and Query
QueryItemWithCompoundBeacon(ddb, ddbTableName);

// If instead you were working in a multi-threaded context
// it might look like this
Runnable myThread = () -> {
for(int i = 0; i < 20; ++i)
QueryItemWithCompoundBeacon(ddb, ddbTableName);
};
// increase once we expect threads to work
for(int i = 0; i < 1; ++i) {
Thread run = new Thread(myThread);
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a thread pool would be cleaner here:

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

...

MAX_CONCURRENT_QUERY_THREADS = 1;

Then,

Suggested change
// If instead you were working in a multi-threaded context
// it might look like this
Runnable myThread = () -> {
for(int i = 0; i < 20; ++i)
QueryItemWithCompoundBeacon(ddb, ddbTableName);
};
// increase once we expect threads to work
for(int i = 0; i < 1; ++i) {
Thread run = new Thread(myThread);
}
}
// If instead you were working in a multi-threaded context
// it might look like this
Runnable myThread = () -> {
for (int i = 0; i < 20; i++) {
QueryItemWithCompoundBeacon(ddb, ddbTableName);
}
};
ExecutorService pool = Executors.newFixedThreadPool(MAX_CONCURRENT_QUERY_THREADS);
for(int i = 0; i < MAX_CONCURRENT_QUERY_THREADS; i++) {
pool.execute(myThread);
}
pool.shutDown();

We could also manage threads individually, but I think we would have to manage them in some data structure to ensure all threads join before we return success from the test, e.g.

    Thread[] threads = new Thread[MAX_CONCURRENT_QUERY_THREADS];
    // If instead you were working in a multi-threaded context
    // it might look like this
    Runnable myThread = () -> {
        for(int i = 0; i < 20; i++)
            QueryItemWithCompoundBeacon(ddb, ddbTableName);
    };
    // increase once we expect threads to work
    for(int i = 0; i < MAX_CONCURRENT_QUERY_THREADS; i++) {
        Thread run = new Thread(myThread);
        threads[i] = run;
        run.start();
    }
    for(int i = 0; i < MAX_CONCURRENT_QUERY_THREADS; i++) {
        run.join()
    }


public static void QueryItemWithCompoundBeacon(DynamoDbClient ddb, String ddbTableName) {

// 11. Put an item with both attributes used in the compound beacon.
final HashMap<String, AttributeValue> item = new HashMap<>();
item.put("work_id", AttributeValue.builder().s("9ce39272-8068-4efd-a211-cd162ad65d4c").build());
Expand Down