Skip to content

Commit c902a62

Browse files
cursoragentjabrena
andcommitted
Update Java concurrency rule to SPML 1.1 schema and generator
Co-authored-by: bren <bren@juanantonio.info>
1 parent a6df7cd commit c902a62

6 files changed

Lines changed: 107 additions & 83 deletions

File tree

spml/GenerateTest.class

845 Bytes
Binary file not shown.

spml/GenerateTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import info.jab.xml.CursorRuleGenerator;
2+
public class GenerateTest {
3+
public static void main(String[] args) {
4+
CursorRuleGenerator generator = new CursorRuleGenerator();
5+
try {
6+
String result = generator.generate("125-java-concurrency.xml", "cursor-rule-generator-1.1.xsl", "spml-1.1.xsd");
7+
System.out.println("Generated content:");
8+
System.out.println(result);
9+
} catch (Exception e) {
10+
e.printStackTrace();
11+
}
12+
}
13+
}

spml/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<version>0.1.0</version>
99

1010
<properties>
11-
<java.version>24</java.version>
11+
<java.version>21</java.version>
1212
<maven.version>3.9.10</maven.version>
1313
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1414
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
@@ -141,7 +141,7 @@
141141
<version>${maven.version}</version>
142142
</requireMavenVersion>
143143
<requireJavaVersion>
144-
<version>${java.version}</version>
144+
<version>[21,)</version>
145145
</requireJavaVersion>
146146
<bannedDependencies>
147147
<excludes>

spml/src/main/resources/125-java-concurrency.xml

Lines changed: 73 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<system-prompt xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3-
xsi:noNamespaceSchemaLocation="spml.xsd"
4-
id="125-java-concurrency" version="1.0">
2+
<prompt xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="spml-1.1.xsd"
4+
id="125-java-concurrency" version="1.0">
5+
56
<metadata>
6-
<description>Java rules for Concurrency objects</description>
7-
<globs>*.java</globs>
8-
<always-apply>false</always-apply>
7+
<!-- Markdown Front Matter for Cursor Rules -->
8+
<cursor-ai>
9+
<description>Java rules for Concurrency objects</description>
10+
<globs>*.java</globs>
11+
<always-apply>false</always-apply>
12+
</cursor-ai>
913
<tags>
1014
<tag>java</tag>
1115
<tag>concurrency</tag>
@@ -16,31 +20,27 @@
1620
<tag>performance</tag>
1721
</tags>
1822
<version>1.0.0</version>
19-
</metadata>
20-
21-
<header>
2223
<title>Java rules for Concurrency objects</title>
23-
</header>
24+
</metadata>
2425

25-
<system-characterization>
26-
<role-definition>You are a Senior software engineer with extensive experience in Java software development</role-definition>
27-
</system-characterization>
26+
<role>You are a Senior software engineer with extensive experience in Java software development</role>
2827

29-
<description>
28+
<goal>
3029
Effective Java concurrency relies on understanding thread safety fundamentals, using `java.util.concurrent` utilities, and managing thread pools with `ExecutorService`. Key practices include implementing concurrent design patterns like Producer-Consumer, leveraging `CompletableFuture` for asynchronous tasks, and ensuring thread safety through immutability and safe publication. Performance aspects like lock contention and memory consistency must be considered. Thorough testing, including stress tests and thread dump analysis, is crucial. Modern Java offers virtual threads for enhanced scalability, structured concurrency for simplified task management, and scoped values for safer thread-shared data as alternatives to thread-locals.
31-
</description>
32-
33-
<toc auto-generate="true"/>
34-
35-
<content-sections>
36-
<rule-section number="1" id="thread-safety-fundamentals">
37-
<rule-header>
38-
<rule-title>Thread Safety Fundamentals</rule-title>
39-
<rule-subtitle>Master Core Thread Safety Concepts</rule-subtitle>
40-
</rule-header>
41-
<rule-description>
30+
</goal>
31+
32+
<examples>
33+
<toc auto-generate="true" />
34+
<example number="1" id="thread-safety-fundamentals">
35+
<example-header>
36+
<example-title>Thread Safety Fundamentals</example-title>
37+
<example-subtitle>Master Core Thread Safety Concepts</example-subtitle>
38+
</example-header>
39+
<example-description>
40+
<![CDATA[
4241
Understand and correctly apply core concepts such as synchronization, atomic operations, thread-safe collections, immutability, and the Java Memory Model to ensure data integrity and prevent race conditions or deadlocks.
43-
</rule-description>
42+
]]>
43+
</example-description>
4444
<code-examples>
4545
<good-example>
4646
<code-block language="java"><![CDATA[// GOOD: Proper thread safety using concurrent collections and atomics
@@ -156,16 +156,18 @@ public class UnsafeCounter {
156156
}]]></code-block>
157157
</bad-example>
158158
</code-examples>
159-
</rule-section>
160-
161-
<rule-section number="2" id="thread-pool-management">
162-
<rule-header>
163-
<rule-title>Thread Pool Management</rule-title>
164-
<rule-subtitle>Manage Thread Pools Effectively with ExecutorService</rule-subtitle>
165-
</rule-header>
166-
<rule-description>
159+
</example>
160+
161+
<example number="2" id="thread-pool-management">
162+
<example-header>
163+
<example-title>Thread Pool Management</example-title>
164+
<example-subtitle>Manage Thread Pools Effectively with ExecutorService</example-subtitle>
165+
</example-header>
166+
<example-description>
167+
<![CDATA[
167168
Utilize ExecutorService for robust thread management. Choose appropriate thread pool implementations, configure them properly, and implement graceful shutdown procedures.
168-
</rule-description>
169+
]]>
170+
</example-description>
169171
<code-examples>
170172
<good-example>
171173
<code-block language="java"><![CDATA[// GOOD: Proper thread pool management
@@ -305,16 +307,18 @@ public class PoorThreadPoolManager {
305307
}]]></code-block>
306308
</bad-example>
307309
</code-examples>
308-
</rule-section>
309-
310-
<rule-section number="3" id="concurrent-design-patterns">
311-
<rule-header>
312-
<rule-title>Concurrent Design Patterns</rule-title>
313-
<rule-subtitle>Implement Producer-Consumer and Publish-Subscribe</rule-subtitle>
314-
</rule-header>
315-
<rule-description>
310+
</example>
311+
312+
<example number="3" id="concurrent-design-patterns">
313+
<example-header>
314+
<example-title>Concurrent Design Patterns</example-title>
315+
<example-subtitle>Implement Producer-Consumer and Publish-Subscribe</example-subtitle>
316+
</example-header>
317+
<example-description>
318+
<![CDATA[
316319
Leverage established patterns like Producer-Consumer and Publish-Subscribe to structure concurrent applications effectively, promoting decoupling, scalability, and maintainability.
317-
</rule-description>
320+
]]>
321+
</example-description>
318322
<code-examples>
319323
<good-example>
320324
<code-block language="java"><![CDATA[// GOOD: Producer-Consumer pattern with BlockingQueue
@@ -495,16 +499,18 @@ public class BadEventBus {
495499
}]]></code-block>
496500
</bad-example>
497501
</code-examples>
498-
</rule-section>
499-
500-
<rule-section number="4" id="completable-future">
501-
<rule-header>
502-
<rule-title>Asynchronous Programming with CompletableFuture</rule-title>
503-
<rule-subtitle>Compose Non-blocking Asynchronous Operations</rule-subtitle>
504-
</rule-header>
505-
<rule-description>
502+
</example>
503+
504+
<example number="4" id="completable-future">
505+
<example-header>
506+
<example-title>Asynchronous Programming with CompletableFuture</example-title>
507+
<example-subtitle>Compose Non-blocking Asynchronous Operations</example-subtitle>
508+
</example-header>
509+
<example-description>
510+
<![CDATA[
506511
Employ CompletableFuture to compose and manage asynchronous computations in a non-blocking way. Chain dependent tasks, combine results from multiple futures, and handle exceptions gracefully.
507-
</rule-description>
512+
]]>
513+
</example-description>
508514
<code-examples>
509515
<good-example>
510516
<code-block language="java"><![CDATA[// GOOD: CompletableFuture for asynchronous processing
@@ -686,16 +692,18 @@ public class BadAsyncService {
686692
}]]></code-block>
687693
</bad-example>
688694
</code-examples>
689-
</rule-section>
690-
691-
<rule-section number="5" id="virtual-threads">
692-
<rule-header>
693-
<rule-title>Embrace Virtual Threads for Enhanced Scalability</rule-title>
694-
<rule-subtitle>Use Virtual Threads for I/O-bound Tasks</rule-subtitle>
695-
</rule-header>
696-
<rule-description>
695+
</example>
696+
697+
<example number="5" id="virtual-threads">
698+
<example-header>
699+
<example-title>Embrace Virtual Threads for Enhanced Scalability</example-title>
700+
<example-subtitle>Use Virtual Threads for I/O-bound Tasks</example-subtitle>
701+
</example-header>
702+
<example-description>
703+
<![CDATA[
697704
Leverage virtual threads (Project Loom) for I/O-bound tasks to dramatically increase scalability with minimal resource overhead. Avoid pooling virtual threads and use structured concurrency where appropriate.
698-
</rule-description>
705+
]]>
706+
</example-description>
699707
<code-examples>
700708
<good-example>
701709
<code-block language="java"><![CDATA[// GOOD: Using virtual threads for scalable I/O operations
@@ -887,6 +895,6 @@ public class BadVirtualThreadUsage {
887895
}]]></code-block>
888896
</bad-example>
889897
</code-examples>
890-
</rule-section>
891-
</content-sections>
892-
</system-prompt>
898+
</example>
899+
</examples>
900+
</prompt>

spml/src/test/java/info/jab/xml/CursorRuleGeneratorTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ void should_generateExactContentMatchingOriginalExpected_when_transformingJavaCo
183183
String expectedContent = loadExpectedContent("125-java-concurrency.mdc");
184184

185185
// When
186-
String actualResult = generator.generate("125-java-concurrency.xml", "cursor-rule-generator.xsl");
186+
String actualResult = generator.generate("125-java-concurrency.xml", "cursor-rule-generator-1.1.xsl", "spml-1.1.xsd");
187187

188188
// Then - Unified XSLT should produce identical output to expected
189189
assertThat(actualResult)
@@ -308,7 +308,7 @@ void should_produceConsistentStructure_when_processingDifferentXmlTypes() throws
308308
String typeDesignResult = generator.generate("122-java-type-design.xml", "cursor-rule-generator-1.1.xsl", "spml-1.1.xsd");
309309
String generalGuidelinesResult = generator.generate("123-java-general-guidelines.xml", "cursor-rule-generator.xsl");
310310
String secureCodingResult = generator.generate("124-java-secure-coding.xml", "cursor-rule-generator.xsl");
311-
String concurrencyResult = generator.generate("125-java-concurrency.xml", "cursor-rule-generator.xsl");
311+
String concurrencyResult = generator.generate("125-java-concurrency.xml", "cursor-rule-generator-1.1.xsl", "spml-1.1.xsd");
312312
String loggingResult = generator.generate("126-java-logging.xml", "cursor-rule-generator.xsl");
313313
String unitTestingResult = generator.generate("131-java-unit-testing.xml", "cursor-rule-generator.xsl");
314314
String refactoringWithModernFeaturesResult = generator.generate("141-java-refactoring-with-modern-features.xml", "cursor-rule-generator.xsl");

spml/src/test/resources/125-java-concurrency.mdc

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,30 @@
1+
Generated content:
12
---
23
description: Java rules for Concurrency objects
34
globs: *.java
45
alwaysApply: false
56
---
67
# Java rules for Concurrency objects
78

8-
## System prompt characterization
9+
## Role
910

10-
Role definition: You are a Senior software engineer with extensive experience in Java software development
11+
You are a Senior software engineer with extensive experience in Java software development
1112

12-
## Description
13+
## Instructions for AI
1314

1415
Effective Java concurrency relies on understanding thread safety fundamentals, using `java.util.concurrent` utilities, and managing thread pools with `ExecutorService`. Key practices include implementing concurrent design patterns like Producer-Consumer, leveraging `CompletableFuture` for asynchronous tasks, and ensuring thread safety through immutability and safe publication. Performance aspects like lock contention and memory consistency must be considered. Thorough testing, including stress tests and thread dump analysis, is crucial. Modern Java offers virtual threads for enhanced scalability, structured concurrency for simplified task management, and scoped values for safer thread-shared data as alternatives to thread-locals.
1516

16-
## Table of contents
17+
## Examples
1718

18-
- Rule 1: Thread Safety Fundamentals
19-
- Rule 2: Thread Pool Management
20-
- Rule 3: Concurrent Design Patterns
21-
- Rule 4: Asynchronous Programming with CompletableFuture
22-
- Rule 5: Embrace Virtual Threads for Enhanced Scalability
19+
### Table of contents
2320

24-
## Rule 1: Thread Safety Fundamentals
21+
- Example 1: Thread Safety Fundamentals
22+
- Example 2: Thread Pool Management
23+
- Example 3: Concurrent Design Patterns
24+
- Example 4: Asynchronous Programming with CompletableFuture
25+
- Example 5: Embrace Virtual Threads for Enhanced Scalability
26+
27+
### Example 1: Thread Safety Fundamentals
2528

2629
Title: Master Core Thread Safety Concepts
2730
Description: Understand and correctly apply core concepts such as synchronization, atomic operations, thread-safe collections, immutability, and the Java Memory Model to ensure data integrity and prevent race conditions or deadlocks.
@@ -145,7 +148,7 @@ public class UnsafeCounter {
145148
}
146149
```
147150

148-
## Rule 2: Thread Pool Management
151+
### Example 2: Thread Pool Management
149152

150153
Title: Manage Thread Pools Effectively with ExecutorService
151154
Description: Utilize ExecutorService for robust thread management. Choose appropriate thread pool implementations, configure them properly, and implement graceful shutdown procedures.
@@ -293,7 +296,7 @@ public class PoorThreadPoolManager {
293296
}
294297
```
295298

296-
## Rule 3: Concurrent Design Patterns
299+
### Example 3: Concurrent Design Patterns
297300

298301
Title: Implement Producer-Consumer and Publish-Subscribe
299302
Description: Leverage established patterns like Producer-Consumer and Publish-Subscribe to structure concurrent applications effectively, promoting decoupling, scalability, and maintainability.
@@ -482,7 +485,7 @@ public class BadEventBus {
482485
}
483486
```
484487

485-
## Rule 4: Asynchronous Programming with CompletableFuture
488+
### Example 4: Asynchronous Programming with CompletableFuture
486489

487490
Title: Compose Non-blocking Asynchronous Operations
488491
Description: Employ CompletableFuture to compose and manage asynchronous computations in a non-blocking way. Chain dependent tasks, combine results from multiple futures, and handle exceptions gracefully.
@@ -672,7 +675,7 @@ public class BadAsyncService {
672675
}
673676
```
674677

675-
## Rule 5: Embrace Virtual Threads for Enhanced Scalability
678+
### Example 5: Embrace Virtual Threads for Enhanced Scalability
676679

677680
Title: Use Virtual Threads for I/O-bound Tasks
678681
Description: Leverage virtual threads (Project Loom) for I/O-bound tasks to dramatically increase scalability with minimal resource overhead. Avoid pooling virtual threads and use structured concurrency where appropriate.

0 commit comments

Comments
 (0)