当前位置:首页 » 软件开发
开发技术指南» 文章正文
    引言: Have you ever wondered why
 

 

 ·j2me tutorial, part 1: creating midlets    »显示摘要«
    摘要:java 2 micro edition (j2me) combines a resource-constrained jvm and a set of java apis for developing applications for mobile devices. this article is the first in a series. this time, after a qui......
 ·revenge of the old fogeys? @ jdj    »显示摘要«
    摘要:(august 26, 2002) - for years, the badge of honor for a software engineer was the ability to write fast code that occupied a small amount of memory. in the days when 640k of ram was a luxury and 10 mh......


Beware the Daemons @ JDJ

have you ever wondered why its necessary to call system.exit() to force your java ui application to exit? have a look at the code in listing 1.

after the setvisible() method has been called at line 15, the main() method completes execution. why doesnt the program end at that point?

the answer to this question is rooted in a subtlety of javas thread creation mechanism.

java makes a distinction between a user thread and another type of thread known as a daemon thread. the difference between these two types of threads is straightforward. if the java runtime determines that the only threads running in an application are daemon threads (i.e., there are no user threads), the java runtime promptly closes down the application. on the other hand, if at least one user thread is alive, the java runtime wont terminate your application. in all other respect(s) the java runtime treats daemon threads and user threads in exactly the same manner.

when your main() method initially receives control from the java runtime, it executes in the context of a user thread. as long as the main-method thread or any other user thread remains alive, your application will continue to execute.

to see just how a thread becomes a daemon thread, read on.

going to the devil

there are two ways a thread can become a daemon thread (or a user thread, for that matter) without putting your soul at
...   下一页
 ·java tech: language lessons    »显示摘要«
    摘要:i first encountered java in 1995. that language soon became my favorite because of its elegance, its interesting language features (e.g., interfaces, garbage collector, and threads), and its simila......
» 本期热门文章:

©2000-2007 All Rights Reserved. 最佳浏览:1024X768 MSIE