Search in sources :

Example 6 with Thread_

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);
        }
    };
}
Also used : DblSink(suite.primitive.DblPrimitives.DblSink) DblSource(suite.primitive.DblPrimitives.DblSource) Dbl_Obj(suite.primitive.DblPrimitives.Dbl_Obj) Iterator(java.util.Iterator) LogUtil(suite.os.LogUtil) Source2(suite.util.FunUtil2.Source2) Source(suite.util.FunUtil.Source) NullableSyncQueue(suite.util.NullableSyncQueue) Thread_(suite.util.Thread_) Fun(suite.util.FunUtil.Fun) DblObjSource(suite.primitive.DblPrimitives.DblObjSource) Sink(suite.util.FunUtil.Sink) Collections(java.util.Collections) Fail(suite.util.Fail) DblTest(suite.primitive.DblPrimitives.DblTest) DblObjPair(suite.primitive.adt.pair.DblObjPair) NullableSyncQueue(suite.util.NullableSyncQueue) DblSink(suite.primitive.DblPrimitives.DblSink)

Example 7 with Thread_

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);
        }
    };
}
Also used : Iterator(java.util.Iterator) LogUtil(suite.os.LogUtil) Source2(suite.util.FunUtil2.Source2) FltSink(suite.primitive.FltPrimitives.FltSink) Source(suite.util.FunUtil.Source) NullableSyncQueue(suite.util.NullableSyncQueue) Thread_(suite.util.Thread_) Fun(suite.util.FunUtil.Fun) Flt_Obj(suite.primitive.FltPrimitives.Flt_Obj) FltObjPair(suite.primitive.adt.pair.FltObjPair) FltObjSource(suite.primitive.FltPrimitives.FltObjSource) Sink(suite.util.FunUtil.Sink) FltTest(suite.primitive.FltPrimitives.FltTest) Collections(java.util.Collections) FltSource(suite.primitive.FltPrimitives.FltSource) Fail(suite.util.Fail) NullableSyncQueue(suite.util.NullableSyncQueue) FltSink(suite.primitive.FltPrimitives.FltSink)

Example 8 with Thread_

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();
}
Also used : BooMutable(suite.primitive.BooMutable) MutexLock(suite.concurrent.Mutex.MutexLock) List(java.util.List) Read(suite.streamlet.Read) DeadlockException(suite.concurrent.Concurrent.DeadlockException) BooMutable(suite.primitive.BooMutable) Assert.assertFalse(org.junit.Assert.assertFalse) Assert.assertTrue(org.junit.Assert.assertTrue) Thread_(suite.util.Thread_) Test(org.junit.Test) DeadlockException(suite.concurrent.Concurrent.DeadlockException)

Example 9 with Thread_

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;
}
Also used : Color(java.awt.Color) Friends.min(suite.util.Friends.min) LogUtil(suite.os.LogUtil) BufferedImage(java.awt.image.BufferedImage) Constants(suite.Constants) R3(suite.math.R3) IntInt_Obj(suite.primitive.IntInt_Obj) Thread_(suite.util.Thread_) Friends.max(suite.util.Friends.max) List(java.util.List) Floats_(suite.primitive.Floats_) JLabel(javax.swing.JLabel) ImageIcon(javax.swing.ImageIcon) Ints_(suite.primitive.Ints_) BorderLayout(java.awt.BorderLayout) JFrame(javax.swing.JFrame) BiFun(suite.util.FunUtil2.BiFun) R3(suite.math.R3) Color(java.awt.Color) BufferedImage(java.awt.image.BufferedImage)

Example 10 with Thread_

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);
        }
    };
}
Also used : ChrTest(suite.primitive.ChrPrimitives.ChrTest) Chr_Obj(suite.primitive.ChrPrimitives.Chr_Obj) ChrSink(suite.primitive.ChrPrimitives.ChrSink) Iterator(java.util.Iterator) LogUtil(suite.os.LogUtil) ChrObjPair(suite.primitive.adt.pair.ChrObjPair) Source2(suite.util.FunUtil2.Source2) Source(suite.util.FunUtil.Source) NullableSyncQueue(suite.util.NullableSyncQueue) Thread_(suite.util.Thread_) Fun(suite.util.FunUtil.Fun) ChrObjSource(suite.primitive.ChrPrimitives.ChrObjSource) Sink(suite.util.FunUtil.Sink) Collections(java.util.Collections) Fail(suite.util.Fail) ChrSource(suite.primitive.ChrPrimitives.ChrSource) NullableSyncQueue(suite.util.NullableSyncQueue) ChrSink(suite.primitive.ChrPrimitives.ChrSink)

Aggregations

Thread_ (suite.util.Thread_)12 LogUtil (suite.os.LogUtil)7 Source (suite.util.FunUtil.Source)6 Collections (java.util.Collections)5 Iterator (java.util.Iterator)5 List (java.util.List)5 Fail (suite.util.Fail)5 Fun (suite.util.FunUtil.Fun)5 Sink (suite.util.FunUtil.Sink)5 Source2 (suite.util.FunUtil2.Source2)5 NullableSyncQueue (suite.util.NullableSyncQueue)5 IOException (java.io.IOException)3 Test (org.junit.Test)3 InputStream (java.io.InputStream)2 OutputStream (java.io.OutputStream)2 Socket (java.net.Socket)2 ArrayList (java.util.ArrayList)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Constants (suite.Constants)2 Copy (suite.util.Copy)2