DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Revolutionizing Content Management
  • Architecting Scalable ASP.NET Core Web APIs With Abstract Factory Method and Onion Architecture
  • Building a Microservices API Gateway With YARP in ASP.NET Core Web API
  • Empowering Real-World Solutions the Synergy of AI and .NET

Trending

  • A Walk-Through of the DZone Article Editor
  • Introduction to Tactical DDD With Java: Steps to Build Semantic Code
  • Offline-First Patch Management for 10,000 Edge Nodes: A Practical Architecture That Scales
  • How to Write for DZone Publications: Trend Reports and Refcards
  1. DZone
  2. Coding
  3. Frameworks
  4. Creating 5,000 background threads in my ASP.NET Web app

Creating 5,000 background threads in my ASP.NET Web app

Threading is ridiculously complex to get right, and the idea of having 5,000 background worker threads is of course madness, unless you're using Hyperlambda, at which point it's just another Saturday

By 
Thomas Hansen user avatar
Thomas Hansen
DZone Core CORE ·
Jan. 01, 22 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
13.0K Views

Join the DZone community and get the full member experience.

Join For Free

I once saw a guy speaking about multi threaded programming, and he asked his audience how many of the people amongst had audience had created multiple threads, at which point half the room lifted their arms. Then he asked how many had used dozens of threads running simultaneously, at which point 25% of the room kept their arms up. When he reached the question "how many have created millions of concurrently executed threads" the speaker was the only person left with his arm raised. The reasons for this of course, is because the idea of creating millions of concurrently executed background threads is simply ridiculous to even imagine.

However with Hyperlambda, at least in theory, this is actually quite easily achieved. To understand how this is possible, realise that multi threading is just a state machine, allowing you to perform context switches, at the CPU level, with an interrupt scheduling work for each individual thread.

Once you understand the above, you realise you can create "multiple threads" using alternative mechanisms, that scales much better than CPU threads, allowing you to create millions of these buggers, without clogging your Web API backend in any ways what so ever. To bring home the point I am demonstrating how to create 5,000 background worker threads in Hyperlambda in the following video, each running in an "infinite loop", repeating some task over and over again. To further the insult, each individual worker thread is writing to a MySQL database, and everything is barely noticeable for my backend in any ways, and my backend is still almost as responsive as it would be without these background threads concurrently executing. And in fact, any performance loss is probably due to my MySQL server being hammered, and not my backend in any ways what so ever. Implying if I exchanged the default logger in Magic, I wouldn't even notice I've got 5,000 concurrently executed threads executed in parallel. And as a final touch, each thread's workload is persisted into my database, implying I can reboot my server without loosing any threads ...

Sounds like Magic? That's because it IS Magic ;)

In the above video I am dealing with 5,000 logical threads. However, at the operating system level, for all practical concerns, I am consuming zero threads, 99% of the time.

This is easily achieved with some "intelligent scheduling", allowing me to create 5,000 concurrently running background tasks, that for all practical concerns becomes the equivalent of 5,000 "logical background worker threads", while never consuming more than maximumly 8 threads from my operating system, and due to the async nature of Hyperlambda, consume zero threads 99% of the time. Implying my backend is still perfectly responsive, almost at the level it would normally be, without any of these background tasks running at all.

Of course, creating 5,000 background worker threads in for instance C# or something would completely choke my web server, making it beg for mercy at the operating system level, and have everything fall down and crash. With Hyperlambda, it's just another walk in the park :)

The code I am using can be found below. First how to create 5,000 scheduled tasks.

Plain Text
 
// Create 5,000 scheduled tasks
.no:int:1
while
   lt
      get-value:x:@.no
      .:int:5000
   .lambda
      guid.new
      convert:x:-
         type:string
      tasks.create:x:@convert
         repeats:120.seconds
         .lambda
            log.info:Scheduled task executed
      math.increment:x:@.no

Then how to delete the tasks afterwards.

Plain Text
tasks.list
   limit:5000
for-each:x:-/*/*/id
   tasks.delete:x:@.dp/#

If you want to reproduce what I am doing in the above video, you'll need at least version 10.0.15 of Magic and Hyperlambda. You can download it below.

  • Download Magic
ASP.NET Web API operating system app

Opinions expressed by DZone contributors are their own.

Related

  • Revolutionizing Content Management
  • Architecting Scalable ASP.NET Core Web APIs With Abstract Factory Method and Onion Architecture
  • Building a Microservices API Gateway With YARP in ASP.NET Core Web API
  • Empowering Real-World Solutions the Synergy of AI and .NET

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook