Golang sync. Pool

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

Add a new feature in the Sync Pack for Go 1.3: Pool.

Official documents can be seen here http://golang.org/pkg/sync/#Pool

The purpose of this class of design is to save and reuse temporary objects to reduce memory allocations and reduce CG pressure.

Type Pool

Func (P *pool) Get () interface{}  func (P *pool) Put (x interface{})  New func () interface{}

Get returns any object in the pool.

If the pool is empty, call new to return a newly created object.

If new is not set, nil is returned.

Another important feature is that the objects that are placed in the pool will be recycled when you don't know when to go.

So if you put in 100 objects in advance, it is possible to find the pool is empty the next time you get it.

But one of the benefits of this feature is that you don't have to worry about the pool growing because go has helped you recycle the pool.

This cleanup process is done before each garbage collection. Garbage collection is triggered at a fixed two-minute time.

And every cleanup will clean up all the objects in the pool!

Package Main
Import

"Sync" "Log"

)
Func Main () {

Build object var pipe = &sync. Pool{new:func () Interface{}{return "hello,beijing"}}//ready to put the string val: = "hello,world!" into the pipe. Put (val)//Remove log. PRINTLN (pipe. Get ())//re-fetch is not, will automatically call NEWLOG.PRINTLN (pipe. Get ())

}
Output
2014/09/30 15:43:30 hello,world!
2014/09/30 15:43:30 hello,beijing

Related Article

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.