Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

queries on threads

 
Venkat Ramsimha
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Alex Belisle Turcot
Ranch Hand
Posts: 516
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

For your 1st question, I never tried it, but I dont see why you couldnt set the name of the main thread...



Can you please not write in caps lock.

Alex
[ April 30, 2005: Message edited by: Alex Turcot ]
 
Venkat Ramsimha
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry alex from hereafter i wont make the mistake of writing in caps and thanks for replying to 1st query but iam expecting the explanations and
solutions for the rest of the topics

thanks
venkatramsimha
 
Alex Belisle Turcot
Ranch Hand
Posts: 516
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I don't know about the stack, which is why I didn't say anything about it

As you can see in my signature, Im also currently studying for the SCJP. I just finished reading about the thread, so I'm not the king of it...

Whenever a thread calls its sleep(), wait() or join method(), it can be bring back to live with the interrupt method.

The interrupt method is a tool you can use to make sure that you thread is not stuck in sleep, wait or join "states". (However, when you do an interrupt(), the thread comes back to live in its InterruptedException catch block.

So, sleep(), join() and wait() calls must be surrounded by try-catch of InterruptedException.

Alex
[ May 02, 2005: Message edited by: Alex Turcot ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic