Current Position:Home > A problem with threads

A problem with threads

Update:10-11Source: network consolidation
Advertisement
I am trying to implement some kind of a server listening for requests. The listener part of the app, is a daemon thread that listens for connections and instantiates a handling daemon thread once it gets some. However, my problem is that i must be able to kill the listening thread at the user's will (say via a sto button). I have done this via the Sun's proposed way, by testing a boolean flag in the loop, which is set to false when i wish to kill the thread. The problem with this thing is the following...
Once the thread starts excecuting, it will test the flag, find it true and enter the loop. At some point it will LOCK on the server socket waiting for connection. Unless some client actually connects, it will keep on listening indefinatelly whithought ever bothering to check for the flag again (no matter how many times you set the damn thing to false).
My question is this: Is there any real, non-theoretical, applied way to stop thread in java safely?
Thank you in advance,
Lefty

The Best Answer

Advertisement
This was one solution from the socket programming forum, have you tried this??
public Thread MyThread extends Thread{
     boolean active = true;          
     public void run(){
          ss.setSoTimeout(90);               
          while (active){                   
               try{                       
                    serverSocket = ss.accept();
               catch (SocketTimeoutException ste){
               // do nothing                   
     // interrupt thread           
     public void deactivate(){               
          active = false;
          // you gotta sleep for a time longer than the               
          // accept() timeout to make sure that timeout is finished.               
          try{
               sleep(91);               
          }catch (InterruptedException ie){            
          interrupt();
}
  • Problem with Thread and InputStream Update:10-11

    Hi, I am having a problem with threads and InputStreams. I have a class which extends Thread. I have created and started four instances of this class. But only one instance finishes its' work. When I check the state of other three threads their state

  • A problem with threads Update:10-11

    I am trying to implement some kind of a server listening for requests. The listener part of the app, is a daemon thread that listens for connections and instantiates a handling daemon thread once it gets some. However, my problem is that i must be ab

  • A problem with Threads and MMapi Update:10-11

    I am tring to execute a class based on Game canvas. The problem begin when I try to Play both a MIDI tone and to run an infinit Thread loop. The MIDI tone "Stammers". How to over come the problem? Thanks in advance Kobi See Code example below: i

  • J2ME problem with threads Update:10-11

    Hi all, I would like to ask you for a help. I need to write a small program at my university. I started to write a midlet which function would be to countdown time for sports activities. I woul like to start a new thread - the one that counts down -

  • Problem with threads within applet Update:10-11

    Hello, I got an applet, inside this applet I have a singleton, inside this singleton I have a thread. this thread is running in endless loop. he is doing something and go to sleep on and on. the problem is, when I refresh my IE6 browser I see more th

  • Problem with Threads and a static variable Update:10-11

    I have a problem with the code below. I am yet to make sure that I understand the problem. Correct me if I am wrong please. Code functionality: A timer calls SetState every second. It sets the state and sets boolean variable "changed" to true. T

  • Problem with threads running javaw Update:10-11

    Hi, Having a problem with multi thread programming using client server sockets. The program works find when starting the the application in a console using java muti.java , but when using javaw multi.java the program doesnt die and have to kill it in

  • Problem with Threads and "plase wait..."-Window Update:10-11

    Hi everyone, I have a problem that I'm not able to solve in any way... I have a time-consuming task (a file decryption) which I execute in a separate thread; I've used the SwingWorker class, like suggested by sun-tutorial, and it works right. The pro

  • Problem with threads in j2me Update:10-11

    im using jdk 1.6 with java wtk 2.1 ya i know.. 2.1 is a jdk1.4 source but im using souce as 1.4 during compiliation so thats not the prob this is my code package org.learn; import javax.microedition.midlet.MIDlet; import javax.microedition.lcdui.Aler

  • Problem with threads and camera. Update:10-11

    Hi everybody! I've a problem with taking snapshot. I would like to display a loading screen after it take snapshot ( sometimes i have to wait few seconds after i took snapshot. Propably photo is being taken in time where i have to wait). I was trying