0

In Java, I am creating 256 threads which communicate with each other using network socket. All this 256 threads run in parallel. When a thread is spawned, it tries to connect to its neighbor threads. The list of neighbors can be arbitrary. In this situation, how to ensure that all the threads create a connection with their neighbors

  1. without deadlock
  2. without a star topology (central node)

In order to form a connection between two threads, one thread has to open a ServerSocket and other thread must join it. Currently, I am using a simple algorithm :

for all edges
do
   if edge.tid > my tid
   then
      connect to edge.ip
   endif   
done

for all edges
do
   if edge.tid < my tid
   then
       accept connection from edge.ip
   endif
done

As you can see above, I am first connecting to bigger neighbors and then I am waiting for smaller neighbor to connect to me. This can result in a deadlock when the neighborhood table looks as given below :

t0 -> t3, t1 
t1 -> t2, t0
t2 -> t1
t3 -> t0

Any specific algorithm in your mind which is deadlock free ?? My algorithm uses blocking connect and accept methods in java. I have a feeling this can be done using non-blocking methods but want to know the other ideas first.

FYI, my algorithm works perfectly well for a mesh topology (at-least I think it does).

prathmesh.kallurkar
  • 5,468
  • 8
  • 39
  • 50

1 Answers1

1

If in your case deadlock means having this situation:

ti -> tj and tj -> ti

Why not include in the conditions, as a new approach in order to avoid this situation, an additional part as follow:

if edge.tid > my tid and edge is not already connected to my tie

for the first case, if ti -> tj already exist, then you don't accept to establish tj -> ti

if edge.tid < my tid and my tie is not already connected to edge

for this case 2nd case, if tj -> ti already exist, then you don't establish ti -> tj

emecas
  • 1,586
  • 3
  • 27
  • 43
  • nope. as you see, I am already doing this. ti tries to connect to tj only if ti < tj – prathmesh.kallurkar Mar 19 '13 at 09:16
  • sorry but then the part that I don't get is: where does the neighborhood table that you exposed have resulted or come from? if is not it a kind of initial condition. I mean for that case `t0 -> t3 and t0 -> t3, t1` that combination would not be acceptable same for `t1 -> t2, t0 and t2 -> t1` – emecas Mar 19 '13 at 09:31