Go language implements thread pool

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

It's really been a long time since I wrote a blog, and recently I've been working too hard to catch a new project. This week, the task is relatively small, and can be casually knocked.

Visit the forum when the whim, want to use go language to implement a thread pool, the main function is: Add total task to the thread pool, the thread pools open number of threads, each thread from the task queue to take a task execution, after the completion of the execution of a task, complete after the completion of a function callback.

I do not know if there are any eggs, but I try to use it to open 3 threads, download 10 files, the effect is good. The first time you write something in this area, it may not be well written.

The idea is to put the task into the channel, and each thread constantly pulls the task out of the channel and writes the execution result to another channel, and when it gets the total result, the callback function.

Previous code:

1Type Goroutinepoolstruct {2 Queue chan func () error3Numberint4Totalint5 6 Result Chan error7 finishcallback func ()8 }9 Ten //Initialize OneFunc (self *goroutinepool) Init (numberint, totalint) { ASelf. Queue =Make (chan func () error, total) -Self. Number = Number -Self. Total = Total theSelf.result =Make (Chan error, total) - } -  - //Door Pick -up +Func (Self *Goroutinepool) Start () { -     //Open number of Goroutine +      forI: =0; I < self. number; i++ { A go func () { at              for { -Task, OK: = <-Self . Queue -                 if!OK { -                      Break -                 } -  inERR: =Task () -Self.result <-Err to             } +         }() -     } the  *     //get the execution results for each work $      forJ: =0; J < Self. Total; J + + {Panax NotoginsengRes, OK: = <-Self.result -         if!OK { the              Break +         } A  the         ifRes! =Nil { + FMT. Println (RES) -         } $     } $  -     //All tasks are completed, callback functions -     ifSelf.finishcallback! =Nil { the Self.finishcallback () -     }Wuyi } the  - //close the door . WuFunc (Self *Goroutinepool) Stop () { - Close (self. Queue) About Close (Self.result) $ } -  - //Add a task -Func (Self *Goroutinepool) AddTask (Task func () error) { ASelf. Queue <-Task + } the  - //Set End Callback $Func (Self *Goroutinepool) Setfinishcallback (callback func ()) { theSelf.finishcallback =Callback the}

Open 3 Threads and download the test code for 10 files:

1 func download_test () {2URLs: = []string{3         "Http://dlsw.baidu.com/sw-search-sp/soft/44/17448/Baidusd_Setup_4.2.0.7666.1436769697.exe",4         "Http://dlsw.baidu.com/sw-search-sp/soft/3a/12350/QQ_V7.4.15197.0_setup.1436951158.exe",5         "Http://dlsw.baidu.com/sw-search-sp/soft/9d/14744/ChromeStandalone_V43.0.2357.134_Setup.1436927123.exe ",6         "Http://dlsw.baidu.com/sw-search-sp/soft/6f/15752/iTunes_V12.2.1.16_Setup.1436855012.exe",7         "Http://dlsw.baidu.com/sw-search-sp/soft/70/17456/BaiduAn_Setup_5.0.0.6747.1435912002.exe",8         "Http://dlsw.baidu.com/sw-search-sp/soft/40/12856/QIYImedia_1_06_v4.0.0.32.1437470004.exe",9         "Http://dlsw.baidu.com/sw-search-sp/soft/42/37473/BaiduSoftMgr_Setup_7.0.0.1274.1436770136.exe",Ten         "Http://dlsw.baidu.com/sw-search-sp/soft/49/16988/YoudaoNote_V4.1.0.300_setup.1429669613.exe", One         "Http://dlsw.baidu.com/sw-search-sp/soft/55/11339/bdbrowserSetup-7.6.100.2089-1212_11000003.1437029629.exe ", A         "Http://dlsw.baidu.com/sw-search-sp/soft/53/21734/91zhushoupc_Windows_V5.7.0.1633.1436844901.exe", -     } -  thePool: =New(Goroutinepool) -Pool. Init (3, Len (URLs)) -  -      forI: =range URLs { +URL: =Urls[i] - pool. AddTask (func () error { +             returnDownload (URL) A         }) at     } -  -Isfinish: =false -  - pool. Setfinishcallback (func () { -Func (Isfinish *BOOL) { in*isfinish =true -} (&isfinish) to     }) +  - pool. Start () the  *      for!Isfinish { $Time. Sleep (Time.millisecond * -)Panax Notoginseng     } -  the pool. Stop () +Fmt. Println ("all operations are complete! ") A } the  +Func Download (URLstring) Error { -Fmt. Println ("Start Download ...", URL) $  $SP: = strings. Split (URL,"/") -FileName: = Sp[len (sp)-1] -  theFile, err: = OS. Create ("/users/staff/documents/red_test/aaaa/"+filename) -     ifErr! =Nil {Wuyi         returnErr the     } -  WuRes, err: =http. Get (URL) -     ifErr! =Nil { About         returnErr $     } -  -Length, err: =io. Copy (file, Res. Body) -     ifErr! =Nil { A         returnErr +     } the  -Fmt. Println ("# # Download Complete! "Url"file Length:", Length) $     returnNil the}

It's OK to use it ...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.