Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If your StockModel.Quotation class allows for it, you could create a pool to limit the number of new objects created. This is a technique they sometimes use in games to prevent the garbage collector stalling in the middle of renders.</p> <p>Here's a basic pool implementation:</p> <pre><code> class StockQuotationPool { private List&lt;StockQuotation&gt; poolItems; private volatile int itemsInPool; public StockQuotationPool(int poolSize) { this.poolItems = new List&lt;StockQuotation&gt;(poolSize); this.itemsInPool = poolSize; } public StockQuotation Create(string name, decimal value) { if (this.itemsInPool == 0) { // Block until new item ready - maybe use semaphore. throw new NotImplementedException(); } // Items are in the pool, but no items have been created. if (this.poolItems.Count == 0) { this.itemsInPool--; return new StockQuotation(name, value); } // else, return one in the pool this.itemsInPool--; var item = this.poolItems[0]; this.poolItems.Remove(item); item.Name = name; item.Value = value; return item; } public void Release(StockQuotation quote) { if (!this.poolItems.Contains(quote) { this.poolItems.Add(quote); this.itemsInPool++; } } } </code></pre> <p>That's assuming that the StockQuotation looks something like this:</p> <pre><code> class StockQuotation { internal StockQuotation(string name, decimal value) { this.Name = name; this.Value = value; } public string Name { get; set; } public decimal Value { get; set; } } </code></pre> <p>Then instead of calling the new StockQuotation() constructor, you ask the pool for a new instance. The pool returns an existing instance (you can precreate them if you want) and sets all the properties so that it looks like a new instance. You may need to play around until you find a pool size that is large enough to accommodate the threads at the same time.</p> <p>Here's how you'd call it from the thread.</p> <pre><code> // Get the pool, maybe from a singleton. var pool = new StockQuotationPool(100); var quote = pool.Create("test", 1.00m); try { // Work with quote } finally { pool.Release(quote); } </code></pre> <p>Lastly, this class isn't thread safe at the moment. Let me know if you need any help with making it so.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload