Results 1 to 2 of 2
  1. #1
    phaulyne08 is offline Member
    Join Date
    Mar 2012
    Posts
    1
    Rep Power
    0

    Question 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();
    	}
    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,234
    Rep Power
    19

    Default Re: How to Simulate Deadlock Prevention in Java?

    Moved from New to Java

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Compiler deadlock
    By Arnold in forum New To Java
    Replies: 2
    Last Post: 06-27-2010, 12:57 PM
  2. Simulate a Network using Java
    By fung1223 in forum New To Java
    Replies: 0
    Last Post: 01-13-2010, 10:46 AM
  3. Thread Deadlock
    By ajeeb in forum New To Java
    Replies: 2
    Last Post: 01-16-2009, 02:49 AM
  4. simulate form type=button press with java application
    By redmoonzer01 in forum Networking
    Replies: 1
    Last Post: 03-29-2008, 06:28 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •