Search in sources :

Example 6 with AbstractRunnable

use of org.spf4j.base.AbstractRunnable in project spf4j by zolyfarkas.

the class PipedStreamsBenchmark method testJdk.

private void testJdk(final String testStr, final int buffSize) throws IOException {
    final java.io.PipedOutputStream pos = new java.io.PipedOutputStream();
    final java.io.PipedInputStream pis = new java.io.PipedInputStream(pos, buffSize);
    DefaultExecutor.INSTANCE.execute(new AbstractRunnable() {

        @Override
        public void doRun() throws Exception {
            try (OutputStream os = pos) {
                final byte[] utf8 = Strings.toUtf8(testStr);
                os.write(utf8[0]);
                os.write(utf8, 1, 10);
                os.write(utf8, 11, 10);
                os.write(utf8, 21, utf8.length - 21);
            }
        }
    });
    StringBuilder sb = new StringBuilder();
    try (InputStream is = pis) {
        byte[] buffer = new byte[1024];
        int read;
        while ((read = is.read(buffer)) > 0) {
            sb.append(Strings.fromUtf8(buffer, 0, read));
        }
    }
    Assert.assertEquals(testStr, sb.toString());
}
Also used : AbstractRunnable(org.spf4j.base.AbstractRunnable) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) IOException(java.io.IOException)

Example 7 with AbstractRunnable

use of org.spf4j.base.AbstractRunnable in project spf4j by zolyfarkas.

the class TcpServerTest method testKill.

@Test(expected = java.net.SocketException.class, timeout = 10000)
public void testKill() throws IOException, InterruptedException {
    String testSite = "10.10.10.10";
    ForkJoinPool pool = new ForkJoinPool(1024);
    try (TcpServer server = new TcpServer(pool, new ProxyClientHandler(HostAndPort.fromParts(testSite, 80), null, null, 10000, 10000), 1982, 10)) {
        server.startAsync().awaitRunning();
        DefaultScheduler.INSTANCE.schedule(new AbstractRunnable(true) {

            @Override
            public void doRun() throws IOException {
                server.close();
            }
        }, 2, TimeUnit.SECONDS);
        // results in a socket exception after 5 seconds
        readfromSite("http://localhost:1982");
        Assert.fail("Should timeout");
    }
}
Also used : AbstractRunnable(org.spf4j.base.AbstractRunnable) ProxyClientHandler(org.spf4j.io.tcp.proxy.ProxyClientHandler) IOException(java.io.IOException) TcpServer(org.spf4j.io.tcp.TcpServer) ForkJoinPool(java.util.concurrent.ForkJoinPool) Test(org.junit.Test)

Example 8 with AbstractRunnable

use of org.spf4j.base.AbstractRunnable in project spf4j by zolyfarkas.

the class MonitorTest method main.

@SuppressFBWarnings("MDM_THREAD_YIELD")
public static void main(final String[] args) throws InterruptedException {
    stopped = false;
    try (ExecutionContext ctx = ExecutionContexts.start("main", 10, TimeUnit.MINUTES)) {
        List<Thread> threads = new ArrayList<Thread>(20);
        for (int i = 0; i < 20; i++) {
            Thread t;
            t = new Thread(new AbstractRunnable() {

                @Override
                public void doRun() throws InterruptedException {
                    try (ExecutionContext tctx = ExecutionContexts.start("testThread", ctx, 10, TimeUnit.MINUTES)) {
                        while (!stopped) {
                            double rnd = Math.random();
                            if (rnd < 0.33) {
                                doStuff1(rnd, 50);
                            } else if (rnd < 0.66) {
                                doStuff2(rnd);
                            } else {
                                doStuff3(rnd);
                            }
                        }
                    }
                }

                @SuppressFBWarnings("MDM_THREAD_YIELD")
                private double doStuff3(final double rnd) throws InterruptedException {
                    Thread.sleep(1);
                    if (rnd > 0.8) {
                        doStuff2(rnd);
                    }
                    return rnd * Math.pow(2, 10000);
                }

                @SuppressFBWarnings("MDM_THREAD_YIELD")
                private double doStuff2(final double prnd) throws InterruptedException {
                    double rnd = prnd;
                    Thread.sleep(1);
                    for (int j = 0; j < 10000; j++) {
                        rnd = rnd + rnd;
                    }
                    return rnd;
                }

                @SuppressFBWarnings("MDM_THREAD_YIELD")
                private void doStuff1(final double rnd, final int depth) throws InterruptedException {
                    if (depth <= 0) {
                        Thread.sleep(10);
                    } else {
                        doStuff1(rnd, depth - 1);
                    }
                }
            }, "Thread" + i);
            t.start();
            threads.add(t);
        }
        Thread.sleep(5000);
        stopped = true;
        for (Thread t : threads) {
            t.join(3000);
        }
    }
}
Also used : AbstractRunnable(org.spf4j.base.AbstractRunnable) ExecutionContext(org.spf4j.base.ExecutionContext) ArrayList(java.util.ArrayList) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 9 with AbstractRunnable

use of org.spf4j.base.AbstractRunnable in project spf4j by zolyfarkas.

the class Explorer method main.

/**
 * @param args the command line arguments
 */
public static void main(final String[] args) {
    System.setProperty("spf4j.tsdb2.lenientRead", "true");
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
     */
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(Explorer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(Explorer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(Explorer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(Explorer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    // </editor-fold>
    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new AbstractRunnable(false) {

        @Override
        @SuppressFBWarnings("PATH_TRAVERSAL_IN")
        public void doRun() throws IOException {
            File[] files = new File[args.length];
            for (int i = 0; i < args.length; i++) {
                files[i] = new File(args[i]);
            }
            new Explorer(files).setVisible(true);
        }
    });
}
Also used : AbstractRunnable(org.spf4j.base.AbstractRunnable) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) File(java.io.File)

Example 10 with AbstractRunnable

use of org.spf4j.base.AbstractRunnable in project spf4j by zolyfarkas.

the class ScalableMeasurementRecorderSource method closeOnShutdown.

private Runnable closeOnShutdown() {
    final AbstractRunnable runnable = new AbstractRunnable(true) {

        @Override
        public void doRun() {
            close();
        }
    };
    org.spf4j.base.Runtime.queueHook(0, runnable);
    return runnable;
}
Also used : AbstractRunnable(org.spf4j.base.AbstractRunnable)

Aggregations

AbstractRunnable (org.spf4j.base.AbstractRunnable)16 IOException (java.io.IOException)6 JmxExport (org.spf4j.jmx.JmxExport)4 InputStream (java.io.InputStream)3 OutputStream (java.io.OutputStream)3 MeasurementRecorder (org.spf4j.perf.MeasurementRecorder)3 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)2 Test (org.junit.Test)2 TObjectLongMap (gnu.trove.map.TObjectLongMap)1 TObjectLongHashMap (gnu.trove.map.hash.TObjectLongHashMap)1 File (java.io.File)1 UncheckedIOException (java.io.UncheckedIOException)1 InetSocketAddress (java.net.InetSocketAddress)1 ByteBuffer (java.nio.ByteBuffer)1 ClosedByInterruptException (java.nio.channels.ClosedByInterruptException)1 DatagramChannel (java.nio.channels.DatagramChannel)1 ArrayList (java.util.ArrayList)1 ForkJoinPool (java.util.concurrent.ForkJoinPool)1 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)1 BeforeClass (org.junit.BeforeClass)1