*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes queries on threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "queries on threads" Watch "queries on threads" New topic
Author

queries on threads

Venkat Ramsimha
Ranch Hand

Joined: Dec 28, 2004
Posts: 127
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

Joined: Apr 26, 2005
Posts: 516
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

Joined: Dec 28, 2004
Posts: 127
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

Joined: Apr 26, 2005
Posts: 516
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: queries on threads
 
Similar Threads
join() vs. synchronized
Regarding Syncronization
Synchronised Vs. Non synchronised Thread
Synchronized method won't let other thread go, why?
Problem with threads (concurrency)