|  | |  |
10-23-2004, 09:50 PM
|
#1 (permalink)
| | Registered User
Join Date: Oct 2004
Posts: 9
| homework help...please...? i'll just post the whole thing and see what ideas you all have. 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. |
| |
10-24-2004, 04:32 AM
|
#2 (permalink)
| | Java fanboy
Join Date: Aug 2003
Posts: 1,175
| Again, you'll have to be more specific. People on these boards (and most coding boards) aren't interested in doing other people's homework assignments, but we will help if you have specific questions. |
| |
10-24-2004, 08:40 AM
|
#3 (permalink)
| | Registered User
Join Date: Oct 2004
Posts: 9
| o.k......for instance, how to do part one? the teacher for this class is horrible, she doesn't explain anything, and doesn't help you. |
| |
10-24-2004, 08:58 AM
|
#4 (permalink)
| | Moderator
Join Date: May 2002 Location: us.ca
Posts: 4,530
| Quote: Originally posted by Belisarius Again, you'll have to be more specific. People on these boards (and most coding boards) aren't interested in doing other people's homework assignments, but we will help if you have specific questions. |
__________________ Mike |
| |
10-24-2004, 09:04 AM
|
#5 (permalink)
| | Java fanboy
Join Date: Aug 2003
Posts: 1,175
| Welcome to college. Quote: Originally posted by kickerman97 o.k......for instance, how to do part one? the teacher for this class is horrible, she doesn't explain anything, and doesn't help you. | |
| |
10-24-2004, 07:18 PM
|
#6 (permalink)
| | Registered User
Join Date: Oct 2004
Posts: 9
| Belisarius.....if you don't want to help, just stay out of the thread please.....as far as welcome to college, i'm a senior so it's not like I just got here, and I haven't had really any problems up to this assignment, so thanks anyway |
| |
10-24-2004, 07:50 PM
|
#7 (permalink)
| | Moderator
Join Date: May 2002 Location: us.ca
Posts: 4,530
| kick, comon man, you are asking him to do the entire thing for you .. bel is the most qualified java guy here to help you .. at least show you're trying and post the code that is giving you a problem.
if you're a senior, you should at least be able to do that.
it's much more likely you will get help if you appear to at least try to help yourself. geesus
__________________ Mike |
| |
10-24-2004, 08:11 PM
|
#8 (permalink)
| | Java fanboy
Join Date: Aug 2003
Posts: 1,175
| You've been asked several times not to simply request us to do your homework for you. You then request us to do your homework for you.
I point you to a book and prove the author's creditentials as being an authority, no *the* authority, on the subject that seems to be vexing you. Have you bought/borrowed it?
I had professors that taught me that the biggest part of programming is problem solving, something anyone should be able to by their senior year. So far your posts haven't shown the slightest attempt at the skill. They amount to "here's my problem, will you please solve it?" I'm sorry if my sympathy runs shallow for you. |
| |
10-25-2004, 08:40 AM
|
#9 (permalink)
| | Registered User
Join Date: Oct 2004
Posts: 9
| jesus, i'll just go somewhere else, thanks alot, but to keep the thing going I'll say this. I don't understand why you get different values when you run it, so how am i supposed to tell anyone where my problem is? Once again, i don't want anyone to do it for me, i just don't know how to start because I don't understand what's happening..... |
| |
10-28-2004, 07:53 AM
|
#10 (permalink)
| | Newbie
Join Date: Jun 2002 Location: Denmark
Posts: 1,726
| The problem is in the thread handling in Java, if you withdraw befor you add then withdraw again, it is going to be different than add, withdraw, add.
It all depends on how the threads lock the program from any inteference from other threads performing actions uppon the total ammount.
Thats why you need to try different aproaches ie: Quote:
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
| What Bel is trying to tell you is this, figure it out by looking at how Java handles threads in teh ways you need to test it. (part 1, 2, 3) |
| | | Thread Tools | | | | Display Modes | Linear Mode |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | All times are GMT -8. The time now is 12:44 AM. |
Copyright © 2000-2008, Milano Interactive Web Hosting provided by Portal 360 Web Hosting |  | |