aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Dan's Thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Dan Watch "Dan New topic
Author

Dan's Thread

Robbie kyodo
Ranch Hand

Joined: May 05, 2003
Posts: 97
Can somebody explain this code ?

class A extends Thread {
public void run() {
long startTime = System.currentTimeMillis();
long endTime = startTime + 10000;
while (System.currentTimeMillis() < endTime) {
yield();
}
}
public static void main(String[] args) {
A a1 = new A();
long startTime = System.currentTimeMillis();
a1.start();
try {sleep(1000);} catch(InterruptedException ie) {}
a1.interrupt();
try {a1.join();} catch(InterruptedException ie) {}
System.out.print(System.currentTimeMillis() - startTime);
}
}

What are the possible


SCJP 2 1.4
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
In this question when a1's start() is called the run method of the a1 executes. In which the thread may constantly run for 10 secs because of the while loop. Now it is possible that the main thread doesn't gets a chance to execute and a1 keeps on executing. If the main thread doesn't gets a chance it will wait for atleast 10 secs. There is also a chance that when a1's start() method is called a1 might not get a chance to run, but as the main thread goes for a nice nap of a sec chances are that a1's start will execute(no gaurantee), in case that doesn't happens then a1 will wait for atleast 1 sec. Now when a1.interrupt() is called it will have no effect because the thread is not waiting, neither blocked so the thread's interrupt flag will be set and that's it. Now a1.join() is called so now main thread will atleast wait for the a1 thread to complete. Now the a1 thread will run and complete. Then the time elasped will be printed. Chances(only chances) are that the number would be 10000 and it is gauranteed that it will be either 10000 or greater than that. This happens because generally while the main thread sleeps thread a1 is completing its work.
Did I confuse you ? :roll:
[ May 22, 2003: Message edited by: Anupam Sinha ]
Robbie kyodo
Ranch Hand

Joined: May 05, 2003
Posts: 97
Confusing....

I don't understand this section :
There is also a chance that when a1's start() method is called a1 might not get a chance to run, but as the main thread goes for a nice nap of a sec chances are that a1's start will execute(no gaurantee), in case that doesn't happens then a1 will wait for atleast 1 sec. Now when a1.interrupt() is called it will have no effect because the thread is not waiting, neither blocked so the thread's interrupt flag will be set and that's it.

How come during a1.interrupt() the thread is not waiting ? Is it becoz interrupt was called after sleep (1000) ?
then what does yield do in run() ?
Robbie kyodo
Ranch Hand

Joined: May 05, 2003
Posts: 97
according to API

"If this thread is blocked in an invocation of the wait(), wait(long), or wait(long, int) methods of the Object class, or of the join(), join(long), join(long, int), sleep(long), or sleep(long, int), methods of this class, then its interrupt status will be cleared and it will receive an InterruptedException "
Where the flag will be set to false
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
The yield method inside of the while loop does not cause the thread to wait, sleep, block on IO, or do anything else that might cause it to move into the not-runnable state. Instead, the yield method might cause the thread to move into the ready state. Since the thread is not waiting, sleeping or otherwise blocking it will not even know that the interrupt has occurred unless it checks to see if the interrupt flag is set using the interrupted() method.


Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
Can any explain it detail? Thanks.....
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Sorry for confusing.

Ok lets examine the code. We'll start from main().
1. A a1 = new A(); An object of the type A is declared and instantiated. But till now no new thread has been created as you require to call the start() method.
2. long startTime = System.currentTimeMillis(); Stores the current time in startTime.
3. a1.start(); Causes the creation of a new thread. Now with this statement we tell the compiler "Whenever you feel, please execute my thread's run method."
Now it depends on compiler to compiler and even on the mood of the compiler that whether it executes your thread's run method or not.
Let's say that it acknowldges your request and starts executing your thread's run method. But now again there are two things that can happen the thread a1(your thread) might just monopolize the CPU causing the CPU to never run any other thread. Or it might just act in a nice gentle way and both threads co-operatively execute. Now the last option of co-operation is likely to happen. When this happens now we have thread a1 constantly iterating in a loop and thread main executing its code.
4. try {sleep(1000);} catch(InterruptedException ie) {} But now from main thread a call to sleep() method causes main() to go to sleep for a sec. It will become runnable after a sec but when will be run will depend on the thread scheduler. Remember while all this is hapening a1 might still be running.
5. a1.interrupt(); This piece of code executes when main thread comes out of the sleeping state. Now on a1 interrupt method is called. interrupt() method causes an exception to occur in case the thread has started and is currently not in the runnable state. But our thread a1 is in the runnable state and happily executing so it doesn't affects out thread. But it simply sets the flag on the thread that this thread's interrupt method was called and no exception was raised.
6. try {a1.join();} catch(InterruptedException ie) {} It causes the main thread to wait for thread a1 to finish execution so that it can continue.
7. System.out.print(System.currentTimeMillis() - startTime); Causes the elapsed time to be printed.
Whew that was a lot of typing.
Still having difficulty understanding.?
[ May 22, 2003: Message edited by: Anupam Sinha ]
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Nice explanation Anupam!


SCJP2. Please Indent your code using UBB Code
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
Quote from a previous post. Read the whole post for context:
Let's say that it acknowldges your request and starts executing your thread's run method. But now again there are two things that can happen the thread a1(your thread) might just monopolize the CPU causing the CPU to never run any other thread. Or it might just act in a nice gentle way and both threads co-operatively execute. Now the last option of co-operation is likely to happen. When this happens now we have thread a1 constantly iterating in a loop and thread main executing its code.

How is that possible? Thread a1 has yield() call. This call will allow other threads of equal priority (main thread in this case) to run.
[ May 22, 2003: Message edited by: Barkat Mardhani ]
[ May 22, 2003: Message edited by: Barkat Mardhani ]
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Hi Barkat
What you are saying will in most cases happen and thats why I said the co-operative way might be chosen. But there is no gaurantee because even if the thread a1 has a call to Thread.yield() there is no gaurantee that thread a1 will ever yield to a thread of higher, same or lower priority. Thread.yield() doesn't gaurantees that any other thread would be given a chance, not even if the other competing thread is a higher priority thread.
[ May 22, 2003: Message edited by: Anupam Sinha ]
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Hi Jose
Thanks Jose for the compliment. I really liked it.
[ May 22, 2003: Message edited by: Anupam Sinha ]
Robbie kyodo
Ranch Hand

Joined: May 05, 2003
Posts: 97
thanks Anupam
Is interrupt mentioned in KnB book ? I don't think I come across
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Well now I also don't really remember whether its mentioned in the book or not. This is also one of the reasons why I looooove Dan's questions. Though it may be out of context or may not be for the exam but you do get to learn a few new methods. Along with that Dan's exam give you a nice understanding of why this is correct or incorrect. In case I have any difficulty finding out any method or anything what this does, I refer to the api documentation or The Complete Reference Java 1.4(book). I guess that refering to the api documetation should be quite helpful in solving out a lot of problems.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Dan's Thread