Results 1 to 2 of 2
- 03-10-2012, 12:30 PM #1
Member
- Join Date
- Mar 2012
- Posts
- 1
- Rep Power
- 0
How to Simulate Deadlock Prevention in Java?
can somebody help me with my project? i've been searching for reference source codes about preventing deadlock in java. the requirement is we have to make a simulation of deadlock prevention, show the GUI in which the user can see the proof that a deadlock was really prevented. semaphores, locks, threads. these classes came out in the research i conducted but i can't seem to make a working source code out of them. please please help me. i really need to make this project, it will be due two days from now

my instructor said, what we should do is limit the number of processes and resources to be used in the simulation. and come up with an algorithm to allocate resources for each process. the sample code that i showed her was rejected, she said, it is easier to simulate deadlock prevention without basing it in a real world situation, that processes and resources need not to be defined.
Java Code:import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; public class Deadlock { static class Cashier { private double balance; private final String name; public final Lock lock = new ReentrantLock(); public Cashier(double balanceIni, String name) { this.balance = balanceIni; this.name = name; } public void debit(double value) { balance += value; } public void credit(double value) { balance -= value; } public double getBalance() { return balance; } public String getName() { return name; } } static class OperateCashier { public boolean transfer(Cashier cashierFrom, Cashier cashierTo, double value, String h){ Boolean lock1 = false; Boolean lock2 = false; System.out.println("Thread " + h + ": transfer cash from " + cashierFrom.getName() + " to " + cashierTo.getName()); try { System.out.println("Thread " + h + ": get lock " + cashierFrom.getName()); lock1 = cashierFrom.lock.tryLock(); System.out.println("Thread " + h + ": get lock " + cashierTo.getName()); lock2 = cashierTo.lock.tryLock(); } finally { if (!(lock1 && lock2)) { if (lock1) { cashierFrom.lock.unlock(); } if (lock2) { cashierTo.lock.unlock(); } } } if (lock1 && lock2) { try { if (cashierFrom.getBalance() >= value) { cashierFrom.debit(value); cashierTo.credit(value); System.out.println("Thread " + h + ": transfer finished..."); } } finally { cashierFrom.lock.unlock(); cashierTo.lock.unlock(); } } else { System.out.println("Thread " + h + ":It was not able to get the lock from both objects"); } return (lock1 && lock2); } } public static void main(String[] args) { final Cashier cashier1 = new Cashier(60000, "CJ1"); final Cashier cashier2 = new Cashier(80000, "CJ2"); final OperateCashier opc = new OperateCashier(); new Thread(new Runnable() { String nameThread = "H1"; boolean go = false; long time = 10000; public void run() { while (!go) { go=opc.transfer(cashier1,cashier2,20000,nameThread); if (!go) { try { System.out.println("Thread "+ nameThread + ": Wating " + time); Thread.sleep(time); } catch (InterruptedException e) {} } } } }).start(); new Thread(new Runnable() { String nameThread = "H2"; boolean go = false; long time = 10000; public void run() { while (!go) { go=opc.transfer(cashier2, cashier1, 10000, nameThread); if (!go) { try { System.out.println("Thread "+nameThread + ": Wating " + time); Thread.sleep(time); } catch (InterruptedException e) {} } } } }).start(); } }
- 03-10-2012, 02:32 PM #2
Similar Threads
-
Compiler deadlock
By Arnold in forum New To JavaReplies: 2Last Post: 06-27-2010, 12:57 PM -
Simulate a Network using Java
By fung1223 in forum New To JavaReplies: 0Last Post: 01-13-2010, 10:46 AM -
Thread Deadlock
By ajeeb in forum New To JavaReplies: 2Last Post: 01-16-2009, 02:49 AM -
simulate form type=button press with java application
By redmoonzer01 in forum NetworkingReplies: 1Last Post: 03-29-2008, 06:28 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks