Hi All,
I have some queries on threads anybody please help me out
1.WE KNOW THAT WE CAN SET A NAME TO THE
THREAD BY USING "SET" METHOD.SIMILARLY CAN WE SET THE NAME FOR THE "MAIN" METHOD IF SO HOW ..ANY SAMPLE PROGRAM?
2.WHT ACTUALLY IS THE "STACK" AND WHTS THE FUNCTIONALITY OF IT?AND WHY THE "MAIN" THREAD RESIDES ON THE TOP OF THE STACK?
3.HOW IS IT POSSIBLE TO SAY THAT SINCE "SLEEP" METHOD BEING OF PART OF "THREAD CLASS" ON BEING INVOKED ON THE THREAD MIGHT RESULT TO THROW AN "InterruptedException" ON WHT BASIS WE CAN SAY THAT?
4.public class AccountDanger implements Runnable
{
private Account acct = new Account();
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();
}
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());
}
}
}
for the above program iam getting compile time errors along with that i needs the explanation for the above program
thanks
venkatramsimha