• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

queries on threads

 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 516
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic