473,406 Members | 2,707 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

why does this program not end?

I read the following in the Thread API:

When a Java Virtual Machine starts up, there is usually a single non-daemon
thread (which typically calls the method named main of some designated
class). The Java Virtual Machine continues to execute threads until either
of the following occurs:
- The exit method of class Runtime has been called and the security manager
has permitted the exit operation to take place.
- All threads that are not daemon threads have died, either by returning
from the call to the run method or by throwing an exception that propagates
beyond the run method.

In the program below I have two Threads, the first thread reaches the end of
main() and the second thread reaches the end of run() because I see all of
the print statements. But the program doesn't terminate (I have to kill it
with Control + c) and I'm wondering why that is.

public class Test {
public static void main(String[] args) throws Exception {
Test2 h = new Test2();
System.out.println("before start");
h.start();
System.out.println("after start");
}
}
public class Test2 extends Thread {
public void run() {
System.out.println("starting run");
System.out.println("ending run");
}
}
Jul 17 '05 #1
2 4481
andthen wrote:
But the program doesn't terminate (I have to kill it
with Control + c) and I'm wondering why that is.

public class Test {
public static void main(String[] args) throws Exception {
Test2 h = new Test2();
System.out.println("before start");
h.start();
System.out.println("after start");
}
}
public class Test2 extends Thread {
public void run() {
System.out.println("starting run");
System.out.println("ending run");
}
}


This program works as expected for me; it must be a bug in your JVM if
it does not work properly. I'm on linux using:

$ java -version
java version "1.4.2_04"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_04-b05)
Java HotSpot(TM) Client VM (build 1.4.2_04-b05, mixed mode)
HTH,
Ray

--
XML is the programmer's duct tape.
Jul 17 '05 #2
andthen wrote:
I read the following in the Thread API:

When a Java Virtual Machine starts up, there is usually a single non-daemon
thread (which typically calls the method named main of some designated
class). The Java Virtual Machine continues to execute threads until either
of the following occurs:
- The exit method of class Runtime has been called and the security manager
has permitted the exit operation to take place.
- All threads that are not daemon threads have died, either by returning
from the call to the run method or by throwing an exception that propagates
beyond the run method.

In the program below I have two Threads, the first thread reaches the end of
main() and the second thread reaches the end of run() because I see all of
the print statements. But the program doesn't terminate (I have to kill it
with Control + c) and I'm wondering why that is.

public class Test {
public static void main(String[] args) throws Exception {
Test2 h = new Test2();
System.out.println("before start");
h.start();
System.out.println("after start");
}
}
public class Test2 extends Thread {
public void run() {
System.out.println("starting run");
System.out.println("ending run");
}
}


Also this program works as expected for me; I'm on Window XP SP 2 using:

java -version
java version "1.5.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-b64)
Java HotSpot(TM) Client VM (build 1.5.0-b64, mixed mode, sharing)

Thanks in Advance...
IchBin
__________________________________________________ ________________________

'The meeting of two personalities is like the contact of two chemical
substances:
if there is any reaction, both are transformed.'
- Carl Gustav Jung, (1875-1961), psychiatrist and psychologist
Jul 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Mike | last post by:
I am sure that I am making a simple boneheaded mistake and I would appreciate your help in spotting in. I have just installed apache_2.0.53-win32-x86-no_ssl.exe php-5.0.3-Win32.zip...
12
by: Fred Pacquier | last post by:
First off, sorry for this message-in-a-bottle-like post... I haven't been able to phrase my questions well enough to get a meaningful answer from Google in my research. OTOH, it is standard...
22
by: Robert Bralic | last post by:
CAN anybody tell me any address where I can download some small(1000-2000) lines C++ proghram source. Or send me ,a small(1000-2000) lines C++ program source that I can compille with gpp under...
75
by: Beni | last post by:
I have been programming in C for about a year now. It sounds silly, but I never took the time to question why a C(or C++ or Java) program execution begins only at the main(). Is it a convention or...
74
by: Suyog_Linux | last post by:
I wish to know how the free()function knows how much memory to be freed as we only give pointer to allocated memory as an argument to free(). Does system use an internal variable to store allocated...
2
by: george r smith | last post by:
Gentlemen, I know that this problem has happened before and I searched and tried all solutions but problem is not solved. I install asp.net community starter kit with some help from this...
126
by: ramyach | last post by:
Hi friends, I need to write a parallel code in 'C' on the server that is running SGI Irix 6.5. This server supports MIPS Pro C compiler. I don't have any idea of parallel C languages. I looked...
89
by: Cuthbert | last post by:
After compiling the source code with gcc v.4.1.1, I got a warning message: "/tmp/ccixzSIL.o: In function 'main';ex.c: (.text+0x9a): warning: the 'gets' function is dangerous and should not be...
9
by: DanielJohnson | last post by:
I am wondering where does the value returned by main goes. Suppoes main returns some number say 42, where is it stored. Supposed a shell script call a C program and the program returns the value...
6
by: itsraghz | last post by:
Dear All, I have an issue with destroy() method of java.lang.Process class. All what I am trying to do is, controlling the execution of one program through another. Let's say, Program B has to be...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.