-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6ed712a
commit 54e843a
Showing
1 changed file
with
61 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package concurrent | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"sync" | ||
"testing" | ||
) | ||
|
||
// WaitCloser 结构体 | ||
type WaitCloser struct { | ||
wg sync.WaitGroup | ||
c io.Closer | ||
} | ||
|
||
// Add 方法,用于在开始并发操作时调用 | ||
func (wc *WaitCloser) Add(delta int) { | ||
wc.wg.Add(delta) | ||
} | ||
|
||
// Done 方法,用于在并发操作完成时调用 | ||
func (wc *WaitCloser) Done() { | ||
wc.wg.Done() | ||
} | ||
|
||
// Close 方法,确保所有并发操作完成后关闭资源 | ||
func (wc *WaitCloser) Close() error { | ||
wc.wg.Wait() | ||
return wc.c.Close() | ||
} | ||
|
||
// 假设一个示例资源 | ||
type exampleResource struct{} | ||
|
||
// 实现 io.Closer 接口的 Close 方法 | ||
func (er *exampleResource) Close() error { | ||
fmt.Println("Resource closed") | ||
return nil | ||
} | ||
|
||
func TestWaitCloser(t *testing.T) { | ||
resource := &exampleResource{} | ||
wc := WaitCloser{c: resource} | ||
|
||
// 开始一个并发操作 | ||
wc.Add(1) | ||
go func() { | ||
defer wc.Done() | ||
fmt.Println("Concurrent operation 1") | ||
}() | ||
|
||
// 再开始另一个并发操作 | ||
wc.Add(1) | ||
go func() { | ||
defer wc.Done() | ||
fmt.Println("Concurrent operation 2") | ||
}() | ||
|
||
// 关闭资源(等待所有操作完成后关闭) | ||
wc.Close() | ||
} |