multithreading.html 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width,initial-scale=1">
  6. <title>Multithreading Optimization :: jMonkeyEngine Docs</title>
  7. <link rel="canonical" href="https://wiki.jmonkeyengine.org/docs/jme3/advanced/multithreading.html">
  8. <meta name="keywords" content="loop, game, performance, state, states, documentation">
  9. <meta name="generator" content="Antora 2.3.3">
  10. <link rel="stylesheet" href="../../../_/css/site.css">
  11. <meta property="og:image" content="https://mitm001.github.io/docs.jmonkeyengine.org/_/img/iconx128.png">
  12. <meta property="og:description" content="Multithreading Optimization">
  13. <meta property="og:title" content="jMonkeyEngine Docs">
  14. <link rel="icon" href="../../../_/img/favicon.ico" type="image/x-icon">
  15. </head>
  16. <body class="article">
  17. <header class="header">
  18. <nav class="navbar">
  19. <div class="navbar-brand">
  20. <a class="navbar-item" href="https://wiki.jmonkeyengine.org">
  21. <img alt="" src="../../../_/img/iconx128.png" width="32" type="image/x-icon" style="margin-right:16px">
  22. jMonkeyEngine Docs
  23. </a>
  24. <button class="navbar-burger" data-target="topbar-nav">
  25. <span></span>
  26. <span></span>
  27. <span></span>
  28. </button>
  29. </div>
  30. <div id="topbar-nav" class="navbar-menu">
  31. <div class="navbar-end">
  32. <div class="navbar-item theme-switch-wrapper">
  33. <label class="theme-switch" for="checkbox">
  34. <input type="checkbox" id="checkbox" />
  35. <div class="slider round"></div>
  36. </label>
  37. <p class="navlabel">Dark Mode</p>
  38. </div>
  39. <a class="navbar-item" href="https://github.com/jmonkeyengine/wiki">Github</a>
  40. </div>
  41. </div>
  42. </nav>
  43. </header>
  44. <div class="body">
  45. <div class="nav-container" data-component="docs" data-version="master">
  46. <aside class="nav">
  47. <div class="panels">
  48. <div class="nav-panel-menu is-active" data-panel="menu">
  49. <nav class="nav-menu">
  50. <h3 class="title"><a href="../../documentation.html">Docs</a></h3>
  51. <ul class="nav-list">
  52. <li class="nav-item" data-depth="0">
  53. <ul class="nav-list">
  54. <li class="nav-item" data-depth="1">
  55. <a class="nav-link" href="../../documentation.html">Getting Started</a>
  56. </li>
  57. <li class="nav-item" data-depth="1">
  58. <a class="nav-link" href="https://javadoc.jmonkeyengine.org/v3.3.2-stable">JavaDoc</a>
  59. </li>
  60. <li class="nav-item" data-depth="1">
  61. <button class="nav-item-toggle"></button>
  62. <a class="nav-link" href="../../jme3.html">jMonkeyEngine 3</a>
  63. <ul class="nav-list">
  64. <li class="nav-item" data-depth="2">
  65. <button class="nav-item-toggle"></button>
  66. <span class="nav-text">Beginner Tutorials</span>
  67. <ul class="nav-list">
  68. <li class="nav-item" data-depth="3">
  69. <a class="nav-link" href="../beginner/hello_simpleapplication.html">Hello SimpleApplication</a>
  70. </li>
  71. <li class="nav-item" data-depth="3">
  72. <a class="nav-link" href="../beginner/hello_node.html">Hello Node</a>
  73. </li>
  74. <li class="nav-item" data-depth="3">
  75. <a class="nav-link" href="../beginner/hello_asset.html">Hello Asset</a>
  76. </li>
  77. <li class="nav-item" data-depth="3">
  78. <a class="nav-link" href="../beginner/hello_main_event_loop.html">Hello Main Event Loop</a>
  79. </li>
  80. <li class="nav-item" data-depth="3">
  81. <a class="nav-link" href="../beginner/hello_input_system.html">Hello Input System</a>
  82. </li>
  83. <li class="nav-item" data-depth="3">
  84. <a class="nav-link" href="../beginner/hello_material.html">Hello Material</a>
  85. </li>
  86. <li class="nav-item" data-depth="3">
  87. <a class="nav-link" href="../beginner/hello_animation.html">Hello Animation</a>
  88. </li>
  89. <li class="nav-item" data-depth="3">
  90. <a class="nav-link" href="../beginner/hello_picking.html">Hello Picking</a>
  91. </li>
  92. <li class="nav-item" data-depth="3">
  93. <a class="nav-link" href="../beginner/hello_collision.html">Hello Collision</a>
  94. </li>
  95. <li class="nav-item" data-depth="3">
  96. <a class="nav-link" href="../beginner/hello_terrain.html">Hello Terrain</a>
  97. </li>
  98. <li class="nav-item" data-depth="3">
  99. <a class="nav-link" href="../beginner/hello_audio.html">Hello Audio</a>
  100. </li>
  101. <li class="nav-item" data-depth="3">
  102. <a class="nav-link" href="../beginner/hello_effects.html">Hello Effects</a>
  103. </li>
  104. <li class="nav-item" data-depth="3">
  105. <a class="nav-link" href="../beginner/hello_physics.html">Hello Physics</a>
  106. </li>
  107. </ul>
  108. </li>
  109. <li class="nav-item" data-depth="2">
  110. <button class="nav-item-toggle"></button>
  111. <span class="nav-text">Intermediate Tutorials</span>
  112. <ul class="nav-list">
  113. <li class="nav-item" data-depth="3">
  114. <button class="nav-item-toggle"></button>
  115. <span class="nav-text">Concepts</span>
  116. <ul class="nav-list">
  117. <li class="nav-item" data-depth="4">
  118. <a class="nav-link" href="../intermediate/best_practices.html">Best Practices</a>
  119. </li>
  120. <li class="nav-item" data-depth="4">
  121. <a class="nav-link" href="../intermediate/simpleapplication.html">Simple Application</a>
  122. </li>
  123. <li class="nav-item" data-depth="4">
  124. <a class="nav-link" href="../features.html">Features</a>
  125. </li>
  126. <li class="nav-item" data-depth="4">
  127. <a class="nav-link" href="../intermediate/optimization.html">Optimization</a>
  128. </li>
  129. <li class="nav-item" data-depth="4">
  130. <a class="nav-link" href="../faq.html">FAQ</a>
  131. </li>
  132. </ul>
  133. </li>
  134. <li class="nav-item" data-depth="3">
  135. <button class="nav-item-toggle"></button>
  136. <span class="nav-text">Math Concepts</span>
  137. <ul class="nav-list">
  138. <li class="nav-item" data-depth="4">
  139. <a class="nav-link" href="../math_for_dummies.html">Math For Dummies</a>
  140. </li>
  141. <li class="nav-item" data-depth="4">
  142. <a class="nav-link" href="../intermediate/math.html">Math</a>
  143. </li>
  144. <li class="nav-item" data-depth="4">
  145. <a class="nav-link" href="../math.html">More Math</a>
  146. </li>
  147. <li class="nav-item" data-depth="4">
  148. <a class="nav-link" href="../rotate.html">Rotate</a>
  149. </li>
  150. <li class="nav-item" data-depth="4">
  151. <a class="nav-link" href="../math_video_tutorials.html">Math Video Tutorials</a>
  152. </li>
  153. </ul>
  154. </li>
  155. <li class="nav-item" data-depth="3">
  156. <button class="nav-item-toggle"></button>
  157. <span class="nav-text">3D Graphics Concepts</span>
  158. <ul class="nav-list">
  159. <li class="nav-item" data-depth="4">
  160. <a class="nav-link" href="../intermediate/multi-media_asset_pipeline.html">Multi-Media Asset Pipeline</a>
  161. </li>
  162. <li class="nav-item" data-depth="4">
  163. <a class="nav-link" href="../scenegraph_for_dummies.html">Scenegraph for Dummies</a>
  164. </li>
  165. <li class="nav-item" data-depth="4">
  166. <a class="nav-link" href="../beginner/hellovector.html">Hello Vector</a>
  167. </li>
  168. <li class="nav-item" data-depth="4">
  169. <a class="nav-link" href="../terminology.html">Terminology</a>
  170. </li>
  171. <li class="nav-item" data-depth="4">
  172. <a class="nav-link" href="../intermediate/how_to_use_materials.html">How to Use Materials</a>
  173. </li>
  174. <li class="nav-item" data-depth="4">
  175. <a class="nav-link" href="../intermediate/transparency_sorting.html">Transparency and Sorting</a>
  176. </li>
  177. <li class="nav-item" data-depth="4">
  178. <a class="nav-link" href="../external/blender.html">Importing from Blender</a>
  179. </li>
  180. <li class="nav-item" data-depth="4">
  181. <a class="nav-link" href="../external/3dsmax.html">Importing from 3DS Max</a>
  182. </li>
  183. </ul>
  184. </li>
  185. </ul>
  186. </li>
  187. </ul>
  188. </li>
  189. <li class="nav-item" data-depth="1">
  190. <a class="nav-link" href="../../logo.html">Logo Usage</a>
  191. </li>
  192. <li class="nav-item" data-depth="1">
  193. <a class="nav-link" href="../../bsd_license.html">License</a>
  194. </li>
  195. <li class="nav-item" data-depth="1">
  196. <a class="nav-link" href="../../github_tips.html">Github Tips</a>
  197. </li>
  198. </ul>
  199. </li>
  200. <li class="nav-item" data-depth="0">
  201. <button class="nav-item-toggle"></button>
  202. <span class="nav-text">SDK</span>
  203. <ul class="nav-list">
  204. <li class="nav-item" data-depth="1">
  205. <a class="nav-link" href="../../sdk.html">jMonkeyEngine SDK</a>
  206. </li>
  207. </ul>
  208. </li>
  209. </ul>
  210. </nav>
  211. </div>
  212. <div class="nav-panel-explore" data-panel="explore">
  213. <div class="context">
  214. <span class="title">Docs</span>
  215. <span class="version">master</span>
  216. </div>
  217. <ul class="components">
  218. <li class="component is-current">
  219. <span class="title">Docs</span>
  220. <ul class="versions">
  221. <li class="version is-current is-latest">
  222. <a href="../../documentation.html">master</a>
  223. </li>
  224. </ul>
  225. </li>
  226. <li class="component">
  227. <span class="title">Wiki UI</span>
  228. <ul class="versions">
  229. <li class="version is-latest">
  230. <a href="../../../wiki-ui/index.html">master</a>
  231. </li>
  232. </ul>
  233. </li>
  234. </ul>
  235. </div>
  236. </div>
  237. </aside>
  238. </div>
  239. <main class="article">
  240. <div class="toolbar" role="navigation">
  241. <button class="nav-toggle"></button>
  242. <nav class="breadcrumbs" aria-label="breadcrumbs">
  243. <ul>
  244. <li><a href="../../documentation.html">Docs</a></li>
  245. <li><a href="multithreading.html">Multithreading Optimization</a></li>
  246. </ul>
  247. </nav>
  248. <div class="edit-this-page"><a href="https://github.com/jMonkeyEngine/wiki/edit/master/docs/modules/ROOT/pages/jme3/advanced/multithreading.adoc">Edit this Page</a></div>
  249. </div>
  250. <div class="content">
  251. <article class="doc">
  252. <h1 class="page">Multithreading Optimization</h1>
  253. <div class="sect1">
  254. <h2 id="the-jme3-threading-model"><a class="anchor" href="#the-jme3-threading-model"></a>The jME3 Threading Model</h2>
  255. <div class="sectionbody">
  256. <div class="paragraph">
  257. <p>jME3 is similar to Swing in that, for speed and efficiency, all changes to the scene graph must be made in a single update thread. If you make changes only in Control.update(), AppState.update(), or SimpleApplication.simpleUpdate(), this will happen automatically. However, if you pass work to another thread, you may need to pass results back to the main jME3 thread so that scene graph changes can take place there.</p>
  258. </div>
  259. <div class="listingblock">
  260. <div class="content">
  261. <pre class="highlightjs highlight"><code class="language-java hljs" data-lang="java">public void rotateGeometry(final Geometry geo, final Quaternion rot) {
  262. mainApp.enqueue(new Callable&lt;Spatial&gt;() {
  263. public Spatial call() throws Exception {
  264. return geo.rotate(rot);
  265. }
  266. });
  267. }</code></pre>
  268. </div>
  269. </div>
  270. <div class="admonitionblock note">
  271. <table>
  272. <tr>
  273. <td class="icon">
  274. <i class="fa icon-note" title="Note"></i>
  275. </td>
  276. <td class="content">
  277. <div class="paragraph">
  278. <p>This example does not fetch the returned value by calling <code>get()</code> on the Future object returned from <code>enqueue()</code>. This means that the example method <code>rotateGeometry()</code> will return immediately and will not wait for the rotation to be processed before continuing.</p>
  279. </div>
  280. <div class="paragraph">
  281. <p>If the processing thread needs to wait or needs the return value then <code>get()</code> or the other methods in the returned Future object such as <code>isDone()</code> can be used.</p>
  282. </div>
  283. </td>
  284. </tr>
  285. </table>
  286. </div>
  287. <div class="paragraph">
  288. <p>First, make sure you know what <a href="application_states.html" class="page">Application States</a> and <a href="custom_controls.html" class="page">Custom Controls</a> are.</p>
  289. </div>
  290. <div class="paragraph">
  291. <p>More complex games may feature complex mathematical operations or artificial intelligence calculations (such as path finding for several NPCs). If you make many time-intensive calls on the same thread (in the update loop), they will block one another, and thus slow down the game to a degree that makes it unplayable. If your game requires long running tasks, you should run them concurrently on separate threads, which speeds up the application considerably.</p>
  292. </div>
  293. <div class="paragraph">
  294. <p>Often multithreading means having separate detached logical loops going on in parallel, which communicate about their state. (For example, one thread for AI, one Sound, one Graphics). However we recommend to use a global update loop for game logic, and do multithreading within that loop when it is appropriate. This approach scales way better to multiple cores and does not break up your code logic.</p>
  295. </div>
  296. <div class="paragraph">
  297. <p>Effectively, each for-loop in the main update loop might be a chance for multithreading, if you can break it up into self-contained tasks.</p>
  298. </div>
  299. <div class="sect2">
  300. <h3 id="java-multithreading"><a class="anchor" href="#java-multithreading"></a>Java Multithreading</h3>
  301. <div class="paragraph">
  302. <p>The java.util.concurrent package provides a good foundation for multithreading and dividing work into tasks that can be executed concurrently (hence the name). The three basic components are the Executor (supervises threads), Callable Objects (the tasks), and Future Objects (the result). You can <a href="http://download.oracle.com/javase/tutorial/essential/concurrency/">read about the concurrent package more here</a>, I will give just a short introduction.</p>
  303. </div>
  304. <div class="ulist">
  305. <ul>
  306. <li>
  307. <p>A Callable is one of the classes that gets executed on a thread in the Executor. The object represents one of several concurrent tasks (e.g, one NPC&#8217;s path finding task). Each Callable is started from the updateloop by calling a method named <code>call()</code>.</p>
  308. </li>
  309. <li>
  310. <p>The Executor is one central object that manages all your Callables. Every time you schedule a Callable in the Executor, the Executor returns a Future object for it.</p>
  311. </li>
  312. <li>
  313. <p>A Future is an object that you use to check the status of an individual Callable task. The Future also gives you the return value in case one is returned.</p>
  314. </li>
  315. </ul>
  316. </div>
  317. </div>
  318. <div class="sect2">
  319. <h3 id="multithreading-in-jme3"><a class="anchor" href="#multithreading-in-jme3"></a>Multithreading in jME3</h3>
  320. <div class="paragraph">
  321. <p>So how do we implement multithreading in jME3?</p>
  322. </div>
  323. <div class="paragraph">
  324. <p>Let&#8217;s take the example of a Control that controls an NPC Spatial. The NPC Control has to compute a lengthy pathfinding operation for each NPC. If we would execute the operations directly in the simpleUpdate() loop, it would block the game each time a NPC wants to move from A to B. Even if we move this behaviour into the update() method of a dedicated NPC Control, we would still get annoying freeze frames, because it still runs on the same update loop thread.</p>
  325. </div>
  326. <div class="paragraph">
  327. <p>To avoid slowdown, we decide to keep the pathfinding operations in the NPC Control, <em>but execute it on another thread</em>.</p>
  328. </div>
  329. </div>
  330. </div>
  331. </div>
  332. <div class="sect1">
  333. <h2 id="executor"><a class="anchor" href="#executor"></a>Executor</h2>
  334. <div class="sectionbody">
  335. <div class="paragraph">
  336. <p>You create the executor object in a global AppState (or the initSimpleApp() method), in any case in a high-level place where multiple controls can access it.</p>
  337. </div>
  338. <div class="listingblock">
  339. <div class="content">
  340. <pre class="highlightjs highlight"><code class="language-java hljs" data-lang="java">/* This constructor creates a new executor with a core pool size of 4. */
  341. ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(4);</code></pre>
  342. </div>
  343. </div>
  344. <div class="paragraph">
  345. <p>Pool size means the executor will keep four threads alive at any time. Having more threads in the pool means that more tasks can run concurrently. But a bigger pool only results in a speed gain if the PC can handle it! Allocating a pool that is uselessly large just wastes memory, so you need to find a good compromise: About the same to double the size of the number of cores in the computer makes sense.</p>
  346. </div>
  347. <div class="admonitionblock warning">
  348. <table>
  349. <tr>
  350. <td class="icon">
  351. <i class="fa icon-warning" title="Warning"></i>
  352. </td>
  353. <td class="content">
  354. <div class="paragraph">
  355. <p>Executor needs to be shut down when the application ends, in order to make the process die properly
  356. In your simple application you can override the destroy method and shutdown the executor:</p>
  357. </div>
  358. </td>
  359. </tr>
  360. </table>
  361. </div>
  362. <div class="listingblock">
  363. <div class="content">
  364. <pre class="highlightjs highlight"><code class="language-java hljs" data-lang="java"> @Override
  365. public void destroy() {
  366. super.destroy();
  367. executor.shutdown();
  368. }</code></pre>
  369. </div>
  370. </div>
  371. </div>
  372. </div>
  373. <div class="sect1">
  374. <h2 id="control-class-fields"><a class="anchor" href="#control-class-fields"></a>Control Class Fields</h2>
  375. <div class="sectionbody">
  376. <div class="paragraph">
  377. <p>In the NPC Control, we create the individual objects that the thread manipulates. In our example case (the pathfinding control), the task is about locations and path arrays, so we need the following variables:</p>
  378. </div>
  379. <div class="listingblock">
  380. <div class="content">
  381. <pre class="highlightjs highlight"><code class="language-Java hljs" data-lang="Java">//The vector to store the desired location in:
  382. Vector3f desiredLocation = new Vector3f();
  383. //The MyWayList object that contains the result waylist:
  384. MyWayList wayList = null;
  385. //The future that is used to check the execution status:
  386. Future future = null;</code></pre>
  387. </div>
  388. </div>
  389. <div class="paragraph">
  390. <p>Here we also created the Future variable to track the state of this task.</p>
  391. </div>
  392. </div>
  393. </div>
  394. <div class="sect1">
  395. <h2 id="control-update-method"><a class="anchor" href="#control-update-method"></a>Control Update() Method</h2>
  396. <div class="sectionbody">
  397. <div class="paragraph">
  398. <p>Next let&#8217;s look at the update() call of the Control where the time-intensive task starts. In our example, the task is the <code>findWay</code> Callable (which contains the pathfinding process). So instead of spelling out the pathfinding process in the Control&#8217;s update() loop, we start the process via <code>future = executor.submit(findWay);</code>.</p>
  399. </div>
  400. <div class="listingblock">
  401. <div class="content">
  402. <pre class="highlightjs highlight"><code class="language-java hljs" data-lang="java">public void update(float tpf) {
  403. try{
  404. //If we have no waylist and not started a callable yet, do so!
  405. if(wayList == null &amp;&amp; future == null){
  406. //set the desired location vector, after that we should not modify it anymore
  407. //because it's being accessed on the other thread!
  408. desiredLocation.set(getGoodNextLocation());
  409. //start the callable on the executor
  410. future = executor.submit(findWay); // Thread starts!
  411. }
  412. //If we have started a callable already, we check the status
  413. else if(future != null){
  414. //Get the waylist when its done
  415. if(future.isDone()){
  416. wayList = future.get();
  417. future = null;
  418. }
  419. else if(future.isCancelled()){
  420. //Set future to null. Maybe we succeed next time...
  421. future = null;
  422. }
  423. }
  424. }
  425. catch(Exception e){
  426. Exceptions.printStackTrace(e);
  427. }
  428. if(wayList != null){
  429. //.... Success! Let's process the wayList and move the NPC...
  430. }
  431. }</code></pre>
  432. </div>
  433. </div>
  434. <div class="paragraph">
  435. <p>Note how this logic makes its decision based on the Future object.</p>
  436. </div>
  437. <div class="paragraph">
  438. <p>Remember not to mess with the class fields after starting the thread, because they are being accessed and modified on the new thread. In more obvious terms: You cannot change the “desired location of the NPC while the path finder is calculating a different path. You have to cancel the current Future first.</p>
  439. </div>
  440. </div>
  441. </div>
  442. <div class="sect1">
  443. <h2 id="the-callable"><a class="anchor" href="#the-callable"></a>The Callable</h2>
  444. <div class="sectionbody">
  445. <div class="paragraph">
  446. <p>The next code sample shows the Callable that is dedicated to performing the long-running task (here, wayfinding). This is the task that used to block the rest of the application, and is now executed on a thread of its own. You implement the task in the Callable always in an inner method named <code>call()</code>.</p>
  447. </div>
  448. <div class="paragraph">
  449. <p>The task code in the Callable should be self-contained! It should not write or read any data of objects that are managed by the scene graph or OpenGL thread directly. Even reading locations of Spatials can be problematic! So ideally all data that is needed for the wayfinding process should be available to the new thread when it starts already, possibly in a cloned version so no concurrent access to the data happens.</p>
  450. </div>
  451. <div class="paragraph">
  452. <p>In reality, you might need access to the game state. If you must read or write a current state from the scene graph, you must have a clone of the data in your thread. There are only two ways:</p>
  453. </div>
  454. <div class="ulist">
  455. <ul>
  456. <li>
  457. <p>Use the execution queue <code>application.enqueue()</code> to create a sub-thread that clones the info. Only disadvantage is, it may be slower.<br>
  458. The example below gets the <code>Vector3f location</code> from the scene object <code>mySpatial</code> using this way.</p>
  459. </li>
  460. <li>
  461. <p>Create a separate World class that allows safe access to its data via synchronized methods to access the scene graph. Alternatively it can also internally use <code>application.enqueue()</code>.<br>
  462. The following example gets the object <code>Data data = myWorld.getData();</code> using this way.</p>
  463. </li>
  464. </ul>
  465. </div>
  466. <div class="paragraph">
  467. <p>These two ways are thread-safe, they don&#8217;t mess up the game logic, and keep the Callable code readable.</p>
  468. </div>
  469. <div class="listingblock">
  470. <div class="content">
  471. <pre class="highlightjs highlight"><code class="language-java hljs" data-lang="java">// A self-contained time-intensive task:
  472. private Callable&lt;MyWayList&gt; findWay = new Callable&lt;MyWayList&gt;(){
  473. public MyWayList call() throws Exception {
  474. //Read or write data from the scene graph -- via the execution queue:
  475. Vector3f location = application.enqueue(new Callable&lt;Vector3f&gt;() {
  476.     public Vector3f call() throws Exception {
  477.         //we clone the location so we can use the variable safely on our thread
  478.         return mySpatial.getLocalTranslation().clone();
  479.     }
  480. }).get();
  481. // This world class allows safe access via synchronized methods
  482. Data data = myWorld.getData();
  483. //... Now process data and find the way ...
  484. return wayList;
  485. }
  486. };</code></pre>
  487. </div>
  488. </div>
  489. <div class="sect2">
  490. <h3 id="useful-links"><a class="anchor" href="#useful-links"></a>Useful Links</h3>
  491. <div class="paragraph">
  492. <p>High level description which describes how to manage the game state and the rendering in different threads:<br>
  493. <a href="http://jahej.com/alt/2011_07_03_threading-and-your-game-loop.html">Threading and your game loop</a>.<br>
  494. A C++ example can be found at:<br>
  495. <a href="http://gamasutra.com/blogs/AndreaMagnorsky/20130527/193087/Multithreading_rendering_in_a_game_engine_with_CDouble_buffer_implementation.php">Multithreading-rendering in a game engine with CDouble buffer implementation</a>.</p>
  496. </div>
  497. </div>
  498. <div class="sect2">
  499. <h3 id="conclusion"><a class="anchor" href="#conclusion"></a>Conclusion</h3>
  500. <div class="paragraph">
  501. <p>The cool thing about this approach is that every entity creates one self-contained Callable for the Executor, and they are all executed in parallel. In theory, you can have one thread per entity without changing anything else but the settings of the executor.</p>
  502. </div>
  503. </div>
  504. </div>
  505. </div>
  506. </article>
  507. <aside class="toc sidebar" data-title="Contents" data-levels="2">
  508. <div class="toc-menu"></div>
  509. </aside>
  510. </div>
  511. </main>
  512. </div>
  513. <footer class="footer">
  514. <p>Copyright 2020 jMonkeyEngine Wiki Contributors. Licensed BSD-3.</p>
  515. </footer>
  516. <script src="../../../_/js/site.js"></script>
  517. <script async src="../../../_/js/vendor/highlight.js"></script>
  518. </body>
  519. </html>