diff --git a/src/Examples.java b/src/Examples.java index a206c6a..ddd8c3e 100644 --- a/src/Examples.java +++ b/src/Examples.java @@ -8,82 +8,6 @@ import java.nio.ByteBuffer; import java.nio.charset.StandardCharsets; public class Examples { - // public static Gen parse(String str){ -// for(var gen = parse(str); gen.next() instanceof Gen.Yield(var item);){ -// -// } -// return Gen.ret(); -// } - -// public static Gen parse(String str){ -// { -// String meow = "10"; -// meow += "11"; -// Gen.yield(meow); -// Gen.yield(meow); -// Gen.yield(meow); -// Gen.yield(meow); -// } -// for(var split : str.split(" ")){ -// Gen.yield(split); -// } -// { -// var str2 = str; -// while(str2.length()>10){ -// var len = str2.length(); -// Gen.yield(len+" length"); -// str2 = str2.substring(1); -// } -// } -// -// while(str.length()>10){ -// var len = str.length(); -// Gen.yield(len+" length"); -// str = str.substring(1); -// } -// return Gen.ret(); -// } - -// public static Gen gen(int times, double mul) { -// mul -= 0.5; -// for (int i = 0; i < times; i ++) { -// Gen.yield("iteration number: " + i*mul); -// } -// return Gen.ret(); -// } -// public static Gen gen(int times, double mul) { -// mul -= 0.5; -// for (int i = 0; i < times; i++) { -// Gen.yield("iteration number: " + i*mul); -// } -// return Gen.ret(); -// } - - // public static Gen gen() { -// Gen.yield("1"); -// Gen.yield("2"); -// Gen.yield("3"); -// Gen.yield("4"); -// return Gen.ret(); -// } - -// public static Gentest(T val){ -// Gen.yield(val); -// return Gen.ret(); -// } - -// public static Future awaitTest2(int number){ -// ((Future)new Delay(number)).await(); -// return Future.ret(number+"ms"); -// } -// -// -// public Future number(int value){ -// Waker.waker().wake(); -// Future.yield(); -// return Future.ret(value); -// } - public static Future test(){ for(int i = 0; i < 1000; i ++){ @@ -108,40 +32,4 @@ public class Examples { } return Future.ret(null); } - -// public Future closing(int number){ -// try(var m = new Meow()){ -// var result = awaitTest2(number).await(); -// return Future.ret(result); -// } -// } - - public static class Meow implements AutoCloseable{ - { - System.out.println("OPen"); - } - - @Override - public void close() { - System.out.println("Close"); - } - } - -// public static Gen test(double[] nyas){ -// -// var test = 1+switch(nyas[0]){ -// case 1.0 -> { -// Gen.yield(11); -// yield 2; -// } -// default -> { -// Gen.yield(12); -// yield 4; -// } -// }; -// for(var d : nyas){ -// Gen.yield(d); -// } -// return Gen.ret(); -// } } diff --git a/src/async_example/Jokio.java b/src/async_example/Jokio.java index 341f3f7..8168642 100644 --- a/src/async_example/Jokio.java +++ b/src/async_example/Jokio.java @@ -6,6 +6,8 @@ import generator.future.Waker; import java.util.ArrayDeque; import java.util.HashSet; import java.util.Queue; +import java.util.concurrent.ConcurrentLinkedDeque; +import java.util.concurrent.atomic.AtomicInteger; public class Jokio implements Runnable{ @@ -18,8 +20,8 @@ public class Jokio implements Runnable{ @Override public void wake() { + woke.add(this); synchronized (Jokio.this){ - woke.add(this); Jokio.this.notifyAll(); } } @@ -42,34 +44,36 @@ public class Jokio implements Runnable{ return ((Task)waker).runtime(); } - private final HashSet> current = new HashSet<>(); - private final Queue> woke = new ArrayDeque<>(); + private final AtomicInteger current = new AtomicInteger(0); + private final ConcurrentLinkedDeque> woke = new ConcurrentLinkedDeque<>(); public void blocking(Future fut){ spawn(fut).run(); } - public synchronized Jokio spawn(Future future){ + public Jokio spawn(Future future){ var task = new Task<>(future); - current.add(task); + current.getAndIncrement(); woke.add(task); return this; } @Override - public synchronized void run(){ - while(!current.isEmpty()) { - while(woke.isEmpty()) { - try { - this.wait(); - } catch (InterruptedException e) { - throw new RuntimeException(e); + public void run(){ + while(current.get() > 0) { + synchronized (this) { + while (woke.isEmpty()) { + try { + this.wait(); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } } } var task = woke.poll(); var result = task.future.poll(task); if(result!=Future.Pending.INSTANCE) { - current.remove(task); + current.getAndDecrement(); System.out.println(result); } }