private static SourceEnd addStream(SourceEnd src) {

    SinkEnd snk = createPair();

    Replicator r2 = new Replicator();
    AsyncDrain ad = new AsyncDrain();

    r2.getSource().connect(snk);
    src.connect(r2.getSink1());
    ad.getSource1().connect(r2.getSink2());

    return ad.getSource2();
  }
  private static void createConnector(int n, int threads) {

    writers = new SinkEnd[n];
    counter = 0;

    SinkEnd snk = createPair();
    AsyncDrain ad = new AsyncDrain();
    snk.connect(ad.getSource1());

    SourceEnd src = ad.getSource2();

    for (int i = 2; i < n; i++) {
      src = addStream(src);
    }

    snk = createPair();
    snk.connect(src);

    // MUST separate in a section per thread.
    // it has 2^height writers
    //        int size = n;
    //        int rest = size % threads;
    //        int firstSection = (int) Math.floor(size/threads);
    //
    //        for (int i=0; i<firstSection; i++) {
    //            for (int thread=0; thread < threads; thread++) {
    //                int next = i+(thread*(firstSection));
    ////                System.out.println("registering: "+next);
    //                OwnerManager.register(writers[next]);
    //            }
    //        }
    //
    //        for (int i=size-rest; i<size; i++) {
    //            ContinuousWriter writer = new ContinuousWriter(1);
    ////            System.out.println("registering: "+i);
    //            OwnerManager.register(writers[i]);
    //        }

    // NEW APPROACH: first the even, then the odd writers.
    for (int i = 0; i < n; i += 2) {
      ContinuousWriter writer = new ContinuousWriter(1);
      //            System.out.println("registering: "+i);
      OwnerManager.register(writers[i]);
    }
    for (int i = 1; i < n; i += 2) {
      ContinuousWriter writer = new ContinuousWriter(1);
      //            System.out.println("registering: "+i);
      OwnerManager.register(writers[i]);
    }
  }