Help answer threads with 0 replies.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 11-05-2004, 02:15 PM   #1
Registered: Oct 2003
Location: Austin,TX
Distribution: Debian SID-->fully content-->Love APT,kernel 2.6.4
Posts: 327

Rep: Reputation: 30
Stop java program(threaded program..should end cleanly)

Hey guys,
I am a bit new to threaded programming in java and have hit a wall. I have a program which rus a POP3 server(written in JAVA). I assig each new connection a thread(there is usually oly one connection at a time).
Now the oly way i know how to kill this process is to use kill -9 (on unix..I dont know how to do this on windows).
I wanted to write a shutdown method so that a user could cleanely exit the program. However, since I have never before written a program which keeps runnig until told to stop, I dont know how to implement a shutdown method.
I basically want something like this:
If the program is running then when the user calls something like:

java -jar POP3Server.jar shutdown
the program finishes all its ongoing processes and then exits cleanely.
I hope someone can help me with this.
Old 11-05-2004, 05:10 PM   #2
Registered: Mar 2004
Location: Valencia, espaņa
Distribution: Slack, Gentoo, Custom
Posts: 162

Rep: Reputation: 30
Perform the check and then call your designated shutdown method. This will cause the threads to terminate.

Old 11-09-2004, 10:00 AM   #3
LQ Guru
Registered: Jul 2003
Location: VA
Distribution: Slack 10.1
Posts: 2,194

Rep: Reputation: 47
Have the server listen over the network for a shutdown command.
Old 11-09-2004, 10:34 AM   #4
Registered: Oct 2003
Location: Austin,TX
Distribution: Debian SID-->fully content-->Love APT,kernel 2.6.4
Posts: 327

Original Poster
Rep: Reputation: 30
Originally posted by aaa
Have the server listen over the network for a shutdown command.
I tried that and though it does work, It does not suit my purposes since I need to close the sever after a fetch command from the client. If i just listen for aclose command, the socket will be closed as soon as i get one.
I am now trying to implement this using java.util.prefs. Lets see if I can manage to get this done.
Old 11-09-2004, 10:36 AM   #5
Senior Member
Registered: Apr 2003
Location: Costa Rica
Distribution: Kubuntu, Debian, Knoppix
Posts: 2,092
Blog Entries: 1

Rep: Reputation: 90
Well, man... as soon as you called "System.exit(0)", it should exit.. however it's not the "best behaved" implementation you can make. For the main process it will work perfectly, however, you should kill your running threads as well. As fas as I know you should implement a mechanisms in the threads so that they check for a "flag" to make sure they can keep on running. Have fun!

     public void run() {
          while (continue) do {
               blah, blah
continue being that flag (implement a method to set the flag after the thread started running).


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
How do i cleanly stop a service? n0vembr Linux - General 18 07-29-2005 05:11 AM
Odd error at the end of my C++ program myrrdyn Programming 5 04-07-2005 02:28 PM
front end for C program in linux ? ratheesh Programming 2 01-12-2004 03:35 AM
How do I stop a program that has crashed? Fe98 Linux - Newbie 4 12-08-2003 08:09 AM
Cant Compile(link) a C Threaded program tristan_vdv Programming 2 05-30-2002 06:16 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:53 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration