Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Firstly, it's very hard to read that code. In your MainThread you nested <code>if</code> statement few times and without deep code analysing, its hard to tell what is wrong.</p> <p>But few things get my attention. </p> <p>In <code>Downloads</code> class you grab references for read and write lock from two different <code>ReentrantReadWriteLock</code> objects!</p> <pre><code>private Lock readLock=new ReentrantReadWriteLock().readLock(); private Lock writeLock=new ReentrantReadWriteLock().writeLock(); </code></pre> <p>It can't work! You should create one <code>ReentrantReadWriteLock</code> instance for <code>Download</code> object and obtain references for <code>readLock</code> and <code>writeLock</code> lock from it. Read and Write lock are connected, so when you lock on <code>writeLock</code> you prevent readers from executing protected part of code. Pleas read more about <a href="http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/locks/ReadWriteLock.html" rel="nofollow">ReadWriteLock</a> and you can look at example of using <a href="https://github.com/bennidi/mbassador/blob/master/src/main/java/net/engio/mbassy/common/AbstractConcurrentSet.java" rel="nofollow">ReentrantReadWriteLock in mbassador EventBus</a>.</p> <p>What's more, is that you don't need locks in <code>Downloads</code> and <code>DownloadsNode</code> either! Because you create instances of those class per thread and you don't share it between threads. So you can simply remove all code responsible for locking from those classes. The only object witch is shared among the thread is instance of <code>Cache</code> class.</p> <p>Furthermore you don't need <code>synchronized(this)</code> in <code>MainThread</code>! Each new thread created from this class will be synchronized on different object (<code>this</code>). So it's doesn't make sense to add all of those <code>synchronized</code> blocks. The worst thing is that I see only one thread in your attached code.</p> <p>I think you should remove all code responsible for locking and synchronizing, and then analyse witch objects are shared between threads and should be protected from concurrent access. For me only object witch need to be locked is <code>Cache</code> instance.</p> <p>Maybe if you post working SSCCE some one can help you with refactoring this code. <code>VGR</code> point few good tips in his comment.</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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