Click here to Skip to main content
15,894,362 members
Articles / .NET

Demystifying concurrent lazy load pattern

Rate me:
Please Sign up or sign in to vote.
5.00/5 (13 votes)
5 Aug 2012CPOL9 min read 43.1K   581   29  
Shows what are common mistakes in lazy load implementations and how to implement fast concurrent lazy load cache.
namespace LazyLoadSample
{
    partial class Simulator
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
			this.label1 = new System.Windows.Forms.Label();
			this.numThreads = new System.Windows.Forms.NumericUpDown();
			this.numReadsPerThread = new System.Windows.Forms.NumericUpDown();
			this.label2 = new System.Windows.Forms.Label();
			this.button1 = new System.Windows.Forms.Button();
			this.numResetsAfterReads = new System.Windows.Forms.NumericUpDown();
			this.label3 = new System.Windows.Forms.Label();
			this.cmbCacheImplementation = new System.Windows.Forms.ComboBox();
			this.label4 = new System.Windows.Forms.Label();
			this.numReadTime = new System.Windows.Forms.NumericUpDown();
			this.label6 = new System.Windows.Forms.Label();
			this.progressBar = new System.Windows.Forms.ProgressBar();
			this.label9 = new System.Windows.Forms.Label();
			this.lblTime = new System.Windows.Forms.Label();
			this.lblStatus = new System.Windows.Forms.Label();
			this.label11 = new System.Windows.Forms.Label();
			this.label7 = new System.Windows.Forms.Label();
			this.lblThreadsStatus = new System.Windows.Forms.Label();
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.label10 = new System.Windows.Forms.Label();
			this.label8 = new System.Windows.Forms.Label();
			this.label5 = new System.Windows.Forms.Label();
			this.lblLoads = new System.Windows.Forms.Label();
			this.label12 = new System.Windows.Forms.Label();
			this.lblDescription = new System.Windows.Forms.Label();
			this.label13 = new System.Windows.Forms.Label();
			((System.ComponentModel.ISupportInitialize)(this.numThreads)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.numReadsPerThread)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.numResetsAfterReads)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.numReadTime)).BeginInit();
			this.groupBox1.SuspendLayout();
			this.SuspendLayout();
			// 
			// label1
			// 
			this.label1.AutoSize = true;
			this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
			this.label1.Location = new System.Drawing.Point(7, 23);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(151, 13);
			this.label1.TabIndex = 1;
			this.label1.Text = "Number of concurrent threads:";
			// 
			// numThreads
			// 
			this.numThreads.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
			this.numThreads.Location = new System.Drawing.Point(164, 21);
			this.numThreads.Maximum = new decimal(new int[] {
            1000000,
            0,
            0,
            0});
			this.numThreads.Minimum = new decimal(new int[] {
            1,
            0,
            0,
            0});
			this.numThreads.Name = "numThreads";
			this.numThreads.Size = new System.Drawing.Size(88, 20);
			this.numThreads.TabIndex = 2;
			this.numThreads.Value = new decimal(new int[] {
            100,
            0,
            0,
            0});
			// 
			// numReadsPerThread
			// 
			this.numReadsPerThread.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
			this.numReadsPerThread.Location = new System.Drawing.Point(164, 47);
			this.numReadsPerThread.Maximum = new decimal(new int[] {
            10000,
            0,
            0,
            0});
			this.numReadsPerThread.Minimum = new decimal(new int[] {
            1,
            0,
            0,
            0});
			this.numReadsPerThread.Name = "numReadsPerThread";
			this.numReadsPerThread.Size = new System.Drawing.Size(88, 20);
			this.numReadsPerThread.TabIndex = 4;
			this.numReadsPerThread.Value = new decimal(new int[] {
            100,
            0,
            0,
            0});
			// 
			// label2
			// 
			this.label2.AutoSize = true;
			this.label2.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
			this.label2.Location = new System.Drawing.Point(19, 49);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(139, 13);
			this.label2.TabIndex = 3;
			this.label2.Text = "Number of reads per thread:";
			// 
			// button1
			// 
			this.button1.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
			this.button1.Location = new System.Drawing.Point(130, 205);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(147, 33);
			this.button1.TabIndex = 5;
			this.button1.Text = "Start Simulation";
			this.button1.UseVisualStyleBackColor = true;
			this.button1.Click += new System.EventHandler(this.ButtonClickHandler);
			// 
			// numResetsAfterReads
			// 
			this.numResetsAfterReads.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
			this.numResetsAfterReads.Location = new System.Drawing.Point(164, 99);
			this.numResetsAfterReads.Maximum = new decimal(new int[] {
            10000,
            0,
            0,
            0});
			this.numResetsAfterReads.Name = "numResetsAfterReads";
			this.numResetsAfterReads.Size = new System.Drawing.Size(88, 20);
			this.numResetsAfterReads.TabIndex = 7;
			this.numResetsAfterReads.Value = new decimal(new int[] {
            50,
            0,
            0,
            0});
			// 
			// label3
			// 
			this.label3.AutoSize = true;
			this.label3.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
			this.label3.Location = new System.Drawing.Point(66, 104);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(92, 13);
			this.label3.TabIndex = 6;
			this.label3.Text = "Reset cache after";
			// 
			// cmbCacheImplementation
			// 
			this.cmbCacheImplementation.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
			this.cmbCacheImplementation.FormattingEnabled = true;
			this.cmbCacheImplementation.Items.AddRange(new object[] {
            "1   - Simple Lazy Load",
            "2   - Naive Locking",
            "3   - If-Lock-If",
            "4   - Locking On Data Read",
            "4a - Locking On Data Read (with 1ms delay)",
            "5   - Good",
            "5a - Good (with 1ms delay)",
            "5b - Lazy<T>",
            "5c - Custom Lazy<T>"});
			this.cmbCacheImplementation.Location = new System.Drawing.Point(164, 6);
			this.cmbCacheImplementation.Name = "cmbCacheImplementation";
			this.cmbCacheImplementation.Size = new System.Drawing.Size(234, 21);
			this.cmbCacheImplementation.TabIndex = 8;
			this.cmbCacheImplementation.SelectedIndexChanged += new System.EventHandler(this.CacheSelectedIndexChangedHandler);
			// 
			// label4
			// 
			this.label4.AutoSize = true;
			this.label4.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
			this.label4.Location = new System.Drawing.Point(12, 9);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(146, 13);
			this.label4.TabIndex = 9;
			this.label4.Text = "Cache implementation to test:";
			// 
			// numReadTime
			// 
			this.numReadTime.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
			this.numReadTime.Location = new System.Drawing.Point(164, 73);
			this.numReadTime.Maximum = new decimal(new int[] {
            10000,
            0,
            0,
            0});
			this.numReadTime.Minimum = new decimal(new int[] {
            1,
            0,
            0,
            0});
			this.numReadTime.Name = "numReadTime";
			this.numReadTime.Size = new System.Drawing.Size(88, 20);
			this.numReadTime.TabIndex = 13;
			this.numReadTime.Value = new decimal(new int[] {
            100,
            0,
            0,
            0});
			// 
			// label6
			// 
			this.label6.AutoSize = true;
			this.label6.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
			this.label6.Location = new System.Drawing.Point(20, 76);
			this.label6.Name = "label6";
			this.label6.Size = new System.Drawing.Size(138, 13);
			this.label6.TabIndex = 12;
			this.label6.Text = "Time it takes to load cache:";
			// 
			// progressBar
			// 
			this.progressBar.Location = new System.Drawing.Point(15, 255);
			this.progressBar.Name = "progressBar";
			this.progressBar.Size = new System.Drawing.Size(383, 23);
			this.progressBar.TabIndex = 18;
			// 
			// label9
			// 
			this.label9.AutoSize = true;
			this.label9.Location = new System.Drawing.Point(511, 175);
			this.label9.Name = "label9";
			this.label9.Size = new System.Drawing.Size(70, 13);
			this.label9.TabIndex = 20;
			this.label9.Text = "Elapsed time:";
			// 
			// lblTime
			// 
			this.lblTime.AutoSize = true;
			this.lblTime.Location = new System.Drawing.Point(587, 175);
			this.lblTime.Name = "lblTime";
			this.lblTime.Size = new System.Drawing.Size(40, 13);
			this.lblTime.TabIndex = 21;
			this.lblTime.Text = "lblTime";
			// 
			// lblStatus
			// 
			this.lblStatus.AutoSize = true;
			this.lblStatus.Location = new System.Drawing.Point(587, 265);
			this.lblStatus.Name = "lblStatus";
			this.lblStatus.Size = new System.Drawing.Size(47, 13);
			this.lblStatus.TabIndex = 23;
			this.lblStatus.Text = "lblStatus";
			// 
			// label11
			// 
			this.label11.AutoSize = true;
			this.label11.Location = new System.Drawing.Point(540, 265);
			this.label11.Name = "label11";
			this.label11.Size = new System.Drawing.Size(41, 13);
			this.label11.TabIndex = 22;
			this.label11.Text = "Reads:";
			// 
			// label7
			// 
			this.label7.AutoSize = true;
			this.label7.Location = new System.Drawing.Point(489, 205);
			this.label7.Name = "label7";
			this.label7.Size = new System.Drawing.Size(92, 13);
			this.label7.TabIndex = 25;
			this.label7.Text = "Running Threads:";
			// 
			// lblThreadsStatus
			// 
			this.lblThreadsStatus.AutoSize = true;
			this.lblThreadsStatus.Location = new System.Drawing.Point(587, 205);
			this.lblThreadsStatus.Name = "lblThreadsStatus";
			this.lblThreadsStatus.Size = new System.Drawing.Size(86, 13);
			this.lblThreadsStatus.TabIndex = 26;
			this.lblThreadsStatus.Text = "lblThreadsStatus";
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.Add(this.label13);
			this.groupBox1.Controls.Add(this.label10);
			this.groupBox1.Controls.Add(this.label8);
			this.groupBox1.Controls.Add(this.numReadTime);
			this.groupBox1.Controls.Add(this.label2);
			this.groupBox1.Controls.Add(this.numReadsPerThread);
			this.groupBox1.Controls.Add(this.label3);
			this.groupBox1.Controls.Add(this.numResetsAfterReads);
			this.groupBox1.Controls.Add(this.label6);
			this.groupBox1.Controls.Add(this.numThreads);
			this.groupBox1.Controls.Add(this.label1);
			this.groupBox1.Location = new System.Drawing.Point(423, 6);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(302, 150);
			this.groupBox1.TabIndex = 27;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "Load Settings";
			// 
			// label10
			// 
			this.label10.AutoSize = true;
			this.label10.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
			this.label10.Location = new System.Drawing.Point(260, 76);
			this.label10.Name = "label10";
			this.label10.Size = new System.Drawing.Size(23, 13);
			this.label10.TabIndex = 30;
			this.label10.Text = "ms.";
			// 
			// label8
			// 
			this.label8.AutoSize = true;
			this.label8.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
			this.label8.Location = new System.Drawing.Point(259, 104);
			this.label8.Name = "label8";
			this.label8.Size = new System.Drawing.Size(36, 13);
			this.label8.TabIndex = 25;
			this.label8.Text = "reads.";
			// 
			// label5
			// 
			this.label5.AutoSize = true;
			this.label5.Location = new System.Drawing.Point(520, 235);
			this.label5.Name = "label5";
			this.label5.Size = new System.Drawing.Size(61, 13);
			this.label5.TabIndex = 28;
			this.label5.Text = "Data loads:";
			// 
			// lblLoads
			// 
			this.lblLoads.AutoSize = true;
			this.lblLoads.Location = new System.Drawing.Point(587, 235);
			this.lblLoads.Name = "lblLoads";
			this.lblLoads.Size = new System.Drawing.Size(46, 13);
			this.lblLoads.TabIndex = 29;
			this.lblLoads.Text = "lblLoads";
			// 
			// label12
			// 
			this.label12.AutoSize = true;
			this.label12.Location = new System.Drawing.Point(95, 34);
			this.label12.Name = "label12";
			this.label12.Size = new System.Drawing.Size(63, 13);
			this.label12.TabIndex = 30;
			this.label12.Text = "Description:";
			// 
			// lblDescription
			// 
			this.lblDescription.AutoEllipsis = true;
			this.lblDescription.Location = new System.Drawing.Point(164, 34);
			this.lblDescription.Name = "lblDescription";
			this.lblDescription.Size = new System.Drawing.Size(234, 122);
			this.lblDescription.TabIndex = 31;
			this.lblDescription.Text = "lblDescription";
			// 
			// label13
			// 
			this.label13.AutoSize = true;
			this.label13.Location = new System.Drawing.Point(146, 122);
			this.label13.Name = "label13";
			this.label13.Size = new System.Drawing.Size(149, 13);
			this.label13.TabIndex = 32;
			this.label13.Text = "( if zero, cache is never reset )";
			// 
			// Simulator
			// 
			this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
			this.ClientSize = new System.Drawing.Size(731, 303);
			this.Controls.Add(this.lblDescription);
			this.Controls.Add(this.label12);
			this.Controls.Add(this.lblLoads);
			this.Controls.Add(this.label5);
			this.Controls.Add(this.groupBox1);
			this.Controls.Add(this.lblThreadsStatus);
			this.Controls.Add(this.label7);
			this.Controls.Add(this.lblStatus);
			this.Controls.Add(this.label11);
			this.Controls.Add(this.lblTime);
			this.Controls.Add(this.cmbCacheImplementation);
			this.Controls.Add(this.label4);
			this.Controls.Add(this.label9);
			this.Controls.Add(this.progressBar);
			this.Controls.Add(this.button1);
			this.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
			this.Name = "Simulator";
			this.Text = "Lazy Load Simulator";
			((System.ComponentModel.ISupportInitialize)(this.numThreads)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.numReadsPerThread)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.numResetsAfterReads)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.numReadTime)).EndInit();
			this.groupBox1.ResumeLayout(false);
			this.groupBox1.PerformLayout();
			this.ResumeLayout(false);
			this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.NumericUpDown numThreads;
        private System.Windows.Forms.NumericUpDown numReadsPerThread;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.NumericUpDown numResetsAfterReads;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.ComboBox cmbCacheImplementation;
		private System.Windows.Forms.Label label4;
        private System.Windows.Forms.NumericUpDown numReadTime;
		private System.Windows.Forms.Label label6;
		private System.Windows.Forms.ProgressBar progressBar;
        private System.Windows.Forms.Label label9;
        private System.Windows.Forms.Label lblTime;
        private System.Windows.Forms.Label lblStatus;
		private System.Windows.Forms.Label label11;
		private System.Windows.Forms.Label label7;
		private System.Windows.Forms.Label lblThreadsStatus;
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.Label label8;
		private System.Windows.Forms.Label label5;
		private System.Windows.Forms.Label lblLoads;
		private System.Windows.Forms.Label label10;
		private System.Windows.Forms.Label label12;
		private System.Windows.Forms.Label lblDescription;
		private System.Windows.Forms.Label label13;
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) CallidusCloud
Serbia Serbia
I am a software developer at CallidusCloud currently working on software for Quoting and Product Configuration.

In past few years I have been working on development of multi-portal CMS and I was responsible for defining Coding standard and Code Review process. For three years, I have lead team of programmers that developed Soprex framework for enterprise applications development and I have also built Soprex Quotation Tool on that framework. My main points of interests are enterprise app architecture, Scrum and TDD.

I blogs about software development at www.Vukoje.NET.

Comments and Discussions