Skip to content

Instantly share code, notes, and snippets.

@otrack
Created January 29, 2021 17:05
Show Gist options
  • Save otrack/077394001cc74e32410b3bf75e9479e5 to your computer and use it in GitHub Desktop.
Save otrack/077394001cc74e32410b3bf75e9479e5 to your computer and use it in GitHub Desktop.
package org.crucial.dso;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
@Command(name = "pipe")
public class Pipe {
@Option(names = "-n" )
public String name = "pipe";
private AtomicCounter counter;
private AtomicReference<String> ipport;
private static final int BARRIER = 2;
public Pipe() {}
public Pipe(String name) {
this.name = name;
this.counter = new AtomicCounter("counter-"+name);
this.ipport = new AtomicReference("ref-"+name);
}
public int wait()
{
int ret = counter.increment();
while(ret < BARRIER) {
try {
Thread.currentThread().sleep(500);
} catch (InterruptedException e) {
// ignore
}
}
return 0;
}
@Override
@Command(name = "begin")
public String begin() {
String ret = this.ipport.get();
this.wait();
}
@Override
@Command(name = "end")
public void end(@Option(names = "-1") String ipport) {
this.ipport.set(ipport);
this.wait();
}
@Override
@Command(name = "getName")
public String getName() {
return this.name;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment