May 21, 2024
Thread management with JDK21 – what’s changing and features to look forward to
Comments
(4)
May 21, 2024
Thread management with JDK21 – what’s changing and features to look forward to
Newbie 3 posts
Followers: 4 people
(4)

JDK21 introduces a bunch of useful functionalities around thread management such as virtual thread but it also changes things with regards to thread.stop functionality which affects ColdFusion’s implementation for thread’s functionality around termination.

First up, let’s understand what’s changing in thread terminate.

Why is cfthread.stop handling required?

The next release of ColdFusion upgrades Java to JDK21. JDK21 has removed the thread stop method so to certify it we will have to make changes in cfthread terminate and threadterminate which internally calls Java’s thread.stop.

Why has Java removed thread.stop?

The stop() method was considered unsafe. If a thread was stopped externally, all the monitors that the thread held became unlocked instantaneously. This could leave shared resources and variables in an unsafe, and unstable meta state.

Solution to absorb this change

Remove methods for ThreadTerminate cfthread attribute for terminating a thread.  Introduce a separate method for ThreadInterrupt and attribute for interrupting a thread.

ColdFusion currently exposes the ThreadTerminate method which stops a thread immediately. Internally we use Java’s Thread Stop which is being removed in JDK21 which is being removed so we will have to remove this method too.

Instead, users will be given the option to interrupt a thread using a new method called Thread interrupt method. This method is safer and a more graceful way of stopping a thread. This method will be implementing functionality provided by Java’s thread interrupt functionality.

Proposed code –

<cfscript> thread action="run" name="myThread" {
    while (!ThreadInterrupted()) {
         // execute task....
    }
}
</cfscript>

 

<cfscript> thread action="run" name="myThread" 
 {    
try { 
        // 1. if the interrupt flag is true, the thread is terminated 
        while (!ThreadInterrupted()) { 
            // execute tasks... 
        } 
    } catch (com.adobe.coldfusion.ThreadInterruptedException ie) {  
        // 2. When catch InterruptedException, thread is ready for gracefull termination 
    } 
} 
</cfscript>

Pros of the solution:

  • Futureproofing with Java
  • Thread Safety
  • No data corruption or resource leaks

Cons of the solution:

  • Customers will have to update their code if they had been using terminate method and attribute

While this was about a change that you can expect in CF2025, planning is also underway for the next release post CF2025. Some of the proposals around Java thread management the team has been thinking about are the below ones for the next release after 2025, are these –

  • Virtual threads with JDK21 – Virtual threads represent lightweight, stackless threads that can be scheduled by the JVM’s scheduler. They are created and managed at the JVM level rather than relying on the underlying OS threads. Virtual threads offer a more efficient concurrency model compared to traditional OS threads, making it easier to write scalable and responsive applications. More on virtual threads can be found here: https://docs.oracle.com/en/java/javase/21/core/virtual-threads.html#GUID-2BCFC2DD-7D84-4B0C-9222-97F9C7C6C521
  • Threadpool – Much older feature in Java but super useful and not present in ColdFusion yet. It entails providing capabilities to create and maintain a pool of pre-initialized threads ready to execute tasks as needed. The approach improves performance by reusing threads and minimizing the overhead of thread creation and destruction. Details about this –  https://docs.oracle.com/javase/tutorial/essential/concurrency/pools.html

Have any thoughts and opinions to share? Please comment or write to me on cdhoot@adobe.com with details about the following:

  1. Whether you are using any of the methods (thread terminate & cfthread terminate) and what are you using it for? Some contexts around the application, code samples and notes around intended functionality will be extremely helpful in understanding your use case for these functions better, and thereby understanding the effectiveness of the proposed solution for it.
  2. Your thoughts / ideas for the proposed solution for thread interrupt.
  3. Your requirements around some thread management features under consideration for the release post CF2025, or any new feature you want us to consider for the same.

Signing off for now! Take care, do write to me. Your feedback is valuable!

4 Comments
2024-05-23 16:43:07
2024-05-23 16:43:07

I’m a bit confused in the second block of proposed code that both has threadInterupted() call _and_ a try/catch. What exactly is throwing the error there that is not throwing in the first block of proposed code.

That said, I don’t terminate threads in my application logic. The only place I’ve ever done that is in an Application Performance Monitoring (APM) tool like FusionReactor. Presumably, you’d still be able to do that without having to author every cfthread as if it could be killed at any moment? Or, to ask another way, is this only relevant for cases in which the application code is explicitly designed to terminate its own threads?

Like
2024-05-23 16:29:44
2024-05-23 16:29:44

I assume you’ll also be going through the Java code that implements CF and making sure there are no synchronized methods as part of this work, since that causes virtual threads to “pin” to O/S threads and can lead to the system being overwhelmed?

Like
2024-05-23 13:00:25
2024-05-23 13:00:25

Love the transparency and “opening the kimono”. Usually things were “done” in the past without much public discussion/explanation, let alone seeking of feedback.

(I do realize there were private conversations with some customers and of course the prerelease. I refer to this sort of public discussion.)

And hey, you said the quiet part your loud and “in print” here: cf2025. Nice to see the new version name admitted to, versus previously only saying it in hushed tones/back channels until the release… or perhaps someone else inside will hear about your naming it here and bark. Time will tell. 🙂

Anyway, thanks for the post. While I don’t use thread termination enough to want to influence implementation, I’ll say the offered code seems quite a change from a simple terminate. 🙁 But perhaps it has to be. Let’s see what others think. (Paolo raised a good question.)

All that said, and as much as I appreciate the public discussion, I suspect a tiny percent of the user base will see this. Just the nature of things. They’ll only see changes after the release. But we can’t let that keep you from trying.

Indeed, looking forward to more discussions of possible changes in cf2025. Thanks for getting the ball rolling.

Finally, before someone inevitably jumps in clamoring that “attention should be paid instead to fixing your bugs!”, note that each new release does tend to fix dozens of bugs, as do the intermediary updates. While some may lament that THEIR reported bug remains unresolved (I have many), it’s just NOT true that they blithely disregard bugs/fail to give attention to them. They can resolve them AND work on new features, as there’s a whole TEAM of developers (and a lot of stakeholders to accommodate).

Again, great to see your efforts, Charvi. 🙂

Like
2024-05-23 09:23:00
2024-05-23 09:23:00

Hi Charvi, thanks for the heads up.

On old code we have iterations on the state of the thread checking the ‘completed’ and ‘terminated’ values. Is this still valid or will the `status` property no longer be able to take on that value?

Like
Add Comment