use of suite.util.Thread_ in project suite by stupidsing.
the class DblFunUtil method suck.
/**
* Sucks data from a sink and produce into a source.
*/
public static DblSource suck(Sink<DblSink> fun) {
NullableSyncQueue<Double> queue = new NullableSyncQueue<>();
DblSink enqueue = c -> enqueue(queue, c);
Thread thread = Thread_.startThread(() -> {
try {
fun.sink(enqueue);
} finally {
enqueue(queue, EMPTYVALUE);
}
});
return () -> {
try {
return queue.take();
} catch (InterruptedException ex) {
thread.interrupt();
return Fail.t(ex);
}
};
}
use of suite.util.Thread_ in project suite by stupidsing.
the class FltFunUtil method suck.
/**
* Sucks data from a sink and produce into a source.
*/
public static FltSource suck(Sink<FltSink> fun) {
NullableSyncQueue<Float> queue = new NullableSyncQueue<>();
FltSink enqueue = c -> enqueue(queue, c);
Thread thread = Thread_.startThread(() -> {
try {
fun.sink(enqueue);
} finally {
enqueue(queue, EMPTYVALUE);
}
});
return () -> {
try {
return queue.take();
} catch (InterruptedException ex) {
thread.interrupt();
return Fail.t(ex);
}
};
}
use of suite.util.Thread_ in project suite by stupidsing.
the class MutexTest method isDeadlock.
private boolean isDeadlock(MutexTestRunnable... mtrs) throws InterruptedException {
BooMutable result = BooMutable.false_();
List<Thread> threads = //
Read.from(//
mtrs).map(mtr -> Thread_.newThread(() -> {
try {
mtr.run();
} catch (DeadlockException ex1) {
result.setTrue();
}
})).toList();
Thread_.startJoin(threads);
return result.isTrue();
}
use of suite.util.Thread_ in project suite by stupidsing.
the class Render method renderPixels.
public Image renderPixels(int width, int height, IntInt_Obj<R3> f) {
int nThreads = Constants.nThreads;
int[] txs = Ints_.toArray(nThreads + 1, i -> width * i / nThreads);
R3[][] pixels = new R3[width][height];
List<Thread> threads = //
Ints_.range(//
nThreads).map(t -> Thread_.newThread(() -> {
for (int x = txs[t]; x < txs[t + 1]; x++) for (int y = 0; y < height; y++) pixels[x][y] = f.apply(x, y);
})).toList();
Thread_.startJoin(threads);
Image image = new Image(width, height, BufferedImage.TYPE_INT_RGB);
for (int x = 0; x < width; x++) for (int y = 0; y < height; y++) {
R3 pixel = limit(pixels[x][y]);
image.setRGB(x, y, new Color(pixel.x, pixel.y, pixel.z).getRGB());
}
return image;
}
use of suite.util.Thread_ in project suite by stupidsing.
the class ChrFunUtil method suck.
/**
* Sucks data from a sink and produce into a source.
*/
public static ChrSource suck(Sink<ChrSink> fun) {
NullableSyncQueue<Character> queue = new NullableSyncQueue<>();
ChrSink enqueue = c -> enqueue(queue, c);
Thread thread = Thread_.startThread(() -> {
try {
fun.sink(enqueue);
} finally {
enqueue(queue, EMPTYVALUE);
}
});
return () -> {
try {
return queue.take();
} catch (InterruptedException ex) {
thread.interrupt();
return Fail.t(ex);
}
};
}
Aggregations