Note that there are some explanatory texts on larger screens.

plurals
  1. POJava: Throw an exception to another Thread
    primarykey
    data
    text
    <p>I have a java code like this:</p> <pre><code>private class Uploader implements Runnable { // ... public void start() { t.start(); } public void run() { try { while(i=in.read()) { output.write(i); // THIS IS A BLOCKING CALL !! } } catch(ProtocolException e) { ... } catch(IOException e1) { ... } } private void restore() { ... } private class Checker implements Runnable { // ... @Override public void run() { // I WANT (IN A PARTICULAR MOMENT) TO THROW AN // EXCEPTION INTO THE Uploader RUN METHOD FROM HERE, // IS IT POSSIBLE? } } } </code></pre> <p>The problem is that i have a blocking write() in the Run() method, so I have added a new thread that checks whether or not the connection is transmitting: if it's not trasmitting I want to stop the blocking write() using the exception mechanism (throwing an exception to the other thread's run() method from the checker thread). Is it possible?</p> <p><strong>EDIT [SOLVED]:</strong></p> <p><strong>The only way is to brutally close the output stream and to work on the amount of written bits to check whether the connection is transmitting:</strong></p> <pre><code>private class Uploader implements Runnable { private OutputStream output; private int readedBits; public void run() { try { while(i=in.read()) { output.write(i); readedBits++; } } catch(IOException e1) { // ENTERS HERE IF RESTORE() IS CALLED } } private void restore() { try { output.close(); } catch (IOException e) {} // Restore connection .... } private int getReadedBits() { return this.readedBits; } private class Checker implements Runnable { // ... @Override public void run() { while(true) { try { Thread.sleep(timeout); } catch (InterruptedException e1) {} if(lastReaded &gt;= getReadedBits()) restore(); else lastReaded = getReadedBits(); } } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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