Skip to content

Commit

Permalink
Fix Thread.sleep Usage
Browse files Browse the repository at this point in the history
  • Loading branch information
Konloch committed Aug 21, 2024
1 parent e138680 commit de433f6
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 25 deletions.
12 changes: 4 additions & 8 deletions src/main/java/the/bytecode/club/bytecodeviewer/api/BCV.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import the.bytecode.club.bytecodeviewer.util.DialogUtils;
import the.bytecode.club.bytecodeviewer.util.JarUtils;
import the.bytecode.club.bytecodeviewer.util.MiscUtils;
import the.bytecode.club.bytecodeviewer.util.SleepUtil;

import static the.bytecode.club.bytecodeviewer.Constants.DEV_MODE;
import static the.bytecode.club.bytecodeviewer.Constants.fs;
Expand Down Expand Up @@ -290,14 +291,9 @@ public static boolean canOverwriteFile(File file) {
*/
public static void hideFrame(JFrame frame, long milliseconds)
{
new Thread(()->{
long started = System.currentTimeMillis();
while(System.currentTimeMillis()-started <= milliseconds)
{
try {
Thread.sleep(100);
} catch (InterruptedException ignored) { }
}
new Thread(()->
{
SleepUtil.sleep(milliseconds);

frame.setVisible(false);
}, "Timed Swing Hide").start();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import the.bytecode.club.bytecodeviewer.translation.TranslatedStrings;
import the.bytecode.club.bytecodeviewer.util.JarUtils;
import the.bytecode.club.bytecodeviewer.util.MiscUtils;
import the.bytecode.club.bytecodeviewer.util.SleepUtil;

import static the.bytecode.club.bytecodeviewer.Constants.fs;
import static the.bytecode.club.bytecodeviewer.Constants.nl;
Expand Down Expand Up @@ -95,15 +96,10 @@ public byte[] compile(String contents, String fullyQualifiedName)
Process process = pb.start();
BytecodeViewer.createdProcesses.add(process);

Thread failSafe = new Thread(() -> {
long started = System.currentTimeMillis();
while (System.currentTimeMillis() - started <= 10_000) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Thread failSafe = new Thread(() ->
{
//wait 10 seconds
SleepUtil.sleep(10_000);

if (process.isAlive())
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,8 @@ public class BootCheck implements Runnable
@Override
public void run()
{
long start = System.currentTimeMillis();

//7 second failsafe
while (System.currentTimeMillis() - start < 7000)
{
try {
Thread.sleep(100);
} catch (InterruptedException ignored) { }
}
SleepUtil.sleep(7000);

//if it's failed to boot and it's not downloading attempt to load the libraries
failSafeLoadLibraries();
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/the/bytecode/club/bytecodeviewer/util/SleepUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package the.bytecode.club.bytecodeviewer.util;

/**
* @author Konloch
* @since 8/21/2024
*/
public class SleepUtil
{
public static void sleep(long ms)
{
try
{
Thread.sleep(ms);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}

0 comments on commit de433f6

Please sign in to comment.