• Post Reply Bookmark Topic Watch Topic
  • New Topic

Multi Thread Interaction

 
Tarun Oohri
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Everyone,
Following is the code i am facing problem to understand. Here we are making 2 threads from the 'main' thread , which contains Input Stream & Output Stream of the Socket respectively.


Now when i run both files (ie. server side & client side) & try to send data from server to client through console , following is the output of the server

Question1. On the 7th line of the output , when the 'Server Input Thread' stops executing & the 'main' thread comes in 'Waiting for Client to connect...' gets printed by the main thread, then why doesnt the next line ' Client connected. IP: /192.168.1.15' got printed. Is it because at that perticular time 'Server Input Thread' started executing ?
Question2. When i sent 'Hello Client'...Everything goes fine but when i enter 'Tarun' , then why doesnt the following gets print before printing 3 ?
"2 Current Thread is : Server Output Thread
AFTER TAKING INPUT FROM CONSOLE "

Thanks In Advance!!!
 
Henry Wong
author
Sheriff
Posts: 22542
109
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tarun Oohri wrote:
Question1. On the 7th line of the output , when the 'Server Input Thread' stops executing & the 'main' thread comes in 'Waiting for Client to connect...' gets printed by the main thread, then why doesnt the next line ' Client connected. IP: /192.168.1.15' got printed. Is it because at that perticular time 'Server Input Thread' started executing ?


The main thread and the input thread are two different threads (actually, more depending on the number of clients). The main thread goes back to accepting another connection after starting the input thread. The ordering in how the two threads perform their tasks is dependent on the scheduler -- and without synchronization, you can't assume an order.

Henry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!