mirror of
https://github.com/ParkerTenBroeck/coroutines.git
synced 2026-06-07 05:08:51 -04:00
improved async runtime
This commit is contained in:
parent
d4771ef4e0
commit
7bb3547cda
2 changed files with 17 additions and 125 deletions
|
|
@ -8,82 +8,6 @@ import java.nio.ByteBuffer;
|
|||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
public class Examples {
|
||||
// public static Gen<String, Void> parse(String str){
|
||||
// for(var gen = parse(str); gen.next() instanceof Gen.Yield(var item);){
|
||||
//
|
||||
// }
|
||||
// return Gen.ret();
|
||||
// }
|
||||
|
||||
// public static Gen<String, Void> 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<String, Void> 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<String, Void> 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<String, Void> gen() {
|
||||
// Gen.yield("1");
|
||||
// Gen.yield("2");
|
||||
// Gen.yield("3");
|
||||
// Gen.yield("4");
|
||||
// return Gen.ret();
|
||||
// }
|
||||
|
||||
// public static <T> Gen<T, Void>test(T val){
|
||||
// Gen.yield(val);
|
||||
// return Gen.ret();
|
||||
// }
|
||||
|
||||
// public static Future<String> awaitTest2(int number){
|
||||
// ((Future<?>)new Delay(number)).await();
|
||||
// return Future.ret(number+"ms");
|
||||
// }
|
||||
//
|
||||
//
|
||||
// public Future<Integer> number(int value){
|
||||
// Waker.waker().wake();
|
||||
// Future.yield();
|
||||
// return Future.ret(value);
|
||||
// }
|
||||
|
||||
|
||||
public static Future<Void> test(){
|
||||
for(int i = 0; i < 1000; i ++){
|
||||
|
|
@ -108,40 +32,4 @@ public class Examples {
|
|||
}
|
||||
return Future.ret(null);
|
||||
}
|
||||
|
||||
// public Future<String> 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<Double, Void> 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();
|
||||
// }
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<Task<?>> current = new HashSet<>();
|
||||
private final Queue<Task<?>> woke = new ArrayDeque<>();
|
||||
private final AtomicInteger current = new AtomicInteger(0);
|
||||
private final ConcurrentLinkedDeque<Task<?>> 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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue