# threasy **Repository Path**: hovvsoon/threasy ## Basic Information - **Project Name**: threasy - **Description**: threasy - 轻量线程队列同步器 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2023-08-21 - **Last Updated**: 2023-10-18 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ## 稍息器 > 稍息器 - 轻量线程队列同步器 ## 功能特性 ### 互斥器 互斥器原理如下。 ```mermaid graph LR 阻塞队列(阻塞队列) --> 线程1 线程1 --> 线程2 线程2 -- ... --> 线程n 主线程(主线程) --> 新建线程 新建线程 -- cas:t --> 占有线程 新建线程 -- cas:f-add --> 阻塞队列 占有线程 -- unpark --> 阻塞队列 阻塞队列 -- cas:t --> 占有线程 阻塞队列 -- cas:f-add --> 阻塞队列 ``` 假设业务逻辑代码如下。 ```java class Logic implements Runnable { Integer cnt = Integer.valueOf(0); public void run() { var local = cnt; java.util.logging.Logger.getGlobal().info( String.format("thread:%s ts:%d \n want:%d->%d", Thread.currentThread().getName(), System.currentTimeMillis(), cnt, local + 1)); cnt = ++local; } } ``` 包装互斥器,实现线程互斥。 ```java class MutexLogic implements Runnable { Logic logic = new Logic(); cc.aolob.threasy.Mutex mutex = new cc.aolob.threasy.Mutex(); @Override public void run() { mutex.lock(); logic.run(); mutex.unlock(); } } ``` 输出: ``` 8月 8, 2023 08:08:08 下午 cc.aolob.threasy.example.Logic run 信息: thread:Thread-4 ts:1692632169724 want:0->1 8月 8, 2023 08:08:08 下午 cc.aolob.threasy.example.Logic run 信息: thread:Thread-1 ts:1692632169724 want:0->1 8月 8, 2023 08:08:08 下午 cc.aolob.threasy.example.Logic run 信息: thread:Thread-5 ts:1692632169724 want:0->1 8月 8, 2023 08:08:08 下午 cc.aolob.threasy.example.Logic run 信息: thread:Thread-0 ts:1692632169724 want:0->1 8月 8, 2023 08:08:08 下午 cc.aolob.threasy.example.Logic run 信息: thread:Thread-6 ts:1692632169724 want:0->1 8月 8, 2023 08:08:08 下午 cc.aolob.threasy.example.Logic run 信息: thread:Thread-2 ts:1692632169724 want:0->1 8月 8, 2023 08:08:08 下午 cc.aolob.threasy.example.Logic run 信息: thread:Thread-7 ts:1692632169724 want:0->1 8月 8, 2023 08:08:08 下午 cc.aolob.threasy.example.Logic run 信息: thread:Thread-8 ts:1692632169724 want:0->1 8月 8, 2023 08:08:08 下午 cc.aolob.threasy.example.Logic run 信息: thread:Thread-9 ts:1692632169724 want:0->1 8月 8, 2023 08:08:08 下午 cc.aolob.threasy.example.Logic run 信息: thread:Thread-3 ts:1692632169724 want:0->1 8月 8, 2023 08:08:08 下午 cc.aolob.threasy.example.MutexTest main 信息: thread:main ts:1692632170652 over:1 8月 8, 2023 08:08:08 下午 cc.aolob.threasy.example.Logic run 信息: thread:Thread-11 ts:1692632170655 want:0->1 8月 8, 2023 08:08:08 下午 cc.aolob.threasy.example.Logic run 信息: thread:Thread-10 ts:1692632170657 want:1->2 8月 8, 2023 08:08:08 下午 cc.aolob.threasy.example.Logic run 信息: thread:Thread-12 ts:1692632170658 want:2->3 8月 8, 2023 08:08:08 下午 cc.aolob.threasy.example.Logic run 信息: thread:Thread-19 ts:1692632170659 want:3->4 8月 8, 2023 08:08:08 下午 cc.aolob.threasy.example.Logic run 信息: thread:Thread-18 ts:1692632170660 want:4->5 8月 8, 2023 08:08:08 下午 cc.aolob.threasy.example.Logic run 信息: thread:Thread-17 ts:1692632170661 want:5->6 8月 8, 2023 08:08:08 下午 cc.aolob.threasy.example.Logic run 信息: thread:Thread-16 ts:1692632170662 want:6->7 8月 8, 2023 08:08:08 下午 cc.aolob.threasy.example.Logic run 信息: thread:Thread-14 ts:1692632170662 want:7->8 8月 8, 2023 08:08:08 下午 cc.aolob.threasy.example.Logic run 信息: thread:Thread-13 ts:1692632170663 want:8->9 8月 8, 2023 08:08:08 下午 cc.aolob.threasy.example.Logic run 信息: thread:Thread-15 ts:1692632170665 want:9->10 8月 8, 2023 08:08:08 下午 cc.aolob.threasy.example.MutexTest main 信息: thread:main ts:1692632171656 over:10 ```