Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can try one of the following approaches:</p> <ol> <li><p>It looks like you're trying to keep your writer thread running all the time; what you can do is spawn the thread only when you need it. Take a look at the example in the Android documentation for <a href="http://developer.android.com/guide/appendix/faq/commontasks.html#threading" rel="nofollow">handling expensive operation in the UI thread</a>.</p> <p>Here is the example from that page:</p> <pre><code>public class MyActivity extends Activity { [ . . . ] // Need handler for callbacks to the UI thread final Handler mHandler = new Handler(); // Create runnable for posting final Runnable mUpdateResults = new Runnable() { public void run() { updateResultsInUi(); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); [ . . . ] } protected void startLongRunningOperation() { // Fire off a thread to do some work that we shouldn't do directly in the UI thread Thread t = new Thread() { public void run() { mResults = doSomethingExpensive(); mHandler.post(mUpdateResults); } }; t.start(); } private void updateResultsInUi() { // Back in the UI thread -- update our UI elements based on the data in mResults [ . . . ] } } </code></pre> <p>Since it doesn't look like you're doing anything in the UI thread once you finish writing you don't really need to bother with a <code>Handler</code>. But you might want to use it to display a <code>Toast</code> once the file has been written to.</p></li> <li><p>On the other hand, if you still want to have a thread running, you might have it <code>sleep()</code> and periodically wake up and check the status of <code>writeNow</code>. </p> <pre><code>Thread thread1 = new Thread() { public void run() { while(true) { if(writeNow == true) { try { fos.write(s.getBytes()); } catch (IOException e) { e.printStackTrace(); } writeNow = false; } try { Thread.sleep(100); //sleep for 100 ms } catch (InterruptedException e) { Log.d('', e.getMessage()); } } } }; </code></pre> <p>Note that this will quickly get complicated and you might lose the bytes you want to write if your thread is sleeping when new data comes in and when it wakes up, even newer data has been received and has overwritten the previous bytes. You'd need some sort of a queue to manage that. </p></li> <li><p>I'm not sure what you were doing with the <code>wait()</code> but that should've also worked and is in fact, the approach for problems involving a consumer and producer. The idea is to have your thread synchronize and <code>wait()</code> on a shared object (like perhaps your queue of bytes); a second thread will call <code>notify()</code> on the shared object when there is data available to write and the writer thread will be woken up. The writer thread should then write and reloop. Take a look at <a href="http://www.javamex.com/tutorials/synchronization_wait_notify.shtml" rel="nofollow">this tutorial</a>.</p> <p>As for the interruption of your thread, your thread may be interrupted for a number of reasons which is why it is good practice (especially when using <code>wait()</code>) to ensure that the condition you checked <em>before</em> you called <code>wait()</code> is still valid because you could've been woken because of either a call to <code>notify()/notifyAll()</code> or because of an interruption.</p></li> </ol>
 

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