Results 1 to 3 of 3
  1. #1
    McChill is offline Member
    Join Date
    Feb 2009
    Posts
    47
    Rep Power
    0

    Default Thread questions

    I have these 3 classes --

    Java Code:
    public class Account {
    	
    	private int balance = 0;
    	
    	void deposit(int amount)
    	{
    		balance += amount;
    	}
    	
    	synchronized int getBalance()
    	{
    		return balance;
    	}
    
    }
    Java Code:
    public class Customer extends Thread{
    	Account account;
    	
    	Customer(Account account)
    	{
    		this.account = account;
    	}
    	
    	public void run()
    	{
    		try {
    			for(int i = 0;i<100000;i++)
    			{
    				account.deposit(10);
    			}
    		}
    		catch(Exception e)
    		{
    			e.printStackTrace();
    		}
    		
    	}
    
    }
    Java Code:
    public class BankDemo {
    
    	public final static int NUMCUSTOMERS = 10;
    	
    	public static void main(String args[])
    	{
    		Account account = new Account();
    		
    		Customer customers[] = new Customer[NUMCUSTOMERS];
    		for(int i=0;i<NUMCUSTOMERS;i++)
    		{
    			customers[i] = new Customer(account);
    			customers[i].start();
    		}
    		
    		//Wait for customer threads to complete
    		for(int i = 0;i<NUMCUSTOMERS;i++)
    		{
    			try {
    				customers[i].join();
    			}
    			catch(InterruptedException e)
    			{
    				e.printStackTrace();
    			}
    			
    		}
    		
    		System.out.println(account.getBalance());
    		
    	}
    	
    	
    }
    Basically, it just makes some customers (as threads) and calls the deposit method a lot. One question I have is that before I put the synchronized modifier in the deposit method, my output was wrong (it is supposed to be 10,000,000 and it came out to some random crazy number). Why does the program not work without the synchronized modifier? Also, in the BankDemo, why do I have to call the join() method to end a thread?

  2. #2
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    Reason not work without the synchronized modifier(Thread Interference (The Java™ Tutorials > Essential Classes > Concurrency))

    j.join() method...
    Thread object j is currently executing, causes the current thread to pause execution until t's thread terminates
    (Joins (The Java™ Tutorials > Essential Classes > Concurrency))

    i have a question
    all Customer use the same account?
    Last edited by mtyoung; 03-17-2009 at 06:02 AM.

  3. #3
    Thennarasu's Avatar
    Thennarasu is offline Member
    Join Date
    Jan 2009
    Location
    Chennai, Tamilnadu
    Posts
    39
    Rep Power
    0

    Default

    Hi,

    Look into this below link to know more about synchronized modifier.
    Sun Certified Java Programmer Pre-Exam Essentials
    One Life!!! Y Serious??? :)

Similar Threads

  1. Difference between Thread.yield() and Thread.sleep() methods
    By Nageswara Rao Mothukuri in forum New To Java
    Replies: 12
    Last Post: 07-30-2010, 05:37 PM
  2. passing a value from parent thread to child thread
    By sachinj13 in forum Threads and Synchronization
    Replies: 7
    Last Post: 09-07-2008, 09:06 PM
  3. data from the main/GUI thread to another runnin thread...
    By cornercuttin in forum Threads and Synchronization
    Replies: 2
    Last Post: 04-23-2008, 10:30 PM
  4. Replies: 0
    Last Post: 01-28-2008, 07:02 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
  •