<?xml version="1.0" encoding="utf-8" ?>
<?xml-stylesheet type="text/xsl" href="RSS_xslt_style.asp" version="1.0" ?>
<rss version="2.0" xmlns:WebWizForums="http://syndication.webwiz.co.uk/rss_namespace/">
 <channel>
  <title>DevForce Community Forum : Multi-Threading In a DevForce Application</title>
  <link>http://www.ideablade.com/forum/</link>
  <description>This is an XML content feed of; DevForce Community Forum : DevForce Classic : Multi-Threading In a DevForce Application</description>
  <pubDate>Fri, 12 Jun 2026 11:24:12 -700</pubDate>
  <lastBuildDate>Wed, 06 Jun 2007 12:35:39 -700</lastBuildDate>
  <docs>http://blogs.law.harvard.edu/tech/rss</docs>
  <generator>Web Wiz Forums 9.69</generator>
  <ttl>360</ttl>
  <WebWizForums:feedURL>www.ideablade.com/forum/RSS_post_feed.asp?TID=41</WebWizForums:feedURL>
  <image>
   <title>DevForce Community Forum</title>
   <url>http://www.ideablade.com/forum/forum_images/IdeaBlade_logo_tm.png</url>
   <link>http://www.ideablade.com/forum/</link>
  </image>
  <item>
   <title>Multi-Threading In a DevForce Application : Answer: Let&#8217;s begin our discussion...</title>
   <link>http://www.ideablade.com/forum/forum_posts.asp?TID=41&amp;PID=105#105</link>
   <description>
    <![CDATA[<strong>Author:</strong> <a href="http://www.ideablade.com/forum/member_profile.asp?PF=21" rel="nofollow">IdeaBlade</a><br /><strong>Subject:</strong> 41<br /><strong>Posted:</strong> 06-Jun-2007 at 12:35pm<br /><br /><P style="MARGIN: 0in 0in 0pt" ="Questi&#111;n"><strong><EM><FONT face="Times New Roman" size=3>Answer:</FONT></EM></strong></P><P style="MARGIN: 8pt 0in 0pt" ="Ms&#111;normal"><SPAN style="mso-bidi-font-size: 10.0pt"><FONT size=2><FONT face="Times New Roman">Let’s begin our discussion of multi-threading with a definition of thread-safety:<?:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></O:P></FONT></FONT></SPAN></P><P style="MARGIN: 8pt 0.5in 0pt" ="Ms&#111;normal"><SPAN style="FONT-SIZE: 8.5pt; COLOR: black"><FONT face="Times New Roman">For a class to be thread-safe, it first must behave correctly in a single-threaded environment. If a class is correctly implemented, which is another way of saying that it conforms to its specification, no sequence of operations (reads or writes of public fields and calls to public methods) on objects of that class should be able to put the object into an invalid state, observe the object to be in an invalid state, or violate any of the class's invariants, preconditions, or postconditions.<o:p></O:P></FONT></SPAN></P><P style="MARGIN: 8pt 0.5in 0pt" ="Ms&#111;normal"><SPAN style="FONT-SIZE: 8.5pt; COLOR: black"><FONT face="Times New Roman">Furthermore, for a class to be thread-safe, it must continue to behave correctly, in the sense described above, when accessed from multiple threads, regardless of the scheduling or interleaving of the execution of those threads by the runtime environment, without any additional synchronization on the part of the calling code. The effect is that operations on a thread-safe object will appear to all threads to occur in a fixed, globally consistent order.<o:p></O:P></FONT></SPAN></P><P style="MARGIN: 8pt 0.5in 0pt" ="Ms&#111;normal"><SPAN style="FONT-SIZE: 8.5pt; COLOR: black"><FONT face="Times New Roman">The relationship between correctness and thread safety is very similar to the relationship between consistency and isolation used when describing ACID (atomicity, consistency, isolation, and durability) transactions: from the perspective of a given thread, it appears that operations on the object performed by different threads execute sequentially (albeit in a nondeterministic order) rather than in parallel. </FONT><a href="http://www.ideablade.com/Forum/RTE_&#116;extarea.asp?mode=reply&amp;ID=1612#_ftn1" target="_blank"><SPAN ="MsoFootnoteReference"><SPAN style="mso-special-character: footnote"><SPAN ="MsoFootnoteReference"><SPAN style="FONT-SIZE: 8.5pt; COLOR: black; FONT-FAMILY: 'Times New Roman'; mso-fareast-font-family: 'Times New Roman'; mso-ansi-: EN-US; mso-fareast-: EN-US; mso-bidi-: AR-SA">&#091;1&#093;</SPAN></SPAN></SPAN></SPAN></A><o:p></O:P></SPAN></P><P style="MARGIN: 8pt 0in 0pt" ="Ms&#111;normal"><SPAN style="mso-bidi-font-size: 10.0pt"><FONT size=2><FONT face="Times New Roman">The .NET Framework Class Library documentation for the DataSet class says this about its thread-safety:<o:p></O:P></FONT></FONT></SPAN></P><P style="MARGIN: 8pt 0.5in 0pt" ="Ms&#111;normal"><FONT face="Times New Roman"><SPAN style="FONT-SIZE: 8pt">This type is safe for multithreaded read operations. You must synchronize any write operations.</SPAN><SPAN style="mso-bidi-font-size: 10.0pt"><o:p></O:P></SPAN></FONT></P><P style="MARGIN: 8pt 0in 0pt" ="Ms&#111;normal"><SPAN style="mso-bidi-font-size: 10.0pt"><FONT size=2><FONT face="Times New Roman">The same is true for the DevForce PersistenceManager. To make it thread-safe for write operations would require that we make thread-safe every method therein – and every method of the business objects it manages, including property setters.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>This would increase the PersistenceManager’s complexity – and degrade its performance – significantly.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>Every user of the PersistenceManager, and every use thereof, would incur the performance penalty, whether such users and uses required thread-safety or not.<o:p></O:P></FONT></FONT></SPAN></P><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Times New Roman'; mso-fareast-font-family: 'Times New Roman'; mso-ansi-: EN-US; mso-fareast-: EN-US; mso-bidi-: AR-SA">At least 90% of the use cases that people submit to us for multi-threading involve retrieving data while other operations proceed.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>For this we have provided Asynchronous Queries.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>You call the PersistenceManager’s GetEntitiesAsync() method, and we take care of putting the data retrieval operation on a separate thread so that the rest of your application can continue processing. Any number of such </SPAN><DIV style="mso-element: footnote-list"><BR clear=all><HR align=left width="33%" SIZE=1><DIV id=ftn1 style="mso-element: footnote"><P style="MARGIN: 8pt 0in 0pt" ="Ms&#111;normal"><a href="http://www.ideablade.com/Forum/RTE_&#116;extarea.asp?mode=reply&amp;ID=1612#_ftnref1" target="_blank"><SPAN ="MsoFootnoteReference"><SPAN style="mso-special-character: footnote"><SPAN ="MsoFootnoteReference"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Times New Roman'; mso-bidi-font-size: 12.0pt; mso-fareast-font-family: 'Times New Roman'; mso-ansi-: EN-US; mso-fareast-: EN-US; mso-bidi-: AR-SA">&#091;1&#093;</SPAN></SPAN></SPAN></SPAN></A><FONT face="Times New Roman" size=2> Excerpted from </FONT><SPAN style="FONT-SIZE: 8.5pt; COLOR: black; FONT-FAMILY: Verdana">”</SPAN><FONT size=2><FONT face="Times New Roman"><SPAN style="COLOR: black">Characterizing Thread Safety” by Brian Goetz, available on the web at:</SPAN><SPAN style="FONT-SIZE: 8.5pt; COLOR: black; FONT-FAMILY: Verdana"><o:p></O:P></SPAN></FONT></FONT></P><P style="MARGIN: 6pt 0in 0pt 0.2in" ="MsoFootnoteText"><SPAN style="FONT-FAMILY: Arial"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href="http://www-128.ibm.com/developerworks/java/library/j-jtp09263.html" target="_blank">http://www-128.ibm.com/developerworks/java/library/j-jtp09263.html</A></P><DIV></SPAN></SPAN><o:p><FONT face="Times New Roman"><DIV =Secti&#111;n1><P =Ms&#111;normal style="MARGIN: 8pt 0in 0pt"><SPAN style="mso-bidi-font-size: 10.0pt"><FONT size=2>asynchronous queries can be launched simultaneously.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>You can read about asynchronous queries in the DevForce Developers Guide (in the chapter on Object Persistence), and see sample code in the Asynchronous Queries instructional unit that is shipped with the product.<?:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></O:P></FONT></SPAN></P><P =Ms&#111;normal style="MARGIN: 8pt 0in 0pt"><SPAN style="mso-bidi-font-size: 10.0pt"><FONT size=2>Does this mean that you can’t do multi-threading (other than by using Asynchronous Queries) in a DevForce application?<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>No, it does not.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>It just means that <I style="mso-bidi-font-style: normal">you should never share a single PersistenceManager, or any of the entities it manages in its cache</I>, across multiple threads.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>Let us repeat:<o:p></O:P></FONT></SPAN></P><P =Ms&#111;normal style="MARGIN: 8pt 0in 0pt 0.5in; TEXT-INDENT: -0.25in; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-list: l0 level2 lfo1; tab-stops: list .5in"><SPAN style="FONT-FAMILY: 'Courier New'; mso-fareast-font-family: 'Courier New'"><SPAN style="mso-list: Ignore"><FONT size=2>o</FONT><SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN></SPAN><SPAN style="mso-bidi-font-size: 10.0pt"><FONT size=2>Never share a PersistenceManager across more than one thread.</FONT></SPAN></P><P =Ms&#111;normal style="MARGIN: 8pt 0in 0pt 0.5in; TEXT-INDENT: -0.25in; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-list: l0 level2 lfo1; tab-stops: list .5in"><SPAN style="FONT-FAMILY: 'Courier New'; mso-fareast-font-family: 'Courier New'"><SPAN style="mso-list: Ignore"><FONT size=2>o</FONT><SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN></SPAN><FONT size=2><SPAN style="mso-bidi-font-size: 10.0pt">Never share entities from a given PersistenceManager in more than one thread.</SPAN> </FONT></P><P =Ms&#111;normal style="MARGIN: 8pt 0in 0pt"><SPAN style="mso-bidi-font-size: 10.0pt"><FONT size=2>Note that the problems that occur with multi-threaded applications are, by their very nature, timing-dependent and difficult to diagnose, reproduce, and test for.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>Your multi-threaded process can work successfully for long periods of time, then fail catastrophically when two or more inconsistent changes happen to be made simultaneously.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>You should definitely not count on this failure occurring at a convenient time!<o:p></O:P></FONT></SPAN></P><P =Ms&#111;normal style="MARGIN: 8pt 0in 0pt"><SPAN style="mso-bidi-font-size: 10.0pt"><o:p><FONT size=2>&nbsp;</FONT></O:P></SPAN></P><P =Ms&#111;normal style="MARGIN: 8pt 0in 0pt"><B style="mso-bidi-font-weight: normal"><FONT size=2><SPAN style="mso-bidi-font-size: 10.0pt">If You’re Determined To Do Multi-Threading…</SPAN><o:p></O:P></FONT></B></P><P =Ms&#111;normal style="MARGIN: 8pt 0in 0pt; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto"><SPAN style="mso-bidi-font-size: 10.0pt"><FONT size=2>Be sure you really need&nbsp;multiple threads. Remember, if all you want to do is fetch data asynchronously, you&nbsp;will be fully satisfied with Asynchronous Queries. Don’t mess around&nbsp;with multi-threading if this is all you want to do.</FONT></SPAN></P><P =Ms&#111;normal style="MARGIN: 8pt 0in 0pt; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto"><SPAN style="mso-bidi-font-size: 10.0pt"><FONT size=2>Use caution when writing <I style="mso-bidi-font-style: normal">any</I> multi-threaded app. Don't be&nbsp;lulled into a false sense of confidence just because it is easy to spawn a BackgroundWorker in .NET 2.0. Multi-threading is still hard. The BackgroundWorker made the syntax easy: it did not make good multi-threaded design easier!</FONT></SPAN></P><P =Ms&#111;normal style="MARGIN: 8pt 0in 0pt; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto"><SPAN style="mso-bidi-font-size: 10.0pt"><FONT size=2>If you’re new to multi-threaded programming, work with someone who has significant prior experience doing it, if at all possible.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>If you can’t arrange that, do some serious reading and study on the topic before attempting it on a critical application.</FONT></SPAN></P><P =Ms&#111;normal style="MARGIN: 8pt 0in 0pt; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto"><SPAN style="mso-bidi-font-size: 10.0pt"><FONT size=2>If your multi-threaded aspirations involve DevForce business objects:</FONT></SPAN></P><UL =disc><LI =Ms&#111;normal style="MARGIN: 8pt 0in 0pt; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-list: l0 level1 lfo1; tab-stops: list .5in"><FONT size=2><SPAN style="mso-bidi-font-size: 10.0pt">Use a different PersistenceManager in each thread.</SPAN> <SPAN style="mso-bidi-font-size: 10.0pt">Such&nbsp;PersistenceManagers&nbsp;can do anything a normal PersistenceManager can do; they can fetch (both synchronously and asynchronously), save, and so forth.</SPAN></FONT></LI><LI =Ms&#111;normal style="MARGIN: 8pt 0in 0pt; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-list: l0 level1 lfo1; tab-stops: list .5in"><SPAN style="mso-bidi-font-size: 10.0pt"><FONT size=2>Never use PersistenceManager.DefaultManager when multi-threading – the DefaultManager is “global” to the AppDomain and will be shared among any threads in which it is used.</FONT></SPAN></LI><LI =Ms&#111;normal style="MARGIN: 8pt 0in 0pt; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-list: l0 level1 lfo1; tab-stops: list .5in"><FONT size=2><SPAN style="mso-bidi-font-size: 10.0pt">Never communicate entities across thread boundaries.</SPAN> <SPAN style="mso-bidi-font-size: 10.0pt">If the caller must know about some entities, send&nbsp;a list of PrimaryKeys across the thread boundary in a call back.</SPAN> <SPAN style="mso-bidi-font-size: 10.0pt">Alternatively,&nbsp;you could bury EntitySets in a call back to serialize copies of entities across the thread boundary.</SPAN></FONT></LI></UL></DIV><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Times New Roman'; mso-fareast-font-family: 'Times New Roman'; mso-ansi-: EN-US; mso-fareast-: EN-US; mso-bidi-: AR-SA"><BR style="PAGE-BREAK-BEFORE: always; mso-break-: secti&#111;n-break" clear=all></SPAN><P =Ms&#111;normal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: 13pt"><SPAN style="FONT-SIZE: 9pt"><o:p>&nbsp;</O:P></SPAN></P>&nbsp;</FONT></O:P></DIV></DIV></DIV><span style="font-size:10px"><br /><br />Edited by IdeaBlade - 06-Jun-2007 at 12:37pm</span>]]>
   </description>
   <pubDate>Wed, 06 Jun 2007 12:35:39 -700</pubDate>
   <guid isPermaLink="true">http://www.ideablade.com/forum/forum_posts.asp?TID=41&amp;PID=105#105</guid>
  </item> 
  <item>
   <title>Multi-Threading In a DevForce Application : Question:  Can I use multi-threading...</title>
   <link>http://www.ideablade.com/forum/forum_posts.asp?TID=41&amp;PID=104#104</link>
   <description>
    <![CDATA[<strong>Author:</strong> <a href="http://www.ideablade.com/forum/member_profile.asp?PF=20" rel="nofollow">Customer</a><br /><strong>Subject:</strong> 41<br /><strong>Posted:</strong> 06-Jun-2007 at 12:33pm<br /><br /><P =Questi&#111;n style="MARGIN: 0in 0in 0pt"><strong><EM><FONT face="Times New Roman" size=3>Question: </FONT></EM></strong></P><P =Ms&#111;normal style="MARGIN: 8pt 0in 0pt"><FONT face="Times New Roman" size=2>Can I use multi-threading in a DevForce app?</FONT></P><P =Questi&#111;n style="MARGIN: 0in 0in 0pt"><?:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p><strong><EM><FONT face="Times New Roman" size=3>&nbsp;</FONT></EM></strong></o:p></P>]]>
   </description>
   <pubDate>Wed, 06 Jun 2007 12:33:34 -700</pubDate>
   <guid isPermaLink="true">http://www.ideablade.com/forum/forum_posts.asp?TID=41&amp;PID=104#104</guid>
  </item> 
 </channel>
</rss>