package guide;

import org.zeromq.SocketType;
import org.zeromq.ZMQ;
import org.zeromq.ZMQ.Socket;
import org.zeromq.ZContext;

/**
 * Multithreaded relay
 */
public class mtrelay
{

    private static class Step1 extends Thread
    {
        private ZContext context;

        private Step1(ZContext context)
        {
            this.context = context;
        }

        @Override
        public void run()
        {
            //  Signal downstream to step 2
            Socket xmitter = context.createSocket(SocketType.PAIR);
            xmitter.connect("inproc://step2");
            System.out.println("Step 1 ready, signaling step 2");
            xmitter.send("READY", 0);
            xmitter.close();
        }

    }

    private static class Step2 extends Thread
    {
        private ZContext context;

        private Step2(ZContext context)
        {
            this.context = context;
        }

        @Override
        public void run()
        {
            //  Bind to inproc: endpoint, then start upstream thread
            Socket receiver = context.createSocket(SocketType.PAIR);
            receiver.bind("inproc://step2");
            Thread step1 = new Step1(context);
            step1.start();

            //  Wait for signal
            receiver.recv(0);
            receiver.close();

            //  Connect to step3 and tell it we're ready
            Socket xmitter = context.createSocket(SocketType.PAIR);
            xmitter.connect("inproc://step3");
            xmitter.send("READY", 0);

            xmitter.close();
        }

    }

    public static void main(String[] args)
    {
        try (ZContext context = new ZContext()) {
            //  Bind to inproc: endpoint, then start upstream thread
            Socket receiver = context.createSocket(SocketType.PAIR);
            receiver.bind("inproc://step3");

            //  Step 2 relays the signal to step 3
            Thread step2 = new Step2(context);
            step2.start();

            //  Wait for signal
            receiver.recv(0);
            receiver.close();

            System.out.println("Test successful!");
        }
    }
}

@@ -58,18 +58,23 @@
 zeromq.Z
-MQ
+Context
 ;%0Aimport
@@ -100,36 +100,8 @@
 ket;
-%0Aimport org.zeromq.ZContext;
 %0A%0A/*
@@ -1125,82 +1125,8 @@
 2%22);
-%0A            Thread step1 = new Step1(context);%0A            step1.start();
 %0A%0A  
@@ -1384,24 +1384,90 @@
 ://step3%22);%0A
+            System.out.println(%22Step 2 ready, signaling step 3%22);%0A
             
@@ -1492,17 +1492,16 @@
 Y%22, 0);%0A
-%0A
         
@@ -1548,21 +1548,22 @@
    p
-ublic
+rivate
  static 
 void
@@ -1562,269 +1562,809 @@
 tic 
-void main(String%5B%5D args)%0A    %7B%0A        try (ZContext context = new ZContext()) %7B%0A            //  Bind to inproc: endpoint, then start upstream thread%0A            Socket receiver = context.createSocket(SocketType.PAIR);%0A            receiver.bind(%22inproc://step3%22
+class Step3 extends Thread%0A    %7B%0A        private ZContext context;%0A%0A        private Step3(ZContext context)%0A        %7B%0A            this.context = context;%0A        %7D%0A%0A        @Override%0A        public void run()%0A        %7B%0A            //  Bind to inproc: endpoint, then start upstream thread%0A            Socket receiver = context.createSocket(SocketType.PAIR);%0A            receiver.bind(%22inproc://step3%22);%0A%0A            //  Wait for signal%0A            receiver.recv(0);%0A            receiver.close();%0A%0A            System.out.println(%22Step 3 ready%22);%0A        %7D%0A%0A    %7D%0A%0A    public static void main(String%5B%5D args) throws InterruptedException%0A    %7B%0A        try (ZContext context = new ZContext()) %7B%0A            //  Step 1 signals to step 2%0A            Thread step1 = new Step1(context);%0A            step1.start(
 );%0A%0A
@@ -2404,16 +2404,28 @@
  signal 
+from step 1 
 to step 
@@ -2509,36 +2509,44 @@
             //  
-W
+Step 3 w
 ait
+s
  for signal%0A    
@@ -2532,32 +2532,44 @@
 waits for signal
+ from step 2
 %0A            rec
@@ -2565,34 +2565,79 @@
         
-receiver.recv(0
+Thread step3 = new Step3(context);%0A            step3.start(
 );%0A
+%0A
         
@@ -2636,38 +2636,86 @@
             
-receiver.close
+step1.join();%0A            step2.join();%0A            step3.join
 ();%0A%0A       

mtrelay Step3 is missing on first glance