1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<title>WiredTiger: Multithreading in Java</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
$(document).ready(initResizable);
</script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
<link href="wiredtiger.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
<tbody>
<tr style="height: 56px;">
<td id="projectlogo"><a href="http://wiredtiger.com/"><img alt="Logo" src="LogoFinal-header.png" alt="WiredTiger" /></a></td>
<td style="padding-left: 0.5em;">
<div id="projectname">
 <span id="projectnumber">Version 3.2.1</span>
</div>
<div id="projectbrief"><!-- 3.2.1 --></div>
</td>
</tr>
</tbody>
</table>
</div>
<div class="banner">
<a href="https://github.com/wiredtiger/wiredtiger">Fork me on GitHub</a>
<a class="last" href="http://groups.google.com/group/wiredtiger-users">Join my user group</a>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.13 -->
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
$(function() {
initMenu('',false,false,'search.php','Search');
});
</script>
<div id="main-nav"></div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
<div id="nav-tree">
<div id="nav-tree-contents">
<div id="nav-sync" class="sync"></div>
</div>
</div>
<div id="splitbar" style="-moz-user-select:none;"
class="ui-resizable-handle">
</div>
</div>
<script type="text/javascript">
$(document).ready(function(){initNavTree('threads_lang_java.html','');});
</script>
<div id="doc-content">
<div class="header">
<div class="headertitle">
<div class="title">Multithreading in Java </div> </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><p>All Connection methods are thread safe, and Connection handles can be shared between threads. Applications typically open a single connection to each database, per process. Multi-threaded programs must wait for all other threads to exit before closing the Connection handle because that will implicitly close all other handles. Alternatively, a thread can set an event handler using Connection.open_session to be notified when a Connection close is in progress.</p>
<p>Session and Cursor methods are not thread safe and Session and Cursor handles cannot be accessed concurrently by multiple threads. Applications typically open one Session handle for each thread accessing a database, and then one or more Cursor handles within the session.</p>
<p>Session and Cursor methods may be accessed by different threads serially (for example, a pool of threads managed by the application with a set of shared session or cursor handles). There is no thread-local state in WiredTiger, and no built-in synchronization of session or cursor handles, either, so if multiple threads access a session or cursor handle, access must be serialized by the application.</p>
<p>AsyncOp methods are not thread-safe, and must be accessed by only a single thread at a time. AsyncOp methods may be accessed by different threads serially (and that is expected to happen when the asynchronous callback function runs).</p>
<h1><a class="anchor" id="threads_example_lang_java"></a>
Code samples</h1>
<p>The code below is taken from the complete example program ex_thread.java.</p>
<p>This is an example of a thread entry point. A new session is opened for the thread and used for all operations within that thread.</p>
<div class="fragment"><div class="line"><span class="keyword">class </span>ScanThread <span class="keyword">extends</span> Thread {</div><div class="line"> <span class="keyword">private</span> Connection conn;</div><div class="line"></div><div class="line"> <span class="keyword">public</span> ScanThread(Connection conn) {</div><div class="line"> this.conn = conn;</div><div class="line"> }</div><div class="line"></div><div class="line"> <span class="keyword">public</span> <span class="keywordtype">void</span> run()</div><div class="line"> {</div><div class="line"> <span class="keywordflow">try</span> {</div><div class="line"> <span class="keywordtype">int</span> ret;</div><div class="line"></div><div class="line"> Session session = conn.open_session(null);</div><div class="line"> Cursor cursor = session.open_cursor(<span class="stringliteral">"table:access"</span>, null, null);</div><div class="line"></div><div class="line"> <span class="comment">/* Show all records. */</span></div><div class="line"> <span class="keywordflow">while</span> ((ret = cursor.next()) == 0) {</div><div class="line"> String key = cursor.getKeyString();</div><div class="line"> String value = cursor.getValueString();</div><div class="line"> System.out.println(<span class="stringliteral">"Got record: "</span> + key + <span class="stringliteral">" : "</span> + value);</div><div class="line"> }</div><div class="line"> <span class="keywordflow">if</span> (ret != wiredtiger.WT_NOTFOUND)</div><div class="line"> System.err.println(<span class="stringliteral">"Cursor.next: "</span> +</div><div class="line"> wiredtiger.wiredtiger_strerror(ret));</div><div class="line"> cursor.<a class="code" href="struct_w_t___c_u_r_s_o_r.html#aeea071f192cab12245a50fbe71c3460b">close</a>();</div><div class="line"> session.close(null);</div><div class="line"> } <span class="keywordflow">catch</span> (WiredTigerException wte) {</div><div class="line"> System.err.println(<span class="stringliteral">"Exception "</span> + wte);</div><div class="line"> }</div><div class="line"> }</div><div class="line">}</div></div><!-- fragment --><p> Here is the main function that starts the threads. It opens a single connection, shared between the threads, and closes the connection after waiting for all of the threads to exit.</p>
<div class="fragment"><div class="line"> <span class="keyword">public</span> <span class="keyword">static</span> <span class="keywordtype">void</span> main(String[] argv)</div><div class="line"> {</div><div class="line"> <span class="keywordflow">try</span> {</div><div class="line"> Thread[] threads = <span class="keyword">new</span> Thread[NUM_THREADS];</div><div class="line"> <span class="keywordtype">int</span> i, ret;</div><div class="line"> Connection conn;</div><div class="line"></div><div class="line"> <span class="comment">/*</span></div><div class="line"><span class="comment"> * Create a clean test directory for this run of the test program if the</span></div><div class="line"><span class="comment"> * environment variable isn't already set (as is done by make check).</span></div><div class="line"><span class="comment"> */</span></div><div class="line"> <span class="keywordflow">if</span> (System.getenv(<span class="stringliteral">"WIREDTIGER_HOME"</span>) == null) {</div><div class="line"> home = <span class="stringliteral">"WT_HOME"</span>;</div><div class="line"> <span class="keywordflow">try</span> {</div><div class="line"> Process proc = Runtime.getRuntime().exec(<span class="stringliteral">"/bin/rm -rf "</span> + home);</div><div class="line"> BufferedReader br = <span class="keyword">new</span> BufferedReader(</div><div class="line"> <span class="keyword">new</span> InputStreamReader(proc.getInputStream()));</div><div class="line"> <span class="keywordflow">while</span>(br.ready())</div><div class="line"> System.out.println(br.readLine());</div><div class="line"> br.close();</div><div class="line"> proc.waitFor();</div><div class="line"> <span class="keywordflow">if</span> (!(<span class="keyword">new</span> File(home)).mkdir())</div><div class="line"> System.err.println(<span class="stringliteral">"mkdir: failed"</span>);</div><div class="line"> } <span class="keywordflow">catch</span> (Exception ex) {</div><div class="line"> System.err.println(<span class="stringliteral">"Exception: "</span> + home + <span class="stringliteral">": "</span> + ex);</div><div class="line"> System.exit(1);</div><div class="line"> }</div><div class="line"> } <span class="keywordflow">else</span></div><div class="line"> home = null;</div><div class="line"></div><div class="line"> <span class="keywordflow">if</span> ((conn = wiredtiger.open(home, <span class="stringliteral">"create"</span>)) == null) {</div><div class="line"> System.err.println(<span class="stringliteral">"Error connecting to "</span> + home);</div><div class="line"> System.exit(1);</div><div class="line"> }</div><div class="line"></div><div class="line"> <span class="comment">/* Note: further error checking omitted for clarity. */</span></div><div class="line"></div><div class="line"> Session session = conn.open_session(null);</div><div class="line"> ret = session.create(<span class="stringliteral">"table:access"</span>, <span class="stringliteral">"key_format=S,value_format=S"</span>);</div><div class="line"> Cursor cursor = session.open_cursor(<span class="stringliteral">"table:access"</span>, null, <span class="stringliteral">"overwrite"</span>);</div><div class="line"> cursor.putKeyString(<span class="stringliteral">"key1"</span>);</div><div class="line"> cursor.putValueString(<span class="stringliteral">"value1"</span>);</div><div class="line"> ret = cursor.insert();</div><div class="line"> cursor.close();</div><div class="line"> ret = session.close(null);</div><div class="line"></div><div class="line"> <span class="keywordflow">for</span> (i = 0; i < NUM_THREADS; i++) {</div><div class="line"> threads[i] = <span class="keyword">new</span> ScanThread(conn);</div><div class="line"> threads[i].start();</div><div class="line"> }</div><div class="line"></div><div class="line"> <span class="keywordflow">for</span> (i = 0; i < NUM_THREADS; i++)</div><div class="line"> <span class="keywordflow">try</span> {</div><div class="line"> threads[i].join();</div><div class="line"> ret = -1;</div><div class="line"> }</div><div class="line"> <span class="keywordflow">catch</span> (InterruptedException ie) {</div><div class="line"> }</div><div class="line"></div><div class="line"> ret = conn.close(null);</div><div class="line"> System.exit(ret);</div><div class="line"> }</div><div class="line"> <span class="keywordflow">catch</span> (WiredTigerException wte) {</div><div class="line"> System.err.println(<span class="stringliteral">"Exception: "</span> + wte);</div><div class="line"> wte.printStackTrace();</div><div class="line"> System.exit(1);</div><div class="line"> }</div><div class="line"> }</div></div><!-- fragment --></div></div><!-- contents -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
<ul>
<li class="navelem"><a class="el" href="index.html">Reference Guide</a></li><li class="navelem"><a class="el" href="programming_lang_java.html">Writing WiredTiger applications in Java</a></li>
<li class="footer">Copyright (c) 2008-2019 MongoDB, Inc. All rights reserved. Contact <a href="mailto:info@wiredtiger.com">info@wiredtiger.com</a> for more information.</li>
</ul>
</div>
</body>
</html>
|