• 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:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Dan's Thread

 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can any explain it detail? Thanks.....
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nice explanation Anupam!
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jose
Thanks Jose for the compliment. I really liked it.
[ May 22, 2003: Message edited by: Anupam Sinha ]
 
Robbie kyodo
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Anupam
Is interrupt mentioned in KnB book ? I don't think I come across
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
So I left, I came home, and I ate some pie. And then I read this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic