Results 1 to 2 of 2

Thread: Threading

  1. #1
    jon80's Avatar
    jon80 is offline Senior Member
    Join Date
    Feb 2008
    Location
    Malta (EU)
    Posts
    211
    Rep Power
    7

    Default Threading

    The following code carries out random transfers to/from randomly generated accounts. I've been reading that in the following code there is a possibility (that might manifest itself intermittently) that a thread is created that overwrites the changes made by a pre-emptive thread that "goes to sleep" until it is called by the OS.

    Does anyone have a solution or suggestions for this problem?

    Java Code:
    [B]UnsynchBankTest.java[/B]
    package homenetwork.bkr.training;
    
    /**
     * This program shows data corruption when multiple threads access a data structure.
     * @author Administrator
     */
    public class UnsynchBankTest {
    		public static void main (String[] args)
    		{
    			Bank b = new Bank(NACCOUNTS, INITIAL_BALANCE);
    			int i;
    			for (i = 0; i < NACCOUNTS; i++)
    			{
    				TransferRunnable r = new TransferRunnable(b, i, INITIAL_BALANCE);
    				Thread t = new Thread(r);
    				t.start();
    			}
    		}
    		public static final int NACCOUNTS = 100;
    		public static final double INITIAL_BALANCE = 1000;
    }
    
    
    [B]Bank.java[/B]
    package homenetwork.bkr.training;
    /**
     * A bank with a number of bank accounts.
     */
    public class Bank {
    	
    	/** Constructs the bank.
    	 * @param n: the number of accounts
    	 * @param initialBalance: the initial balance for each account
    	 */
    	public Bank (int n, double initialBalance)
    	{
    		accounts = new double[n];
    		for (int i = 0; i < accounts.length; i++)
    			accounts[i] = initialBalance;
    	}
    	
    	/**
    	 * Transfers money from one account to another.
    	 * @param from: the account to transfer from
    	 * @param to: the account to transfer to
    	 * @param amount: the amount to transfer
    	 */
    	public void transfer (int from, int to, double amount)
    	{
    		if (accounts[from] < amount) return;
    		System.out.print(Thread.currentThread());
    		accounts[from] -= amount;
    		System.out.printf("%10.2f from %d to %d", amount, from, to);
    		accounts[to] += amount;
    		System.out.printf(" Total balance: %10.2f%n", getTotalBalance());
    	}
    	
    	/**
    	 * Gets the sum of all account balances.
    	 * @return: the total balance
    	 */
    	public double getTotalBalance()
    	{
    		double sum = 0;
    		for (double a: accounts)
    			sum += a;
    		
    		return sum;
    	}
    	
    	/** 
    	 * Gets the number of accounts in the bank.
    	 * @return: the number of accounts
    	 */
    	public int size()
    	{
    		return accounts.length;
    	}
    	
    	private final double[] accounts;
    
    }
    
    [B]TransferRunnable.java[/B]
    package homenetwork.bkr.training;
    /** 
     * A runnable that transfers money from an account to other accounts in a bank.
     * @author Administrator
     */
    public class TransferRunnable implements Runnable {
    	
    	/**
    	 * Constructs a transfer runnable.
    	 * @param b: the bank between whose account money is transferred.
    	 * @param from: the account to transfer money from.
    	 * @param max: the maximum amount of money in each transfer
    	 */
    	public TransferRunnable(Bank b, int from, double max)
    	{
    		bank = b;
    		fromAccount = from;
    		maxAmount = max;
    	}
    	
    	public void run()
    	{
    		try
    		{
    			while (true)
    			{
    				int toAccount = (int) (bank.size() * Math.random());
    				double amount = maxAmount * Math.random();
    				bank.transfer(fromAccount, toAccount, amount);
    				Thread.sleep((long) ((int) DELAY * Math.random()));
    			}
    		}
    		catch (InterruptedException e) {}
    	}
    	
    	private Bank bank;
    	private int fromAccount;
    	private double maxAmount;
    	private int DELAY = 10;
    	
    }
    NOTE 1: atm I'm running the code hoping to see something happen e.g. program crash
    NOTE 2: Ref#1 Pg.756 (nevermind what this means)
    NOTE 3: It would be helpful if I could update the subject title, because I realize I needed to make it more meaningful...

  2. #2
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    You need some kind of locking scheme for any data structure that is accessed by multiple threads. You need a system that follows the ACID principle.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

Similar Threads

  1. Problem in threading
    By saurabh in forum Threads and Synchronization
    Replies: 6
    Last Post: 12-01-2008, 08:16 PM
  2. Threading issue
    By Eku in forum New To Java
    Replies: 2
    Last Post: 09-18-2008, 10:47 AM
  3. Threading in EJB
    By java08 in forum Advanced Java
    Replies: 2
    Last Post: 08-12-2008, 11:09 AM
  4. Java threading
    By Eranga in forum Advanced Java
    Replies: 2
    Last Post: 03-13-2008, 05:30 AM
  5. Threading prob..
    By banie in forum Java Applets
    Replies: 0
    Last Post: 02-05-2008, 06:30 AM

Posting Permissions

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