File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "thread" Watch "thread" New topic
Author

thread

Jian Yi
Ranch Hand

Joined: Feb 01, 2002
Posts: 127
Question ID :966828596590
What will happen if you run the following program...

which are correct?
1. It will print "Starting Main"
2. It will print "Main returns"
3. It will not print "Main returns"
4. The program will never exit.
5. main() method will never return
what's teh difference between 4 and 5? why 4 is correct and 5 is wrong?
Thanks,
Jenny
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Hello Jenny,
After starting the new thread, the program continues execution: that is main returns and the new thread goes on forever.
Add the following constructor to see the difference respect a deamon thread (one that doesn't prevent the program from exiting)
TestClass()
{setDaemon(true);
}
[ April 22, 2002: Message edited by: Jose Botella ]

SCJP2. Please Indent your code using UBB Code
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by Jenny Yin:
what's the difference between 4 and 5? why 4 is correct and 5 is wrong?

4 and 5 are two entirely different statements. In 4, you're saying that the program will never exit. This is a true statement because the new thread that you've created (which is a user thread by default) will go into an infinite loop.
In statement 5, however, you're saying that the main method will call the start() method of your new thread and never continue. This is not true. Had you called run(), this would have been true. When you call start, the JVM creates a new thread and invokes it's run() method on your behalf. The main thread immediately returns from the call to start continues execution on the following line. Most likely (although not always), the main thread will terminate before the new thread even starts.
I hope that helps,
Corey


SCJP Tipline, etc.
WiLL Tao
Ranch Hand

Joined: Dec 14, 2001
Posts: 56
My question is
"Is it OS depended?"
eg:If in an Time Slice ystem
When executing

then SYSTEM switch to another thread,

after some time,then it switch to main thread,

now main thread terminate,switch to TestClass thread,infinite loop....
So the answer should be :
1,2,4;
Am i right?Thanks.


Thank Folks who <b>Make Sense</b> here.<br />SCJP Platform 2
Jian Yi
Ranch Hand

Joined: Feb 01, 2002
Posts: 127
I guess my confusion lies in the difference between the "program" and the "main thread." So what's the definition of a program? Correct me if I'm wrong, please. if any thread that a program starts doesn't exit, the program doesn't exit. So, in this case, TestClass thread never exits. Thus the program never exits.
BTW, Tao, you were right, the answer is 1,2,4.
Thanks guys!
Jenny
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by WiLL Tao:
So the answer should be :
1,2,4;
Am i right?Thanks.

Yes, the correct answers are 1, 2, & 4. We had been focusing on the difference between 4 and 5, but those are the correct answers.
Corey
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
There is a distinct difference between the program and the threads within that program.
Any program has at least one user thread executing within it at all times. Once all user threads have terminated, the program terminates.
That said, where does the main thread come in to play? Simple. When you start a Java application, a new thread is created (called the main thread), which executes the main method of the class your executing. That thread (which is a user thread) is capable of starting other threads (both user and daemon) which are then, in turn, capable of starting other new threads. It isn't until all of the user threads complete that the program exits.
Let's look at how this relates to this example. First, you start the program by executing the class TestClass. That causes a new user thread to be created to execute the main method (this is the main thread). The main thread creates a new thread (which is a user thread by default) and starts it. The main thread continues on and completes execution of the main method. The main thread, now that it has finished executing the main method, terminates. The thread that the main thread created, however, is stuck in an infinite loop.
So, even though the main thread has finished its work and has terminated, the program doesn't terminate because there is still a user thread executing.
I hope that helps clear things up,
Corey
Jian Yi
Ranch Hand

Joined: Feb 01, 2002
Posts: 127
Corey,
Your detailed explanation is greatly appreciated. I now undertand the diff btw the two.
Now I have another question. As Jose pointed out,
"Add the following constructor to see the difference respect a deamon thread (one that doesn't prevent the program from exiting)
TestClass()
{ setDaemon(true);
}"
I don't quite understand this. Doesn't a daemon thread never exit? Isn't that why it is called a daemon? Isn't that the diff btw a daemon thread and a user thread?
Thanks again,
Jenny
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Daemon threads don't prevent a program from completing. The program ends when the only threads running are daemon.
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by Corey McGlone:
Any program has at least one user thread executing within it at all times. Once all user threads have terminated, the program terminates.

The difference between user threads and daemon threads is that an application will continue to execute until all user threads have completed. At that time, if there are any daemon threads executing, they will be automatically terminated.
A prime example is the garbage collection thread. This thread is a daemon thread. It does its job (basically as a background process) and, when the application ends, you don't wait for the garbage collection thread to finish, it dies with the application.
Most often, daemon threads are used in this way - to implement "background processes." For example, if you're implementing a word processor, you might have a thread that does real-time spell checking. It would probably make sense to make this thread a daemon thread. That way, when the user closes the application, you don't have to kill the thread yourself, it just dies with the application.
So, back to the original question. Had the thread that you created been a daemon thread instead of a user thread, the program wouldn't have hung in an infinite loop, it would have terminated as soon as the main thread (the only user thread) finished executing the main method.
I hope that helps,
Corey
Gautam Sewani
Ranch Hand

Joined: Apr 19, 2002
Posts: 93
Just a minute,according to me the answer is 1,4.
The option 2 says,that the program will print "Main exiting",but this is not true on all platforms.For eg,on a non preemptive OS,if the user defined thread starts executing first,it will continue to execute a infinite loop.Nothing will stop it.So it is possible that "main exiting" will never be printed.
Please clear my doubts
Jian Yi
Ranch Hand

Joined: Feb 01, 2002
Posts: 127
Corey,
Your explanation is crystal clear. Thanks a lot!
-Jenny
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by Gautam Sewani:
Just a minute,according to me the answer is 1,4.
The option 2 says,that the program will print "Main exiting",but this is not true on all platforms.For eg,on a non preemptive OS,if the user defined thread starts executing first,it will continue to execute a infinite loop.Nothing will stop it.So it is possible that "main exiting" will never be printed.
Please clear my doubts

Unless I'm horribly mistaken, there isn't an OS on the market that will allow a thread to run indefinitely without making it share the processor. It seems foolish to rely on a thread to voluntairly give up the processor as many threads simply aren't designed to do this.
Even if the new thread begins executing right away (before that main method can end), it will eventually be preempted and the main thread will have access to the processor and be able to complete. Then, the main thread will go to the terminated state and another thread will get the processor (maybe the one in an infinite loop, maybe something outside the JVM entirely). That part of it is up to the underlying OS.
I hope that helps,
Corey
Jon Dornback
Ranch Hand

Joined: Apr 24, 2002
Posts: 137
i ran the code on a winNT box, and worked as follows: the starting main message printed, the ending main message printed, and then the program did not terminate while the proc usage shot up to 100%. so i get answers 1,2,4.
however, when i set the thread to daemon, it printed starting main, then ending main, and then it printed out an "illegal thread state exception" and exited. why does that happen? is this because the daemon thread is still running when the program tries to exit? from previous posts my understanding is that the thread should be killed automatically -- or am i confused?


use the [CODE] tags - it makes it much easier for people to help you.
Jessica Sant
Sheriff

Joined: Oct 17, 2001
Posts: 4313

"nonsleepingjon"-
Welcome to the JavaRanch! Please adjust your displayed name to meet the JavaRanch Naming Policy. You can do so here.
Thanks! and again, Welcome to the Ranch!
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by nonsleepingjon:
i ran the code on a winNT box, and worked as follows: the starting main message printed, the ending main message printed, and then the program did not terminate while the proc usage shot up to 100%. so i get answers 1,2,4.
however, when i set the thread to daemon, it printed starting main, then ending main, and then it printed out an "illegal thread state exception" and exited. why does that happen? is this because the daemon thread is still running when the program tries to exit? from previous posts my understanding is that the thread should be killed automatically -- or am i confused?

I believe the exception is thrown simply because the daemon thread was still alive when the application terminated. I haven't been able to find any documentation on it, but that's my best guess.
Note, though, that the application did terminate, even though it threw the exception. Had the thread been a user thread, as in the first case, the app wouldn't have terminated. Perhaps the exception is thrown in order to give you a chance to "clean up" when the application terminates.
I hope that helps,
Corey
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: thread