Let’s take a look at how to create multithreaded batch jobs in Microsoft Dynamics 365 F&SCM. In most cases a single threaded batch job will be perfect for the job. However, there are situations where additional performance may be needed and, in those cases, using multithreaded batch jobs could provide us with the throughput required.
The code referenced in this article follows this project structure:

Read this article for more information about different approaches to multithreading. Here we will implement Top Picking.
A quick overview of the approach:

Create a standard single-threaded batch job. This will be our main batch job that will divide our work into tasks and assign each task to a new thread. Most of this should be very familiar as it follows the standard SysOps framework.
The contract class will take input from the user to determine how many tasks to break our work up into.
The service is the most interesting part, so let’s dive into that more.

Here we have another standard controller class, and a service class with some interesting pieces. Looking into the service class more we see:

Here is where we would run our business logic. For the sake of a high-level demonstration, this class will simply log our value and sleep for 5 seconds to simulate a longer processing job. However, this is where your core functionality would exist to use the data in the staging table in order to complete some process.

To demonstrate this, we will load our staging table up with test data. To do this we will run our runnable job “CreateStagingRecords” by visiting the following URL: https://[D365URL].com/?cmp=[LegalEntity]&mi=SysClassRunner&cls=[ClassName]

We loaded our staging table with 100 records, and the processing job should take around 5 seconds to process each record. That would be a total of 500 seconds of execution time. However, we can run our job with 8 threads to divide that work, which should bring each thread’s execution time to just over 1 minute.
Now, let’s go kick off our batch job.

We can see that the main job used to create tasks completed quickly after spinning up the multithreaded job.

We can see our “Get work item” job completed with all 8 threads taking approximately 1 minute each. Exactly what we were hoping for!

As you can see, multithreading batch jobs in D365 can really increase the performance of a long running batch job. If you elect to use this approach, make sure you understand the number of threads parameterized in your System administration settings. Additionally, make sure your processing is able to work with each record in isolation to avoid threads competing.
I hope you found this useful, and your Dynamics 365 batch jobs will now be lighting fast!
For more D365 tech advice, project planning and support connect with us here.
Post originally published https://markedcode.com/index.php/2024/01/24/d365-multithreading-batch-jobs/
This publication contains general information only and Sikich is not, by means of this publication, rendering accounting, business, financial, investment, legal, tax, or any other professional advice or services. This publication is not a substitute for such professional advice or services, nor should you use it as a basis for any decision, action or omission that may affect you or your business. Before making any decision, taking any action or omitting an action that may affect you or your business, you should consult a qualified professional advisor. In addition, this publication may contain certain content generated by an artificial intelligence (AI) language model. You acknowledge that Sikich shall not be responsible for any loss sustained by you or any person who relies on this publication.