dcsimg
www.webdeveloper.com
Results 1 to 7 of 7

Thread: please help........homework...

  1. #1
    Join Date
    Oct 2004
    Posts
    9

    please help........homework...

    i don't really know how to describe it, so i'll just post the whole thing. Your help would be greatly appreciated..thanks in advance

    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 for 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; i<5; i++){
    payrolls[i]= new Payroll(sd);
    payTh[i]= new Thread (payrolls [i] );
    }
    for (int j=0; j<30; j++){
    withdraws[j]=new Withdraw(sd);
    wdTh[j]= new Thread (withdraws[j]);
    }

    for (int i=0; i<5; i++) {
    payTh[i].start();
    }
    for (int j=0; j<30; 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.

  2. #2
    Join Date
    Nov 2003
    Location
    Jerryville, Tejas
    Posts
    11,715
    What's the question? "How do I compile and run a Java program?" or "What is the procedure for dropping this class?"

  3. #3
    Join Date
    Oct 2004
    Posts
    9
    actually the question is "How do i do part 1,2,and 3?", but thanks for your reply.

  4. #4
    Join Date
    Nov 2003
    Location
    Jerryville, Tejas
    Posts
    11,715
    If your prof or book didn't provide the answer then I'd recommend googling for "java synchronize example", "java semaphore example" and I'm afraid I have no idea what "Peterman's solution" means but if it's a valid CS term then you can google for that, too.

  5. #5
    Join Date
    Oct 2004
    Posts
    9
    thanks Ray

  6. #6
    Join Date
    Oct 2004
    Posts
    8
    Are you in beginning Java? Just curious. I'm taking a class too.

  7. #7
    Join Date
    Oct 2004
    Posts
    9
    i'm not really beginning in Java, this is my third class that has used java, but this one is way above my last one

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles