Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I would really recommend that you go through a tutorial like <a href="http://java.sun.com/docs/books/tutorial/essential/concurrency/" rel="noreferrer">Sun's Java Concurrency</a> before you commence in the magical world of multithreading.</p> <p>There are also a number of good books out (google for "Concurrent Programming in Java", "Java Concurrency in Practice".</p> <p><strong>To get to your answer:</strong></p> <p>In your code that must wait for the <code>dbThread</code>, you must have something like this:</p> <pre><code>//do some work synchronized(objectYouNeedToLockOn){ while (!dbThread.isReady()){ objectYouNeedToLockOn.wait(); } } //continue with work after dbThread is ready </code></pre> <p>In your <code>dbThread</code>'s method, you would need to do something like this:</p> <pre><code>//do db work synchronized(objectYouNeedToLockOn){ //set ready flag to true (so isReady returns true) ready = true; objectYouNeedToLockOn.notifyAll(); } //end thread run method here </code></pre> <p>The <code>objectYouNeedToLockOn</code> I'm using in these examples is preferably the object that you need to manipulate concurrently from each thread, or you could create a separate <code>Object</code> for that purpose (I would not recommend making the methods themselves synchronized):</p> <pre><code>private final Object lock = new Object(); //now use lock in your synchronized blocks </code></pre> <p><strong>To further your understanding:</strong><br> There are other (sometimes better) ways to do the above, e.g. with <code>CountdownLatches</code>, etc. Since Java 5 there are a lot of nifty concurrency classes in the <code>java.util.concurrent</code> package and sub-packages. You really need to find material online to get to know concurrency, or get a good book. </p>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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