mirror of
https://github.com/ParkerTenBroeck/coroutines.git
synced 2026-06-07 05:08:51 -04:00
so many small issues all at once
This commit is contained in:
parent
21412f4670
commit
b0d6737b07
8 changed files with 167 additions and 57 deletions
52
src/async_example/Delay.java
Normal file
52
src/async_example/Delay.java
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
package async_example;
|
||||
|
||||
import generator.future.Future;
|
||||
import generator.future.Waker;
|
||||
|
||||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
|
||||
public class Delay implements Future<String> {
|
||||
private final static Timer timer;
|
||||
private TimerTask task;
|
||||
|
||||
static {
|
||||
timer = new Timer(true);
|
||||
}
|
||||
|
||||
private int delay;
|
||||
private boolean ready;
|
||||
|
||||
public Delay(int ms) {
|
||||
if (ms < 0) throw new IllegalArgumentException("async_example.Delay cannot be negative");
|
||||
delay = ms;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cancel() {
|
||||
if (task != null) task.cancel();
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized Object poll(Waker waker) {
|
||||
if (delay == 0) {
|
||||
ready = true;
|
||||
delay = -1;
|
||||
return null;
|
||||
}
|
||||
if (delay != -1) {
|
||||
task = new TimerTask() {
|
||||
@Override
|
||||
public void run() {
|
||||
ready = true;
|
||||
waker.wake();
|
||||
}
|
||||
};
|
||||
timer.schedule(task, delay);
|
||||
delay = -1;
|
||||
}
|
||||
|
||||
if (ready) return null;
|
||||
return Pending.INSTANCE;
|
||||
}
|
||||
}
|
||||
73
src/async_example/Jokio.java
Normal file
73
src/async_example/Jokio.java
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
package async_example;
|
||||
|
||||
import generator.future.Future;
|
||||
import generator.future.Waker;
|
||||
|
||||
import java.util.ArrayDeque;
|
||||
import java.util.HashSet;
|
||||
import java.util.Queue;
|
||||
|
||||
public class Jokio implements Runnable{
|
||||
|
||||
private class Task<T> implements Waker{
|
||||
public final Future<T> future;
|
||||
|
||||
private Task(Future<T> future) {
|
||||
this.future = future;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void wake() {
|
||||
synchronized (Jokio.this){
|
||||
woke.add(this);
|
||||
Jokio.this.notifyAll();
|
||||
}
|
||||
}
|
||||
|
||||
public Jokio runtime(){
|
||||
return Jokio.this;
|
||||
}
|
||||
}
|
||||
|
||||
public static Future<Jokio> runtime(){
|
||||
return new Future<>() {
|
||||
@Override
|
||||
public Jokio poll(Waker waker) {
|
||||
return ((Task<?>)waker).runtime();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private final HashSet<Task<?>> current = new HashSet<>();
|
||||
private final Queue<Task<?>> woke = new ArrayDeque<>();
|
||||
|
||||
public void blocking(Future<?> fut){
|
||||
spawn(fut).run();
|
||||
}
|
||||
|
||||
public synchronized Jokio spawn(Future<?> future){
|
||||
var task = new Task<>(future);
|
||||
current.add(task);
|
||||
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);
|
||||
}
|
||||
}
|
||||
var task = woke.poll();
|
||||
var result = task.future.poll(task);
|
||||
if(result!=Future.Pending.INSTANCE) {
|
||||
current.remove(task);
|
||||
System.out.println(result);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue