Running 2 threads simultaneously

lamsaitat picture lamsaitat · Apr 20, 2009 · Viewed 58.6k times · Source

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.

Answer

jsight picture jsight · Apr 20, 2009

Without more details, it is hard to give a complete answer. Nevertheless, here is the code for starting two threads:

Thread thread1 = new Thread () {
  public void run () {
    // ... your code here
  }
};
Thread thread2 = new Thread () {
  public void run () {
    // ... your code here
  }
};
thread1.start();
thread2.start();