For Programmers: Free Programming Magazines  


Home > Archive > Java Help > May 2006 > Threads









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Threads
sarath

2006-05-19, 8:08 am

Hi everyone..

The question is about threads. i am posting two programs and their
corresponding outputs below this. you can try to run it both. how many
times i run them, the output is same. What i found in them is, if i
extend Thread, the instance variables are shared between the threads.
but when i am implementing Runnable, it does not happen. can anyone
explain this ?

thanks in advance

sarath



public class AccountDanger implements Runnable{
private Account acct = new Account();

/** Creates a new instance of AccountDanger */
public AccountDanger() {

}

public void run(){
for(int x= 0; x < 5 ; x++){
makeWithdrawal(10);
if(acct.getBalance() < 0){
System.out.println("account is overDrawn");
}
}
}

private void makeWithdrawal(int amt){
if(acct.getBalance() >= amt){
System.out.println(Thread.currentThread().getName()+" is
going to withdraw");
try{
Thread.sleep(500);
}catch(InterruptedException ex){

}
acct.withdraw(amt);
System.out.println(Thread.currentThread().getName()+"
completes the withdrawal");
}else{
System.out.println("Not enough in account for
"+Thread.currentThread().getName()+ " to withdraw "+acct.getBalance());
}
}

public static void main(String args[]){
AccountDanger r = new AccountDanger();
Thread one = new Thread(r);
Thread two = new Thread(r);
one.setName("Fred");
two.setName("Lucy");

one.start();
two.start();

}

}

class Account {

private int balance = 50;
public int getBalance(){
return balance;
}
public void withdraw(int amount){
balance = balance - amount;
}
}



output is,

Fred is going to withdraw
Lucy is going to withdraw
Fred completes the withdrawal
Fred is going to withdraw
Lucy completes the withdrawal
Lucy is going to withdraw
Fred completes the withdrawal
Fred is going to withdraw
Lucy completes the withdrawal
Lucy is going to withdraw
Fred completes the withdrawal
Not enough in account for Fred to withdraw 0
Not enough in account for Fred to withdraw 0
Lucy completes the withdrawal
account is overDrawn
Not enough in account for Lucy to withdraw -10
account is overDrawn
Not enough in account for Lucy to withdraw -10
account is overDrawn







/*
* AccountDanger.java
*
* Created on May 19, 2006, 5:13 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/

/**
*
* @author root
*/
public class AccountDanger extends Thread{
private Account acct = new Account();

/** Creates a new instance of AccountDanger */
public AccountDanger() {

}

public void run(){
for(int x= 0; x < 5 ; x++){
makeWithdrawal(10);
if(acct.getBalance() < 0){
System.out.println("account is overDrawn");
}
}
}

private void makeWithdrawal(int amt){
if(acct.getBalance() >= amt){
System.out.println(Thread.currentThread().getName()+" is
going to withdraw");
try{
Thread.sleep(500);
}catch(InterruptedException ex){

}
acct.withdraw(amt);
System.out.println(Thread.currentThread().getName()+"
completes the withdrawal");
}else{
System.out.println("Not enough in account for
"+Thread.currentThread().getName()+ " to withdraw "+acct.getBalance());
}
}

public static void main(String args[]){
AccountDanger one = new AccountDanger();
AccountDanger two = new AccountDanger();
//Thread one = new Thread(r);
//Thread two = new Thread(r);
one.setName("Fred");
two.setName("Lucy");

one.start();
two.start();

}

}

class Account {

private int balance = 50;
public int getBalance(){
return balance;
}
public void withdraw(int amount){
balance = balance - amount;
}
}




output :

Fred is going to withdraw
Lucy is going to withdraw
Fred completes the withdrawal
Fred is going to withdraw
Lucy completes the withdrawal
Lucy is going to withdraw
Fred completes the withdrawal
Fred is going to withdraw
Lucy completes the withdrawal
Lucy is going to withdraw
Fred completes the withdrawal
Fred is going to withdraw
Lucy completes the withdrawal
Lucy is going to withdraw
Fred completes the withdrawal
Fred is going to withdraw
Lucy completes the withdrawal
Lucy is going to withdraw
Fred completes the withdrawal
Lucy completes the withdrawal

Chris Smith

2006-05-19, 7:06 pm

sarath <mesarath@gmail.com> wrote:
> The question is about threads. i am posting two programs and their
> corresponding outputs below this. you can try to run it both. how many
> times i run them, the output is same. What i found in them is, if i
> extend Thread, the instance variables are shared between the threads.
> but when i am implementing Runnable, it does not happen. can anyone
> explain this ?


Okay, actually you saw exactly the opposite. This isn't a question
about threads at all. It's just basic OO concepts.

In the first case, you have ONE object of class AccountDanger. You are
asking two different threads to call the run() method of that object.
However, there's still only one AccountDanger object, and it has one
Account object as a field. In the second case, you are creating TWO
objects of class AccountDanger, and arranging for each of their run
methods to be called in a new thread. Hence, you have two different
Account objects as well, with each thread using a different account.

Forget about threads. Just look at what objects you are creating. Draw
object diagrams if it helps you. You'll see that your Runnable code
creates one Account object, whereas the code extending Thread creates
two.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
sarath

2006-05-20, 8:04 am

thanks chris,

that reply really helped..

regards
sarath

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com