-
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
3047348
commit c13d8ab
Showing
1 changed file
with
65 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,65 @@ | ||
// Инкапусляция: тип-обертка для WaitGroup | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"sync" | ||
"time" | ||
) | ||
|
||
// ConcRunner выполняет заданные функции одновременно. | ||
type ConcRunner struct { | ||
wg sync.WaitGroup | ||
funcs []func() | ||
} | ||
|
||
// NewConcRunner создает новый экземпляр ConcRunner. | ||
func NewConcRunner() *ConcRunner { | ||
return &ConcRunner{wg: sync.WaitGroup{}} | ||
} | ||
|
||
// Add добавляет функцию, не выполняя ее. | ||
func (cg *ConcRunner) Add(fn func()) { | ||
cg.funcs = append(cg.funcs, fn) | ||
} | ||
|
||
// Run выполняет функции одновременно и дожидается их окончания. | ||
func (cg *ConcRunner) Run() { | ||
cg.wg.Add(len(cg.funcs)) | ||
for _, fn := range cg.funcs { | ||
go func() { | ||
defer cg.wg.Done() | ||
fn() | ||
}() | ||
} | ||
cg.wg.Wait() | ||
} | ||
|
||
func timeit(cg *ConcRunner) { | ||
start := time.Now() | ||
cg.Run() | ||
elapsed := time.Now().Sub(start).Milliseconds() | ||
fmt.Printf("3 functions took %d ms\n", elapsed) | ||
} | ||
|
||
func main() { | ||
work := func() { | ||
time.Sleep(50 * time.Millisecond) | ||
fmt.Print(".") | ||
} | ||
|
||
cr := NewConcRunner() | ||
|
||
// формируем набор функций | ||
cr.Add(work) | ||
cr.Add(work) | ||
cr.Add(work) | ||
|
||
// выполняем настроенные функции | ||
timeit(cr) | ||
// ...3 functions took 50 ms | ||
|
||
// и еще разок | ||
timeit(cr) | ||
// ...3 functions took 50 ms | ||
} |