LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

Reply
 
LinkBack Search this Thread
Old 11-05-2004, 01:15 PM   #1
rmanocha
Member
 
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:

Code:
java -jar POP3Server.jar shutdown
the program finishes all its ongoing processes and then exits cleanely.
I hope someone can help me with this.
thanks
 
Old 11-05-2004, 04:10 PM   #2
darkRoom
Member
 
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.

Code:
System.exit(0);
 
Old 11-09-2004, 09:00 AM   #3
aaa
Guru
 
Registered: Jul 2003
Location: VA
Distribution: Slack 10.1
Posts: 2,194

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

Original Poster
Rep: Reputation: 30
Quote:
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.
Thanks
 
Old 11-09-2004, 09:36 AM   #5
eantoranz
Senior Member
 
Registered: Apr 2003
Location: Colombia
Distribution: Kubuntu, Debian, Knoppix
Posts: 1,957
Blog Entries: 1

Rep: Reputation: 81
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!

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


Reply


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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


All times are GMT -5. The time now is 05:28 AM.

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