<p>In my SWT Java app I often want to return information from inside a Display.syncExec() call.  The best way I've found so far to do this is:</p>

<pre><code>final ArrayList&lt;Integer&gt; result = new ArrayList&lt;Integer&gt;();
GUI.display().syncExec(new Runnable(){ public void run() {
   MessageBox mb = /* ... */;
    /* set up messagebox */
   result.add(mb.open());
}});
if (SWT.OK == result.get(0)) { /* ... */ }
</code></pre>

<p>I think this is allowed because ArrayList is thread-safe, but is there a better container I should be using, or an easier way altogether?</p>

[{'answer_id': 76393, 'author': 'Dónal', 'author_id': 2648, 'author_profile': 'https://Stackoverflow.com/users/2648', 'pm_score': 1, 'selected': False, 'text': '<p>ArrayList is <em>not</em> thread-safe. From the relevant <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/ArrayList.html" rel="nofollow noreferrer">Javadoc</a>:</p>\n\n<blockquote>\n  <p>Note that this implementation is not\n  synchronized. If multiple threads\n  access an ArrayList instance\n  concurrently, and at least one of the\n  threads modifies the list\n  structurally, it must be synchronized\n  externally.</p>\n</blockquote>\n\n<p>If you need a thread-safe implementation of List, there are (at least) two provided in the JDK: CopyOnWriteArrayList and Vector.</p>\n'}, {'answer_id': 76431, 'author': 'Heath Borders', 'author_id': 9636, 'author_profile': 'https://Stackoverflow.com/users/9636', 'pm_score': 4, 'selected': True, 'text': '<p><a href="http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html" rel="nofollow noreferrer"><code>ArrayList</code> is not thread-safe</a>. You can obtain a thread-safe <code>List</code> with <a href="http://docs.oracle.com/javase/7/docs/api/java/util/Collections.html#synchronizedList%28java.util.List%29" rel="nofollow noreferrer"><code>Collections.synchronizedList</code></a>. However, it is much simpler to use an <code>AtomicInteger</code> in your case or <code>AtomicReference</code> in a more general case.</p>\n\n<pre><code>final AtomicInteger resultAtomicInteger = new AtomicInteger();\nDisplay.getCurrent().syncExec(new Runnable() { \n    public void run() {\n        MessageBox mb = /* ... */;\n            /* set up messagebox */\n        resultAtomicInteger.set(mb.open());\n}});\nif (SWT.OK == resultAtomicInteger.get()) { /* ... */ }\n</code></pre>\n'}, {'answer_id': 76443, 'author': 'James A. N. Stauffer', 'author_id': 6770, 'author_profile': 'https://Stackoverflow.com/users/6770', 'pm_score': 1, 'selected': False, 'text': "<p>You could use an Integer[1] array to make it more concise but I don't think it can directly update a non-final variable from inside an anonymous inner class.</p>\n\n<pre><code>final Integer[] result = new Integer[1];\n</code></pre>\n\n<p>I thought that you had to declare results as final (but that change wouldn't affect your code).  Since the current Thread blocks until the inner Thread is finished I don't think you need to worry about synchronization (but you might need to make the variable violate so that you see the result).</p>\n"}, {'answer_id': 101304, 'author': 'Bahadır Yağan', 'author_id': 3812, 'author_profile': 'https://Stackoverflow.com/users/3812', 'pm_score': 1, 'selected': False, 'text': '<p>If this happens often, you better use subscribe/notify model between your process and view. Your view subscribes to the event which should trigger that message box and gets notified when conditions met.</p>\n'}, {'answer_id': 8941478, 'author': 'crevos', 'author_id': 1160611, 'author_profile': 'https://Stackoverflow.com/users/1160611', 'pm_score': 2, 'selected': False, 'text': "<p>I just tackled this problem and my first try was similar - array or list of desired type items. But after a while I made up something like this:</p>\n\n<pre><code>abstract class MyRunnable&lt;T&gt; implements Runnable{\n    T result;\n}\nMyRunnable&lt;Integer&gt; runBlock = new MyRunnable&lt;Integer&gt;(){\n   MessageBox mb = /* ... */;\n    /* set up messagebox */\n   result = mb.open();\n}\nGUI.display().syncExec(runBlock);\nrunBlock.result; //holds a result Integer\n</code></pre>\n\n<p>It's much tidier and removes redundant variables.</p>\n\n<p>BTW. My really first try was to use UIThreadRunnable, but I didn't want SWTBot dependency, so I dropped this solution. After I made my own solution I found out, they use similar work around in there.</p>\n"}]
['https://Stackoverflow.com/questions/76300', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/13581/']