Skip to content

Commit

Permalink
FIXED ASYNC NOT WORKING WHEN MAIN THREAD IS SLEEPING
Browse files Browse the repository at this point in the history
  • Loading branch information
slowcheet4h committed Jun 12, 2022
1 parent bd82646 commit 3387413
Show file tree
Hide file tree
Showing 28 changed files with 900 additions and 351 deletions.
30 changes: 28 additions & 2 deletions pisi/unitedmeows/yystal/YYStal.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import pisi.unitedmeows.yystal.logger.impl.YLogger;
import pisi.unitedmeows.yystal.parallel.ITaskPool;
import pisi.unitedmeows.yystal.parallel.pools.BasicTaskPool;
import pisi.unitedmeows.yystal.parallel.repeaters.Repeater;
import pisi.unitedmeows.yystal.parallel.repeaters.RepeaterPool;
import pisi.unitedmeows.yystal.sql.YSQLCommand;
import pisi.unitedmeows.yystal.ui.YUI;
import pisi.unitedmeows.yystal.ui.YWindow;
Expand All @@ -23,8 +25,7 @@

import java.io.Closeable;
import java.io.IOException;
import java.util.HashMap;
import java.util.Stack;
import java.util.*;
import java.util.function.Consumer;
import java.util.function.Supplier;

Expand All @@ -36,6 +37,9 @@ public class YYStal {
private static final Thread mainThread;
private static final HashMap<String, valuelock<?>> valueLocks;

private static final RepeaterPool repeaterPool;
private static List<IDisposable> disposables;


static {
mainThread = Thread.currentThread();
Expand All @@ -48,6 +52,11 @@ public class YYStal {
settings.put(YSettings.TASK_AWAIT_DELAY, 1L);
settings.put(YSettings.TCP_CLIENT_QUEUE_CHECK_DELAY, 1L);
settings.put(YSettings.TCP_CLIENT_WRITE_DELAY, 12L);

repeaterPool = new RepeaterPool();
disposables = new ArrayList<>();

disposables.add(repeaterPool);
}

public static void setCurrentPool(ITaskPool taskPool) {
Expand Down Expand Up @@ -99,6 +108,10 @@ public static <X> ref<X> reference(X initValue) {
return new ref<X>(initValue);
}

public static <X> ref<X> reference() {
return new ref<X>();
}

public static <F, S> Pair<F, S> pair(F first, S second) {
return new Pair<>(first, second);
}
Expand Down Expand Up @@ -150,6 +163,10 @@ public static <X extends Closeable> boolean using(X element, Consumer<X> consume
return true;
}

public static Repeater repeater(int time) {
return repeaterPool.createRepeater(time);
}

public static YLogger createLogger(Class<?> clazz) {
return new YLogger(clazz);
}
Expand Down Expand Up @@ -180,4 +197,13 @@ public static ITaskPool taskPool() {
return currentPool;
}

public static RepeaterPool repeaterPool() {
return repeaterPool;
}

public static void free() {
disposables.forEach(IDisposable::close);
disposables.clear();
}

}
3 changes: 2 additions & 1 deletion pisi/unitedmeows/yystal/clazz/event.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;

Expand All @@ -21,7 +22,7 @@ public event() {
public void fire(Object... params) {
for (Pair<delegate, Method> bound : delegateMap.values()) {
try {
bound.item2().invoke(bound.item1(), params);
bound.item2().invoke(bound.item1(), params);
} catch (Exception ex) {
ex.printStackTrace();
}
Expand Down
45 changes: 45 additions & 0 deletions pisi/unitedmeows/yystal/clazz/living.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package pisi.unitedmeows.yystal.clazz;

import pisi.unitedmeows.yystal.parallel.Async;
import pisi.unitedmeows.yystal.parallel.Promise;

import java.util.function.Supplier;

public class living<X> {

private X value;
private Promise promise;
private Supplier<X> supplier;
private final long interval;

public living(X _value, long _interval) {
interval = _interval;
value = _value;
promise = Async.async_loop_w(() -> value = supplier.get(), interval);
}

public living(long _interval) {
interval = _interval;
promise = Async.async_loop(() -> value = supplier.get(), interval);
}

public living<X> update(Supplier<X> _supplier) {
supplier = _supplier;
return this;
}

public living<X> pause() {
promise.stop();
return this;
}

public living<X> resume() {
promise = Async.async_loop_w(() -> value = supplier.get(), interval);
return this;
}


public X get() {
return value;
}
}
23 changes: 15 additions & 8 deletions pisi/unitedmeows/yystal/file/YFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class YFile {

private String path;
private File file;

public YFile(String _path) {
this(new File(_path));
}
Expand All @@ -26,63 +26,69 @@ public YFile(String _directory, String _fileName) {
this(new File(_directory, _fileName));
}

public void create() {
public YFile create() {
try {
file.createNewFile();
} catch (IOException e) {
YYStal.pop(new YexIO("Couldn't create a new file PATH: " + path));
}
return this;
}

public void writeAll(String input) {
public YFile writeAll(String input) {
try {
FileWriter myWriter = new FileWriter(file);
myWriter.write(input);
myWriter.close();
} catch (IOException ex) {
YYStal.pop(new YexIO("Error while YFile:writeAll (couldn't write to file)"));
}
return this;
}

public void writeAll(List<String> input) {
public YFile writeAll(List<String> input) {
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < input.size(); i++) {
stringBuilder.append(input.get(i));
stringBuilder.append(System.lineSeparator());
}
writeAll(stringBuilder.toString());
return this;
}

public void writeAll(String[] input) {
public YFile writeAll(String[] input) {
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < input.length; i++) {
stringBuilder.append(input[i]);
stringBuilder.append(System.lineSeparator());
}
writeAll(stringBuilder.toString());
return this;
}

public void write(List<String> input) {
public YFile write(List<String> input) {
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < input.size(); i++) {
stringBuilder.append(input.get(i));
stringBuilder.append(System.lineSeparator());
}

write(stringBuilder.toString());
return this;
}

public void write(String[] input) {
public YFile write(String[] input) {
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < input.length; i++) {
stringBuilder.append(input[i]);
stringBuilder.append(System.lineSeparator());
}

write(stringBuilder.toString());
return this;
}

public void write(String input) {
public YFile write(String input) {
try {
FileWriter myWriter = new FileWriter(file, true);

Expand All @@ -91,6 +97,7 @@ public void write(String input) {
} catch (IOException ex) {
YYStal.pop(new YexIO("Error while YFile:write (couldn't write to file)"));
}
return this;
}

public List<String> readAllLines()
Expand Down
8 changes: 8 additions & 0 deletions pisi/unitedmeows/yystal/parallel/Task.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,12 @@ public long took() {
}


public long startTime() {
return startTime;
}

@Deprecated public Task startTime(long _startTime) {
startTime = _startTime;
return this;
}
}
2 changes: 1 addition & 1 deletion pisi/unitedmeows/yystal/parallel/TaskWorker.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public TaskWorker() {
@Override
public void run() {
running = true;
while (isRunning() && YYStal.mainThread().isAlive()) {
while (isRunning()) {
currentTask = YYStal.taskPool().nextTask();
if (currentTask != null) {
busy = true;
Expand Down
Loading

0 comments on commit 3387413

Please sign in to comment.