Communication with Java (non-blocking socket)

I have a flash animation that communication with Java process through a socket.

I have a while loop that keeps collecting live sensor data from Java from the socket, then draw the sensor data on flash.

But when I run the flash, it just crash / freeze, how do I use multiple thread in flash so one thread is collect data from sensor in an infinite loop, other thread will draw the animation on screen once data is collected?


mySocket = new XMLSocket();

mySocket.onConnect = function(success)
{
    if (success){
        trace('connected');
        sendMsg();
    }
    else
        trace('failure');
}

mySocket.onClose = function()
{
    trace('close');
}

XMLSocket.prototype.onData = function(msg)
{
    trace('data: ' + msg);
}

mySocket.connect("localhost", 9999)

function sendMsg()
{
        var i:Number = 0;
        trace('sendMsg');
        while(true){
            i++;
            mySocket.send('msg-'+ i +'
');
            sleep(1);
        }
}






import java.io.*;
import java.net.*;

public class SimpleServer 
{
        
    public static void main(String args[])
    {
        // Message terminator
        char EOF = (char)0x00;
        
        try
        {
            // create a serverSocket connection on port 9999
            ServerSocket s = new ServerSocket(9999);
            
            System.out.println("Server started. Waiting for connections ...");
            // wait for incoming connections
            Socket incoming = s.accept();
            
            BufferedReader data_in = new BufferedReader(new InputStreamReader(incoming.getInputStream()));
            PrintWriter data_out = new PrintWriter(incoming.getOutputStream());
            
            data_out.println("Welcome! type EXIT to quit." + EOF);
            data_out.flush();
            
            boolean quit = false;
            
            // Waits for the EXIT command
            while (!quit)
            {
                String msg = data_in.readLine();
                
                if (msg == null) quit = true;
                
                if (!msg.trim().equals("EXIT"))
                {
                    System.out.println(msg);
                    data_out.println("You sayed+++: <b>" + msg.trim() + "</b>" + EOF);
                    data_out.flush();
                }
                else
                {
                    quit = true;
                }
            }
        }
        catch (Exception e)
        {
            System.out.println("Connection lost");
        }
    }
    
}