Running two threads at the same time

In the case of an IM client. I created 2 separate threads for processing packets (via std io) and receiving packets. The question is how to make these two threads at the same time, so that I can continue to request input and at the same time be ready to receive packets at any time?

I already tried to set a timer, but the data is always lost.

+3
source share
3 answers

I think you might have missed something important with threads, threads, or both :-)

You can start a new thread as follows:

myThread.start ();

The thread starts and the run () method will execute automatically with jvm.

- , , - .

+7

. , :

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

, , , . , , , .

I/O, , , , . , ?

+7

Source: https://habr.com/ru/post/1706742/


All Articles