Skip to content

Commit 365068e

Browse files
author
replydev
committed
Use java concurrent api to handle ProgressBarRunnable, fix some warnings
1 parent e977c85 commit 365068e

File tree

10 files changed

+27
-60
lines changed

10 files changed

+27
-60
lines changed

src/main/java/me/replydev/mcping/PingOptions.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,6 @@ public PingOptions setTimeout(int timeout) {
2222
return this;
2323
}
2424

25-
/*public PingOptions setCharset(String charset) {
26-
this.charset = charset;
27-
return this;
28-
}*/
29-
3025
String getHostname() {
3126
return this.hostname;
3227
}
@@ -39,8 +34,4 @@ public int getTimeout() {
3934
return this.timeout;
4035
}
4136

42-
/*public String getCharset() {
43-
return this.charset;
44-
}*/
45-
4637
}

src/main/java/me/replydev/mcping/net/TCPPinger.java

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ public boolean ping(InetAddress address, int count)
3232
// cycle through different ports until a working one is found
3333
int probePort = PROBE_TCP_PORTS[i % PROBE_TCP_PORTS.length];
3434
// change the first port to the requested one, if it is available
35-
/*if (i == 0 && subject.isAnyPortRequested())
36-
probePort = subject.requestedPortsIterator().next();*/
3735

3836
//long startTime = System.currentTimeMillis();
3937
try
@@ -74,10 +72,6 @@ public boolean ping(InetAddress address, int count)
7472
// host is down
7573
break;
7674
}
77-
/*else {
78-
// something unknown
79-
//LOG.log(FINER, subject.toString(), e);
80-
}*/
8175
}
8276
}
8377
finally
@@ -90,17 +84,8 @@ public boolean ping(InetAddress address, int count)
9084
}
9185

9286
//A socket is already a closeable, this method is redundant
93-
/*
94-
private static void closeQuietly(Socket socket) {
95-
if (socket != null) try {
96-
socket.close();
97-
}
98-
catch (IOException ignore) {
99-
}
100-
}
101-
*/
102-
103-
87+
88+
10489
private static void closeQuietly(Closeable closeable) {
10590
if (closeable != null) try {
10691
closeable.close();

src/main/java/me/replydev/mcping/net/WindowsPinger.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,6 @@ private boolean ping4(InetAddress address, int count) throws IOException {
4545
int numReplies = dll.IcmpSendEcho(handle, ipaddr, sendData, (short) sendDataSize, null, replyData, replyDataSize, timeout);
4646
WinIpHlpDll.IcmpEchoReply echoReply = new WinIpHlpDll.IcmpEchoReply(replyData);
4747
if (numReplies > 0 && echoReply.status == 0 && Arrays.equals(echoReply.address.bytes, ipaddr.bytes)) {
48-
/*result.addReply(echoReply.roundTripTime);
49-
result.setTTL(echoReply.options.ttl & 0xFF);*/
5048
return true;
5149
}
5250
}

src/main/java/me/replydev/mcping/rawData/ExtraDescription.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
public class ExtraDescription {
66

7+
@SuppressWarnings("MismatchedReadAndWriteOfArray")
78
@SerializedName("extra")
89
private Extra[] extra;
910

src/main/java/me/replydev/qubo/CLI.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ private static void txtRun()
8383
{
8484
try
8585
{
86-
BufferedReader reader = new BufferedReader(new FileReader(new File("ranges.txt")));
86+
BufferedReader reader = new BufferedReader(new FileReader("ranges.txt"));
8787
String s;
8888
while ((s = reader.readLine()) != null)
8989
{

src/main/java/me/replydev/qubo/InputData.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public class InputData{
2020
private boolean ping;
2121
private final String filename;
2222

23-
private Options options;
23+
private final Options options;
2424

2525
private Options buildOptions()
2626
{

src/main/java/me/replydev/qubo/gui/MainWindow.java

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,26 +25,23 @@
2525
import java.net.URISyntaxException;
2626
import java.net.URL;
2727
import java.util.Locale;
28+
import java.util.concurrent.Executors;
29+
import java.util.concurrent.ScheduledExecutorService;
30+
import java.util.concurrent.TimeUnit;
2831

2932
public class MainWindow extends JFrame {
3033

3134
/**
3235
*
3336
*/
3437
private static final long serialVersionUID = 1L;
35-
3638
public static DefaultTableModel dtm;
37-
3839
private QuboInstance quboInstance;
39-
4040
private Thread instanceThread;
4141
private InstanceRunnable instanceRunnable;
42-
43-
private Thread progressBarThread;
44-
private ProgressBarRunnable progressBarRunnable;
4542
private Point initialClick;
46-
4743
private final JFrame meMyselfAndI;
44+
private ScheduledExecutorService schedulerProgressBarService;
4845

4946
public MainWindow() {
5047
setUndecorated(true);
@@ -141,8 +138,7 @@ public void mouseDragged(MouseEvent e) {
141138
public void idle() {
142139
instanceRunnable.stop();
143140
instanceThread = null;
144-
progressBarRunnable.stop();
145-
progressBarThread = null;
141+
schedulerProgressBarService.shutdown();
146142
quboInstance = null;
147143
progressBar1.setString("100%");
148144
progressBar1.setValue(100);
@@ -171,11 +167,11 @@ private void running(InputData i) {
171167
instanceRunnable = new InstanceRunnable(quboInstance, this);
172168
instanceThread = new Thread(instanceRunnable);
173169
instanceThread.start();
174-
175-
progressBarRunnable = new ProgressBarRunnable(progressBar1, quboInstance);
176-
progressBarThread = new Thread(progressBarRunnable);
177-
progressBarThread.start();
178-
170+
schedulerProgressBarService = Executors.newScheduledThreadPool(1);
171+
schedulerProgressBarService.scheduleAtFixedRate(new ProgressBarRunnable(progressBar1, quboInstance),
172+
0,
173+
TimeUnit.SECONDS.toSeconds(1),
174+
TimeUnit.SECONDS);
179175
ipRangeTextField.setEnabled(false);
180176
portRangeTextField.setEnabled(false);
181177
threadTextField.setEnabled(false);

src/main/java/me/replydev/qubo/gui/ProgressBarRunnable.java

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,29 @@
55
import me.replydev.utils.Log;
66

77
import javax.swing.*;
8+
import java.util.concurrent.Executors;
9+
import java.util.concurrent.ScheduledExecutorService;
810

911
class ProgressBarRunnable implements Runnable {
1012

1113
private final JProgressBar progressBar;
1214
private final QuboInstance quboInstance;
13-
private boolean stop = false;
1415
public ProgressBarRunnable(JProgressBar progressBar, QuboInstance quboInstance){
1516
this.progressBar = progressBar;
1617
this.quboInstance = quboInstance;
1718
}
1819

1920
@Override
2021
public void run() {
21-
while(true){
22-
if(stop) return;
23-
int percentage = (int)quboInstance.getPercentage();
24-
progressBar.setString(percentage + "%");
25-
progressBar.setValue(percentage);
26-
if(progressBar.getValue() == 100) return;
27-
try {
28-
Thread.sleep(1000);
29-
} catch (InterruptedException e) {
30-
Log.log_to_file(e.toString(),"log.txt");
31-
}
22+
int percentage = (int)quboInstance.getPercentage();
23+
progressBar.setString(percentage + "%");
24+
progressBar.setValue(percentage);
25+
if(progressBar.getValue() == 100) return;
26+
try {
27+
Thread.sleep(1000);
28+
} catch (InterruptedException e) {
29+
Log.log_to_file(e.toString(),"log.txt");
3230
}
3331
}
3432

35-
public void stop(){
36-
stop = true;
37-
}
3833
}

src/main/java/me/replydev/versionChecker/GithubResponse.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package me.replydev.versionChecker;
22

3+
@SuppressWarnings("MismatchedReadAndWriteOfArray")
34
public class GithubResponse {
45

56
private String tag_name;

src/main/java/me/replydev/versionChecker/Version.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
public class Version implements Comparable<Version> {
44

5-
private String version;
5+
private final String version;
66
public final String get() {
77
return this.version;
88
}

0 commit comments

Comments
 (0)