 | homework help please.... The initial balance value is $5000. The payroll thread adds $1000 to the balance each time. The withdraw thread deducts $60 each time. To make the race condition more obvious and interesting, the payroll is activated for 5 times, and the withdraw is activated for 30 times.
First, compile the code. Then run it 10 times. Record the result of the final balance you see in each execution (so you should write down 10 numbers). Are they consistent? What is the correct final balance? Explain the reason behind the phenomenon.
Here's the code: ---------------------------------------------------------------------------------- -------- class SharedData { private int balance=5000; public int getBalance() { return balance; }
public void setBalance (int bal) { balance = bal; } }
class Payroll implements Runnable { private SharedData sd;
public Payroll (SharedData sd) { this.sd = sd; }
public void run() { int balance=sd.getBalance(); System.out.println("Inside payroll, balance is "+balance); try { Thread.sleep((int)(Math.random()*10 )); } catch (InterruptedException e) { } sd.setBalance (balance+1000); System.out.println("Inside payroll: New balance is "+sd.getBalance()); } }
class Withdraw implements Runnable { private SharedData sd;
public Withdraw (SharedData sd) { this.sd = sd; }
public void run() { int balance=sd.getBalance(); System.out.println("Inside withdraw, balance is "+balance); try { Thread.sleep((int)(Math.random() * 100)); } catch (InterruptedException e) { } sd.setBalance (balance-60); System.out.println("Inside withdraw: New balance is "+sd.getBalance()); }
}
class lab3_multi { public static void main (String arg[]) { SharedData sd = new SharedData(); Payroll[] payrolls = new Payroll [5]; Thread[] payTh = new Thread [5]; Withdraw[] withdraws = new Withdraw [30]; Thread[] wdTh = new Thread [30];
for (int i=0; i5; i++){ payrolls[i]= new Payroll(sd); payTh[i]= new Thread (payrolls [i] ); } for (int j=0; j30; j++){ withdraws[j]=new Withdraw(sd); wdTh[j]= new Thread (withdraws[j]); }
for (int i=0; i5; i++) { payTh[i].start(); } for (int j=0; j30; j++) { wdTh[j].start(); } } } ---------------------------------------------------------------------------------- ---------- ------------ part 1:
Use Java Synchronization to avoid the race condition presented in the code. part 2:
Use semaphore approach for this problem.
part 3:
Modify the code so that only one payroll thread and one withdraw thread are launched. Then use Peterson's solution for mutual exclusion between the payroll and withdraw threads. |
|
 SholnayMy Name Is In My ProfilePremium join:2000-12-08 Dallas, TX | so... you want us to do this for you?
sincerely, -confused. |
|
 | well, i don't know where to start, my teacher sucks, i just thought someone would help out...... |
|
 javaManThe Dude abides.Premium,MVM join:2002-07-15 San Luis Obispo, CA | reply to kickerman97 I would start at the beginning. The instructions are pretty straight forward. You will, no doubt, need to sit and think about the problem though; that's what the assignment is designed to do. If you have a question about something specific I'm sure someone will be glad to assist you. -- Woe unto them that call evil good, and good evil; that put darkness for light, and light for darkness. . . Isa. 5:20 |
|
 | thanks javaman |
|