我正在一个javaFX项目上工作,在该项目中,用户只需浏览几个文件,创建一个文件队列,单击“开始上传”,然后在指定路径上上传这些文件的开始。现在,当用户单击“暂停”按钮时,必须暂停上载过程,而在单击“恢复”时,必须从暂停点重新开始该过程。因此,到目前为止,我已经通过Executors.newSingleThreadExecutor()创建了一个对象(文件)队列。并提交了任务。在我看来,taks类被称为Item。现在,当用户单击暂停按钮时,我调用executorService的shutdownnow()方法。当用户单击简历时,我将创建一个Executorservice的全新实例。但是,当我通过executor.execute方法提交作业时,它将跳过当前任务并继续执行下一个任务,依此类推。因此,我需要从最后执行的任务开始。
public class UploadManagerController implements Initializable {
private static boolean checkRunning = true;
private static String currentClient;
private static int currentView = 0;
private static int checkLastSelectedQueue = 0;
private static Timeline fiveSecondsWonder;
private List<File> uploadDir;
private static long currentSelectedRow = -1;
private static ThreadFactory threadFactory;
private static ExecutorService executor;
private static Thread t;
private static LinkedHashMap<String, ObservableList> clientUploadInfo;
private static HashMap<String, Long> clientUploadInfoCount;
private static HashMap<String, String> clientUploadInfoTotalSize;
private static HashMap<String, Double> clientUploadInfoTotalSizeLong;
public static class Item extends Task<Void> {
public SimpleStringProperty fileName = new SimpleStringProperty();
public SimpleStringProperty fileSize = new SimpleStringProperty();
public SimpleStringProperty ETA = new SimpleStringProperty();
public SimpleStringProperty speed = new SimpleStringProperty();
public SimpleStringProperty status = new SimpleStringProperty();
public SimpleDoubleProperty progress = new SimpleDoubleProperty();
public SimpleObjectProperty clientName = new SimpleObjectProperty();
public SimpleStringProperty path = new SimpleStringProperty();
public SimpleLongProperty id = new SimpleLongProperty();
public File file;
public long time = 0;
// Getter and setter of above variables
@Override
protected Void call() throws Exception {
File filee = this.getFile();
FileInputStream is = new FileInputStream(filee);
String mRecordingFile = "UserSpecifiedPath\\" + this.getFileName();
File fl = new File(mRecordingFile);
if (fl.exists() && (this.getStatus().equals("error") != true || this.getStatus().equalsIgnoreCase("stopped") != true)) {
fl.renameTo(new File("UserSpecifiedPath\\" + Util.getTime() + "_" + fl.getName()));
}
boolean checkDelete = false;
for (String key : clientUploadInfo.keySet()) {
if (clientUploadInfo.get(key).contains(this)) {
checkDelete = true;
break;
}
}
if ( checkDelete) {
java.nio.channels.FileChannel fc = is.getChannel();
long totalSize = filee.length();
long total = totalSize / 10000;
java.nio.ByteBuffer bb = java.nio.ByteBuffer.allocate(10000);
byte[] bytes;
while (fc.read(bb) > 0) {
bb.flip();
bytes = bb.array();
File f = new File(mRecordingFile);
try (FileOutputStream fOut = new FileOutputStream(mRecordingFile, true)) {
fOut.write(bytes);
}
bb.clear();
}
}
return null;
}
}
@Override
public void initialize(URL url, ResourceBundle rb) {
clientUploadInfo = new LinkedHashMap<>();
threadFactory = new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
t = new Thread(r);
try {
t.join();
} catch (InterruptedException ex) {
System.out.println("inte");
}
t.setDaemon(true);
return t;
}
};
executor = Executors.newSingleThreadExecutor(threadFactory);
}
@FXML
private void startupload(ActionEvent event) {
ObservableList<Item> temp = FXCollections.observableArrayList();
temp.addAll(clientUploadInfo.get(currentClient));
currentView = 1;
checkRunning = true;
if (executor.isShutdown()) {
executor = null;
t=null;
threadFactory = null;
threadFactory = new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
t = new Thread(r);
try {
t.join();
} catch (InterruptedException ex) {
Logger.getLogger(UploadManagerController.class.getName()).log(Level.SEVERE, null, ex);
}
t.setDaemon(true);
return t;
}
};
executor = Executors.newSingleThreadExecutor(threadFactory);
for (Item itm : temp) {
if (itm.getStatus().equalsIgnoreCase("In Active") || itm.getStatus().equalsIgnoreCase("queued") || itm.getStatus().equalsIgnoreCase("error") || itm.getStatus().equalsIgnoreCase("stopped")) {
executor.submit(itm);
}
}
} else {
for (Item itm : temp) {
if (itm.getStatus().equalsIgnoreCase("In Active") || itm.getStatus().equalsIgnoreCase("queued") || itm.getStatus().equalsIgnoreCase("error") || itm.getStatus().equalsIgnoreCase("stopped")) {
executor.execute(itm);
}
}
}
}
@FXML
private void stopUpload(ActionEvent event) {
checkRunning = false;
executor.shutdownNow();
}
}
抱歉,我的英语很抱歉,但是让我尝试解释一下,在上面的代码中,它们是STARTUPLOAD和STOPUPLOAD这两种方法,单击javafx按钮时会调用这两种方法,因此,当我第一次单击startupload按钮时,它将开始在clientUploadInfo.get(KEY)hasMap,我单击添加文件按钮时会保持它,我没有提到使代码更短的代码,Item类包含所有详细信息。在单击“停止上载”按钮时,调用stopUpload方法,在再次单击“开始上载”按钮时,调用startUpload方法,但要恢复上载过程,在我的情况下,请记住它会继续上载,但从下一项开始,它将保留当前项就像单击“停止”按钮时一样 任何帮助,将不胜感激 。提前致谢。
尝试覆盖Task <?>类,要从同一线程暂停并重新启动,您需要使用执行程序和ThreadFactory获取该线程的实例,重新初始化执行程序将始终跳过当前线程,因为当前线程仍在内存中并且具有链接到它,即使您输入null。因此,我建议您在完成当前线程后立即尝试暂停它,而是显示弹出窗口,以向用户传达无法取消当前进程的消息,否则尝试覆盖您的Task和ThreadFactory并获取当前线程的ID并重新初始化仅该线程。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句