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());
}
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");
}
}
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);
}
}
}
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);
}
});
}
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;
}
Aggregations