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;
|
import java.nio.charset.StandardCharsets;
|
||||||
|
|
||||||
public class Examples {
|
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(){
|
public static Future<Void> test(){
|
||||||
for(int i = 0; i < 1000; i ++){
|
for(int i = 0; i < 1000; i ++){
|
||||||
|
|
@ -108,40 +32,4 @@ public class Examples {
|
||||||
}
|
}
|
||||||
return Future.ret(null);
|
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.ArrayDeque;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Queue;
|
import java.util.Queue;
|
||||||
|
import java.util.concurrent.ConcurrentLinkedDeque;
|
||||||
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
|
||||||
public class Jokio implements Runnable{
|
public class Jokio implements Runnable{
|
||||||
|
|
||||||
|
|
@ -18,8 +20,8 @@ public class Jokio implements Runnable{
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void wake() {
|
public void wake() {
|
||||||
|
woke.add(this);
|
||||||
synchronized (Jokio.this){
|
synchronized (Jokio.this){
|
||||||
woke.add(this);
|
|
||||||
Jokio.this.notifyAll();
|
Jokio.this.notifyAll();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -42,34 +44,36 @@ public class Jokio implements Runnable{
|
||||||
return ((Task<?>)waker).runtime();
|
return ((Task<?>)waker).runtime();
|
||||||
}
|
}
|
||||||
|
|
||||||
private final HashSet<Task<?>> current = new HashSet<>();
|
private final AtomicInteger current = new AtomicInteger(0);
|
||||||
private final Queue<Task<?>> woke = new ArrayDeque<>();
|
private final ConcurrentLinkedDeque<Task<?>> woke = new ConcurrentLinkedDeque<>();
|
||||||
|
|
||||||
public void blocking(Future<?> fut){
|
public void blocking(Future<?> fut){
|
||||||
spawn(fut).run();
|
spawn(fut).run();
|
||||||
}
|
}
|
||||||
|
|
||||||
public synchronized Jokio spawn(Future<?> future){
|
public Jokio spawn(Future<?> future){
|
||||||
var task = new Task<>(future);
|
var task = new Task<>(future);
|
||||||
current.add(task);
|
current.getAndIncrement();
|
||||||
woke.add(task);
|
woke.add(task);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized void run(){
|
public void run(){
|
||||||
while(!current.isEmpty()) {
|
while(current.get() > 0) {
|
||||||
while(woke.isEmpty()) {
|
synchronized (this) {
|
||||||
try {
|
while (woke.isEmpty()) {
|
||||||
this.wait();
|
try {
|
||||||
} catch (InterruptedException e) {
|
this.wait();
|
||||||
throw new RuntimeException(e);
|
} catch (InterruptedException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
var task = woke.poll();
|
var task = woke.poll();
|
||||||
var result = task.future.poll(task);
|
var result = task.future.poll(task);
|
||||||
if(result!=Future.Pending.INSTANCE) {
|
if(result!=Future.Pending.INSTANCE) {
|
||||||
current.remove(task);
|
current.getAndDecrement();
|
||||||
System.out.println(result);
|
System.out.println(result);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue