Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.crypto.Cipher;
- import java.io.FileOutputStream;
- import java.io.OutputStreamWriter;
- import java.io.PrintWriter;
- import java.util.concurrent.ThreadLocalRandom;
- import java.util.concurrent.atomic.AtomicLong;
- class Scratch {
- static ThreadLocal<byte[]> tl = new ThreadLocal<>();
- static long last;
- static final Object lastLock = new Object();
- static long count;
- static AtomicLong cycles = new AtomicLong();
- public static void main(String[] args) {
- for (int i=0; i<50; i++) {
- spawn();
- }
- while (true) {
- try {
- Thread.sleep(10000);
- } catch (Exception ignored) {}
- }
- }
- private static void spawn() {
- new Thread(()->{
- cycles.getAndIncrement();
- int lim = ThreadLocalRandom.current().nextInt(10000) + 100;
- while (lim-- > 0) {
- new Scratch().doIt();
- }
- spawn();
- }).start();
- }
- private void doIt() {
- byte [] stuff = new byte[1024];
- ThreadLocalRandom.current().nextBytes(stuff);
- tl.set(stuff);
- try {
- uselessWork();
- } catch (Exception e) {
- dumpException(e);
- } finally {
- tl.remove();
- }
- if (tl.get() != null) {
- System.out.println("Finally broke");
- System.exit(1);
- }
- long now = System.currentTimeMillis();
- synchronized (lastLock) {
- if (now - last > 10000) {
- last = now;
- System.out.println("I are OK "+(++count) + " " + cycles.get());
- }
- }
- }
- private void dumpException(Throwable e) {
- try {
- try (FileOutputStream fos = new FileOutputStream("/dev/null"); OutputStreamWriter w = new OutputStreamWriter(fos); PrintWriter pw = new PrintWriter(
- w)) {
- e.printStackTrace(pw);
- }
- } catch (Exception e2) {
- e2.printStackTrace();
- System.exit(1);
- }
- }
- private void uselessWork() throws Exception {
- Cipher c = Cipher.getInstance("AES_192/CFB/NoPadding");
- byte [] data = c.update(tl.get());
- byte [] fin = c.doFinal();
- if (fin.length == 0) { fin = data; }
- if (ThreadLocalRandom.current().nextDouble() > 0.5) {
- throw new IllegalArgumentException(toStr(fin));
- }
- }
- static private String toStr(byte[] b) {
- StringBuilder sb = new StringBuilder();
- for (byte ib : b) {
- sb.append(ib);
- }
- return sb.toString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment