原文: 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. MySQL的join on和 where 的执行顺序和区别,以及各种连接说明

    目录 1.各种连接的解读说明: 1.1.各种连接的含义和说明 1.1.1 所有连接分类 1.1.2 left join 和 left outer join 区别 1.2.神图参考 1.4.一些参考说明 ...

  2. PHP中的重载技术

    PHP中的重载技术 通常面向对象语言的重载技术 其基本语法是这样的: 在一个类中,有多个同名的方法,每个方法的参数不同而已.这种现象就称为“重载”. 参数不同可以是:数量个数不同,或类型不同,或顺序不 ...

  3. java jri null

    java通过jri调用r文件,r文件必须和当前类在同一目录下,然后才能re.eval("source(fpath)")执行脚本;其中fpath为通过re.assign设置的文件全路 ...

  4. [转帖]MyCat教程【简单介绍】

    MyCat教程[简单介绍] 2019-10-15 10:27:23 波波烤鸭 阅读数 618 版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明. ...

  5. [Xamarin] - Xamarin.Forms Project with .Net Standard 2.0

    1. Install .NET Core 2.0 SDK .https://www.microsoft.com/net/download/core 2. Install Android 7.1 (AP ...

  6. java当中请给出一个oracle的helloworld例子

    [学习笔记] 2.oracle的helloworld例子: import java.sql.*;public class OracleHello{    public static void main ...

  7. c#序列化基类(包含派生类继承DynamicObject和 IXmlSerializable)对象

    直接上代码 using System.Diagnostics; using System.Text; using System.Xml; using System.Xml.Schema; using ...

  8. STM32之SPI时钟相位选择

    SPI的时钟模式分为四种,由SPI_CR1寄存器的两位CPOL,CPHA组合选择. CPOL 如果为1,则时钟的空闲电平为高电平:CPOL 如果为0,则时钟的空闲电平为低电平.空闲电平影响不大. CP ...

  9. C++_对象数组与对象指针

    对象数组与对象指针 1. 对象数组 所谓对象数组是指每一数组元素都是对象的数组, 也就是说,若一个类有若干个对象,则把这一系列的对象用一个数组来存放. 对象数组的元素是对象, 不仅具有数据成员,而且还 ...

  10. PAT(B) 1057 数零壹(Java)字符串

    题目链接:1057 数零壹 (20 point(s)) 题目描述 给定一串长度不超过 10​5​​ 的字符串,本题要求你将其中所有英文字母的序号(字母 a-z 对应序号 1-26,不分大小写)相加,得 ...