Note that there are some explanatory texts on larger screens.

plurals
  1. POTwo threads deadlocking but can't see why, lock released with notifyAll()
    primarykey
    data
    text
    <p>using JConsole it seems i get a deadlock situation when 2 threads try to modify this object.</p> <pre><code>package com.steven.concurrent.assignment2.memoryallocator; /* * This seems to deadlock... cant see why though. */ public class MemAllocMonitor implements IMemoryAllocator { private final int MAX_FREE = 50; private int freePages = MAX_FREE; //I think this would work, without even the need for sync blocks..... // But only in the situaion where i would not have to check the bounds of the updates. If it was just modification, this would be // fine.... //private volatile int freePages = 50; public MemAllocMonitor(int pages){ assert(pages &lt; MAX_FREE); this.freePages = pages; } public MemAllocMonitor(){ } @Override public synchronized void request(int number) { if(number &lt; 0) throw new IllegalArgumentException(); while(freePages - number &lt; 0) { System.out.println("No space....waiting..."); try { this.wait(); } catch (Exception e) {} } freePages -= number; System.out.println("Requested : " + number + " remaining " + freePages); this.notifyAll(); } @Override public synchronized void release(int number) { if(number &lt; 0) throw new IllegalArgumentException(); while(freePages + number &gt; MAX_FREE) { System.out.println("page table full....would be " + (number + freePages) ); try { this.wait(); } catch (Exception e) {} } freePages += number; System.out.println("Released : " + number + " remaining " + freePages); this.notifyAll(); } @Override public int getFreePages() { return freePages; } } </code></pre> <p>This object is accessed via a simple wrapper that implements runnable, and calls either method as shown below. </p> <pre><code>package com.steven.concurrent.assignment2.memoryallocator; import concurrent.RandomGenerator; import concurrent.Time; public class MemAllocRequester implements Runnable, MemoryAllocatorAction{ private IMemoryAllocator memoryAllocator; private volatile boolean shutdown = false;; public MemAllocRequester(IMemoryAllocator memAlloc){ this.memoryAllocator = memAlloc; } @Override public void run() { while(!shutdown){ Time.delay(500); memoryAllocator.request(RandomGenerator.integer(0, 30)); } } public void ShutDown(){ this.shutdown = true; } } </code></pre> <p>and</p> <pre><code>package com.steven.concurrent.assignment2.memoryallocator; import concurrent.RandomGenerator; import concurrent.Time; public class MemAllocReleaser implements Runnable, MemoryAllocatorAction{ private IMemoryAllocator memoryAllocator; private volatile boolean shutdown = false;; public MemAllocReleaser(IMemoryAllocator memAlloc){ this.memoryAllocator = memAlloc; } @Override public void run() { while(!shutdown){ Time.delay(500); memoryAllocator.release(RandomGenerator.integer(0, 30)); } } public void ShutDown(){ this.shutdown = true; } </code></pre> <p>}</p> <p>It is started off as such...</p> <pre><code>package com.steven.concurrent.assignment2.memoryallocator; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class MemAllocMain { public static void main(String[] args){ ExecutorService executor = Executors.newFixedThreadPool(10); //IMemoryAllocator memoryAllocator = new MemAllocSemaphore(); IMemoryAllocator memoryAllocator = new MemAllocMonitor(); System.out.println("Starting app with " + memoryAllocator.getFreePages() + " pages..."); Thread t1 = new Thread(new MemAllocRequester(memoryAllocator)); Thread t2 = new Thread(new MemAllocReleaser(memoryAllocator)); t1.setName("MEMORY REQUESTER £££££££££££££££££££"); t2.setName("MEMORY RELEASER £££££££££££££££££££"); executor.submit(t1); executor.submit(t2); } } </code></pre> <p>I have implemented a solution using the semaphore class, but for some reason this is causing trouble using the default java monitor solution. It runs for about 30 seconds, then both threads go into their waiting state, even though the lock should be enforced. </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. 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