CECS 277
LAB ASSIGNMENT #6
Assigned date:4/15
Due date: 4/22
20 points
1. [5 points] Given the main method below and its output:
public class TwoThreadsDemo {
public static void main (String[] args) {
new SimpleThread("CECS 277").start();
new SimpleThread("OOP in Java").start();
}
}
Output
0 OOP in Java
0 CECS 277
1 OOP in Java
2 OOP in Java
1 CECS 277
3 OOP in Java
4 OOP in Java
2 CECS 277
5 OOP in Java
3 CECS 277
4 CECS 277
5 CECS 277
6 OOP in Java
6 CECS 277
7 OOP in Java
7 CECS 277
8 CECS 277
8 OOP in Java
9 CECS 277
DONE! CECS 277
9 OOP in Java
DONE! OOP in Java
Implement the class SimpleThread.
2. [5 points] Here is a runnable task, called TaskThread
. This task performs some work and then periodically reports what percent of the work it has completed:
import java.util.*;
public class TaskThread implements Runnable {
private int taskNumber;
TaskThread(int number) {
taskNumber = number;
}
public void run() {
for (int i=0;i<=100;i+=20) {
// Perform some task ...
System.out.println("Task number: " + taskNumber
+ ", percent complete: " + i );
try {
Thread.sleep((int)(Math.random() * 1000));
}
catch (InterruptedException e)
{
}
}//end for
}//end main
}//end class
You're allow to have two threads. Write a main method to produce the following ouput:
Task number: 1, percent complete: 0
Task number: 0, percent complete: 0
Task number: 1, percent complete: 20
Task number: 0, percent complete: 20
Task number: 1, percent complete: 40
Task number: 1, percent complete: 60
Task number: 0, percent complete: 40
Task number: 0, percent complete: 60
Task number: 1, percent complete: 80
Task number: 1, percent complete: 100
Task number: 0, percent complete: 80
Task number: 2, percent complete: 0
Task number: 2, percent complete: 20
Task number: 0, percent complete: 100
Task number: 2, percent complete: 40
Task number: 3, percent complete: 0
Task number: 3, percent complete: 20
Task number: 3, percent complete: 40
Task number: 2, percent complete: 60
Task number: 3, percent complete: 60
Task number: 3, percent complete: 80
Task number: 2, percent complete: 80
Task number: 2, percent complete: 100
Task number: 3, percent complete: 100
3. [10 points] Read the BankAccount class problem in the book from pages 809 to 824. Add a condition to the deposit method of the class BankAccount class, restricting deposits to $100,000.00 (the insurance limit of the U.S. government). The method should block until the sufficient money have been withdrawn by another thread. Test your program with a large number of deposit threads.
/**
This program runs threads that deposit and withdraw
money from the same bank account.
*/
public class BankAccountThreadRunner
{
public static void main(String[] args)
{
BankAccount account = new BankAccount();
final double AMOUNT = 10000;
final int REPETITIONS = 10;
final int DEPOSIT_THREADS = 10;
final int WITHDRAW_THREADS = 2;
for (int i = 0; i < DEPOSIT_THREADS; i++)
{
DepositRunnable d = new DepositRunnable(account, AMOUNT, REPETITIONS);
Thread t = ____________________;
__________________
}
for (int i = 0; i < WITHDRAW_THREADS; i++)
{
WithdrawRunnable d = new WithdrawRunnable(account, AMOUNT, REPETITIONS * DEPOSIT_THREADS / WITHDRAW_THREADS);
Thread t ____________
_____________________________
}
}
}
public class BankAccount
{
public static final double MAX_BALANCE = 100000;
private double balance;
private Lock balanceChangeLock;
private Condition sufficientFundsCondition;
private Condition lessThanMaxBalanceCondition;
/**
Constructs a bank account with a zero balance.
*/
public BankAccount()
{
balance = 0;
......
......
}
/**
Deposits money into the bank account.
@param amount the amount to deposit
*/
public void deposit(double amount)
throws InterruptedException
{
______________________________________________
try
{
while (balance + amount > MAX_BALANCE)
____________________________________________
System.out.print("Depositing " + amount);
double newBalance = balance + amount;
System.out.println(", new balance is " + newBalance);
balance = newBalance;
____________________________________________________
}
finally
{
_________________________________________
}
}
/**
Withdraws money from the bank account.
@param amount the amount to withdraw
*/
public void withdraw(double amount)
throws InterruptedException
{
_____________________________________
try
{
while (balance < amount)
______________________________________________
System.out.print("Withdrawing " + amount);
double newBalance = balance - amount;
System.out.println(", new balance is " + newBalance);
balance = newBalance;
__________________________________________________
}
finally
{
_______________________________________
}
}
/**
Gets the current balance of the bank account.
@return the current balance
*/
public double getBalance()
{
return balance;
}
}
/**
A deposit runnable makes periodic deposits to a bank account.
*/
public class DepositRunnable _____________________________
{
private static final int DELAY = 1;
private BankAccount account;
private double amount;
private int count;
/**
Constructs a deposit runnable.
@param anAccount the account into which to deposit money
@param anAmount the amount to deposit in each repetition
@param aCount the number of repetitions
*/
public DepositRunnable(BankAccount anAccount, double anAmount,
int aCount)
{
account = anAccount;
amount = anAmount;
count = aCount;
}
public void run()
{
try
{
for (int i = 1; i <= count; i++)
{
____________________________
__________________________________
}
}
catch (InterruptedException exception) {}
}
}
/**
A withdraw runnable makes periodic withdrawals from a bank account.
*/
public class WithdrawRunnable implements Runnable
{
private static final int DELAY = 1;
private BankAccount account;
private double amount;
private int count;
/**
Constructs a withdraw runnable.
@param anAccount the account from which to withdraw money
@param anAmount the amount to deposit in each repetition
@param aCount the number of repetitions
*/
public WithdrawRunnable(BankAccount anAccount, double anAmount,
int aCount)
{
account = anAccount;
amount = anAmount;
count = aCount;
}
public void run()
{
try
{
for (int i = 1; i <= count; i++)
{
______________________________
__________________________
}
}
catch (InterruptedException exception) {}
}
}