原文: https://golangcode.com/run-code-once-with-sync/

---------------------------------------------------

Using a web server as an example, there are multiple stages you can load resources. Within the main() function and within the handler are the obvious two - each with their own advantages and disadvantages. Within the main function can hinder the start-up time of the server, while code within the handler is run on every request.

Sometimes we want to load a resource only once and when it’s first needed. This means it needs to be in the http handler. We have to be careful how we do this though, as each time the handler is called it will be in a separate goroutine. We could use a global variable and if it hasn’t been set (or == nil), load the resource - but this won’t be safe from a concurrency point of view as you could end up running the load sequence twice.

The best way to achieve this is therefore using the sync.Once mutex to efficiently run code once even across goroutines. The example below should demonstrate this.

package main

import (
"fmt"
"sync"
) var doOnce sync.Once func main() {
DoSomething()
DoSomething()
} func DoSomething() {
doOnce.Do(func() {
fmt.Println("Run once - first time, loading...")
})
fmt.Println("Run this every time")
}

Screenshot of without and with the discard option:

Run Code Once on First Load (Concurrency Safe)的更多相关文章

  1. Specifying the Code to Run on a Thread

    This lesson shows you how to implement a Runnable class, which runs the code in its Runnable.run() m ...

  2. Instant Run

    http://tools.android.com/tech-docs/instant-run N Developer Preview users: Instant Run is currently i ...

  3. $( document ).ready()&$(window).load()

    $( document ).ready() https://learn.jquery.com/using-jquery-core/document-ready/ A page can't be man ...

  4. (4)事件处理——(3)代码执行的顺序(Timing of code execution)

    In Chapter 1, Getting Started, we noted that $(document).ready()was jQuery's primary way to perform ...

  5. Run Nutch In Eclipse on Linux and Windows nutch version 0.9

    Running Nutch in Eclipse Here are instructions for setting up a development environment for Nutch un ...

  6. How to Debug Enterprise Portal Code in Dynamics AX 2009

    转载 To set up debugging for pages1. Log into the server that is running the AOS.2. Open the Microsoft ...

  7. Thread的run()与start()的区别

    Java的线程是通过java.lang.Thread类来实现的.VM启动时会有一个由主方法所定义的线程.可以通过创建Thread的实例来创建新的线程.每个线程都是通过某个特定Thread对象所对应的方 ...

  8. Thread’s start method and run method

    工作中用到了Thread,一开始用错了,仔细研究了一下,稍作整理. 前言,今天写代码居然这样写的 new Thread() { @Override public void run() { System ...

  9. 细说OC中的load和initialize方法

    OC中有两个特殊的类方法,分别是load和initialize.本文总结一下这两个方法的区别于联系.使用场景和注意事项.Demo可以在我的Github上找到--load和initialize,如果觉得 ...

随机推荐

  1. Intellij IDEA Debug 多模块下断点目标源问题

    Ctrl+Alt+S进入Settings,选择Debugger->show alternative source switcher 勾选,开启显示替代源选择器 由于项目工程存放了多个应用模块,包 ...

  2. centos下通过yum安装redis-cli

    redis在第三方的源里,首先添加源,最常用的是epel源 yum install epel-release 然后就可以安装redis了 yum install -y redis 安装完成以后配置文件 ...

  3. python:如何传递一个列表参数

  4. robot:接口入参为图片时如何发送请求

    https://www.cnblogs.com/changyou615/p/8776507.html 接口是上传图片,通过F12抓包获得如下信息 由于使用的是RequestsLibrary,所以先看一 ...

  5. npm i node-sass 报错&npm 镜像切换

    npm install --save node-sass --registry=https://registry.npm.taobao.org --disturl=https://npm.taobao ...

  6. Quartz.Net—配置化

    Schedule配置 线程数量 如果一个Schedule中有很多任务,这样默认的10个线程就不够用了. 有很多种方法配置线程的个数. 工厂构造函数 webfonfig quartzconfig 环境变 ...

  7. Git使用总结(三):协同开发常见冲突

    1.不同人修改了不同的文件 a.账户A,账户B分别从远端拉取了相同分支     b.账户A修改了main.cpp文件后提交到远端,账户B修改fun.cpp文件提交远端时会报如下错误           ...

  8. Python进阶:并发编程之Futures

    区分并发和并行 并发(Concurrency). 由于Python 的解释器并不是线程安全的,为了解决由此带来的 race condition 等问题,Python 便引入了全局解释器锁,也就是同一时 ...

  9. Android Manifest 中 uses-feature 和 uses-permission的作用 关系和区别

    Manifest中的 <uses-permission android:name="android.permission.CAMERA" /> 和 <uses-f ...

  10. 剪贴板神器:Ditto

    ditto – 善用佳软 免费开源的 Windows 管理剪贴板,让你处理文字更高效:Ditto - 少数派