Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 10-23-2004, 09:50 PM   #1 (permalink)
kickerman97
Registered User
 
Join Date: Oct 2004
Posts: 9
kickerman97 is on a distinguished road
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.
kickerman97 is offline   Reply With Quote
Old 10-24-2004, 04:32 AM   #2 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,175
Belisarius is on a distinguished road
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.
__________________
GitS
Belisarius is offline   Reply With Quote
Old 10-24-2004, 08:40 AM   #3 (permalink)
kickerman97
Registered User
 
Join Date: Oct 2004
Posts: 9
kickerman97 is on a distinguished road
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.
kickerman97 is offline   Reply With Quote
Old 10-24-2004, 08:58 AM   #4 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,530
sde is on a distinguished road
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
sde is offline   Reply With Quote
Old 10-24-2004, 09:04 AM   #5 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,175
Belisarius is on a distinguished road
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.
__________________
GitS
Belisarius is offline   Reply With Quote
Old 10-24-2004, 07:18 PM   #6 (permalink)
kickerman97
Registered User
 
Join Date: Oct 2004
Posts: 9
kickerman97 is on a distinguished road
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
kickerman97 is offline   Reply With Quote
Old 10-24-2004, 07:50 PM   #7 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,530
sde is on a distinguished road
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
sde is offline   Reply With Quote
Old 10-24-2004, 08:11 PM   #8 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,175
Belisarius is on a distinguished road
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.
__________________
GitS
Belisarius is offline   Reply With Quote
Old 10-25-2004, 08:40 AM   #9 (permalink)
kickerman97
Registered User
 
Join Date: Oct 2004
Posts: 9
kickerman97 is on a distinguished road
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.....
kickerman97 is offline   Reply With Quote
Old 10-28-2004, 07:53 AM   #10 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
redhead is on a distinguished road
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)
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with a little CompSci homework? Dante Java 9 04-23-2003 09:46 AM


All times are GMT -8. The time now is 12:44 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0 RC8 ©2007, Crawlability, Inc.





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting