Threading
Native threading constructs for simple asynchronous execution in BoxLang
📋 Table of Contents
🎯 Overview
Thread Execution Flow
🔀 When to Use Threading vs Async Programming
Feature
Native Threading
Async Programming (BoxFutures)
⚠️ A Fair Warning About Threading
Thread Safety Requirements
🐛 Thread Debugging
Debugging Functions
Function
Purpose
Output
Debugging Examples
Debug Output Destinations
📝 Thread Syntax
Script Syntax
Template Syntax
Thread Attributes
Attribute
Type
Required
Default
Description
🎬 Thread Actions
Action: run (Create and Execute)
run (Create and Execute)Action: join (Wait for Completion)
join (Wait for Completion)Action: sleep (Pause Thread)
sleep (Pause Thread)Action: terminate (Force Stop)
terminate (Force Stop)🏗️ Complete Thread Examples
Example 1: Fire-and-Forget Background Task
Example 2: Parallel Data Processing
Example 3: Thread with Priority
Example 4: Cooperative Thread Cancellation
🔭 Thread Scopes
Scope Overview
Scope
Write Access
Read Access
Lifetime
Use Case
1. Thread Local Scope
2. Thread Scope (Shared Output)
Writing to Thread Scope (Inside Thread)
Reading from Thread Scope (Outside Thread)
Complete Example
3. Attributes Scope (Input Parameters)
Attribute Copy Example
Scope Access Patterns
🧪 Built-In Functions (BIFs)
Thread Management BIFs
BIF
Purpose
Example
threadNew() - Functional Approach
threadNew() - Functional ApproachthreadJoin() - Wait for Completion
threadJoin() - Wait for CompletionisInThread() - Context Detection
isInThread() - Context DetectionisThreadAlive() - Status Check
isThreadAlive() - Status CheckCooperative Cancellation Pattern
☁️ Virtual Threads (Java 21+)
What are Virtual Threads?
Using Virtual Threads
When to Use Virtual Threads
Virtual Thread Example
📊 Thread Metadata
Metadata Properties
Property
Type
Description
Accessing Metadata
Error Handling
✅ Best Practices
1. Always Name Your Threads
2. Use Join for Data Dependencies
3. Minimize Shared Data
4. Use Locks for Shared Resources
5. Always Handle Exceptions
6. Clean Up Resources
7. Use Timeouts for Joins
⚠️ Common Pitfalls
1. ❌ Race Conditions on Shared State
2. ❌ Forgetting Deep Copy of Attributes
3. ❌ Using writedump() + abort()
4. ❌ Ignoring Thread Safety
5. ❌ Thread Scope Memory Leaks
🔗 Next Steps: Advanced Async Programming
🚀 BoxFutures & Async Programming
📚 Related Documentation
Last updated
Was this helpful?
