Search code examples
javamultithreadingnetwork-programmingsimultaneous-calls

Running 2 threads simultaneously


In the case of an IM client. I have made 2 separate threads to handle sending packets (by std io) and receiving packets. The question is how to make these 2 threads run simultaneously so that I can keep prompting for input while at the same time be ready to receive packets at any time?

I have already tried setting a timer but the data is always lost receiving.


Solution

  • I think you might have missed something significant with either Threads, Streams or both :-)

    You can start a new thread like this:

    myThread.start();
    

    The thread will be started and the run() method will be executed automatically by the jvm.

    If the threads run-method is reading from a Stream, and it is the only one reading, it will not "miss" anything in that stream.