# JUC **Repository Path**: bfs1201/juc ## Basic Information - **Project Name**: JUC - **Description**: juc学习代码演示 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-09-05 - **Last Updated**: 2026-04-01 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Java Multithreading Programming Learning Project This project is a comprehensive Java multithreading programming tutorial, covering multiple topics from basics to advanced levels. Through rich code examples, it helps developers deeply understand the core concepts and practical techniques of Java concurrency programming. ## Project Structure ``` src/main/java/cn/itcast/ ├── n1/ # Thread Basics - Creation and Startup ├── n2/ # Asynchronous Programming Basics ├── n3/ # Thread Control and State Management ├── t4/ # Thread Synchronization and Safety │ ├── deadLock/ # Deadlock and Livelock │ ├── exercise/ # Exercises │ ├── multiLock/ # Multi-lock Solutions │ ├── park/ # Park/Unpark Mechanism │ ├── pattern/ # Concurrency Design Patterns │ │ ├── guardedSuspension/ # Guarded Suspension Pattern │ │ ├── messageQueue/ # Message Queue │ │ └── sequence/ # Sequential Control │ ├── reentrantLock/ # Reentrant Lock │ ├── safe/ # Thread-Safe vs. Thread-Unsafe │ ├── synchronized_test/ # synchronized Testing │ └── wait/ # wait/notify Mechanism ├── t5/ # volatile and Thread-Local Variables │ └── volatile_test/ # volatile Keyword ├── thread/local/ # ThreadLocal ├── util/ # Utility Classes └── constant/ # Constant Definitions ``` ## Core Topics ### 1. Thread Basics (n1 package) - **Test1**: Thread creation and basic usage - **Test2**: Creating threads using Lambda expressions - **Test3**: Thread return values and Future ### 2. Thread States and Control (n3 package) - **TestStartAndRun**: Difference between start() and run() - **TestSleepAndYield**: sleep() and yield() - **TestInterrupt**: Thread interruption mechanism - **TestJoin**: Usage of join() method - **TestDaemon**: Daemon threads - **TestTwoPhaseTermination**: Two-phase termination pattern ### 3. Thread Synchronization (t4 package) - **synchronized**: Synchronized blocks and methods - **ReentrantLock**: Reentrant lock - **Wait/Notify**: wait() and notify() - **Condition**: Condition variables - **DeadLock**: Deadlock issues and solutions ### 4. Concurrency Design Patterns (t4/pattern package) - **Guarded Suspension**: Guarded Suspension Pattern - **Message Queue**: Message Queue - **Promise**: Asynchronous programming pattern - **Parallel**: Parallel processing ### 5. volatile Keyword (t5/volatile_test package) - **CanStop**: Using volatile to stop a thread - **Balking**: Balking Pattern - **TwoPhaseTermination**: Two-phase termination pattern ### 6. ThreadLocal - Thread-local variables - Thread-isolated context ## Technology Stack - **JDK**: Java 8+ - **Build Tool**: Maven - **Logging Framework**: SLF4J + Logback ## Quick Start ### Compile the Project ```bash mvn clean compile ``` ### Run Tests ```bash mvn test ``` ### Run a Single Example ```bash # Run thread basics example mvn exec:java -Dexec.mainClass="cn.itcast.n1.Test1" # Run synchronization example mvn exec:java -Dexec.mainClass="cn.itcast.t4.synchronized_test.TestSynchronized" ``` ## Key Examples Explained ### Two-Phase Termination Pattern Gracefully stops a thread service by first setting a shutdown flag, then waiting for the thread to complete its current work before exiting. ### Producer-Consumer Pattern (MessageQueue) Implements a thread-safe blocking queue using wait/notify, supporting timed waits. ### Dining Philosophers Problem (PhilosopherMeal) A classic concurrency problem demonstrating deadlock occurrence and how to avoid it using lock ordering or hierarchical resource allocation. ### Tea Brewing Process (TestPAOCHA) A multi-threaded collaboration example simulating parallel tasks such as boiling water, cleaning teacups, and fetching tea leaves. ## Dependency Configuration Project main dependencies: ```xml ```