Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/374.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
暂停并恢复Executor服务或关闭并重新启动Java Executor服务_Java_Javafx_Executorservice_Java.util.concurrent_Threadpoolexecutor - Fatal编程技术网

暂停并恢复Executor服务或关闭并重新启动Java Executor服务

暂停并恢复Executor服务或关闭并重新启动Java Executor服务,java,javafx,executorservice,java.util.concurrent,threadpoolexecutor,Java,Javafx,Executorservice,Java.util.concurrent,Threadpoolexecutor,我正在从事一个javaFX项目,用户只需浏览几个文件,创建一个队列,单击开始上传,然后在指定路径上上传这些文件。现在,当用户单击“暂停”按钮时,上载过程必须暂停,当单击“恢复”时,该过程必须从暂停点重新启动。 因此,到目前为止,我已经通过Executors创建了一个对象(文件)队列;并提交了任务。 在我的情况下,taks类被称为项目。 现在,当用户单击pause按钮时,我调用executorService的shutdownnow()方法。 当用户单击resume时,我创建了Executorser

我正在从事一个javaFX项目,用户只需浏览几个文件,创建一个队列,单击开始上传,然后在指定路径上上传这些文件。现在,当用户单击“暂停”按钮时,上载过程必须暂停,当单击“恢复”时,该过程必须从暂停点重新启动。 因此,到目前为止,我已经通过Executors创建了一个对象(文件)队列;并提交了任务。 在我的情况下,taks类被称为项目。 现在,当用户单击pause按钮时,我调用executorService的shutdownnow()方法。 当用户单击resume时,我创建了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();

}
}
公共类UploadManagerController实现可初始化{
私有静态布尔checkRunning=true;
私有静态字符串客户端;
私有静态int currentView=0;
私有静态int-checkLastSelectedQueue=0;
私有静态时间线fiveSecondsWonder;
私有列表上传目录;
私有静态长currentSelectedRow=-1;
私人静电丝厂丝厂;
私有静态执行器服务执行器;
私有静态线程t;
私有静态LinkedHashMap客户端上传信息;
私有静态HashMapClientUploadInfoCount;
私有静态HashMapClientUploadInfoTotalSize;
私有静态HashMap客户端上传InfoTotalSizeLong;
公共静态类项扩展任务{
公共SimpleStringProperty文件名=新SimpleStringProperty();
public SimpleStringProperty fileSize=新SimpleStringProperty();
public SimpleStringProperty ETA=新SimpleStringProperty();
公共SimpleStringProperty速度=新SimpleStringProperty();
公共SimpleStringProperty状态=新SimpleStringProperty();
public SimpleDoubleProperty progress=新建SimpleDoubleProperty();
public SimpleObjectProperty clientName=new SimpleObjectProperty();
公共SimpleStringProperty路径=新SimpleStringProperty();
公共SimpleLongProperty id=新SimpleLongProperty();
公共文件;
公共长时间=0;
//上述变量的Getter和setter
@凌驾
受保护的Void调用()引发异常{
File filee=this.getFile();
FileInputStream is=新的FileInputStream(filee);
字符串mRecordingFile=“UserSpecifiedPath\\”+this.getFileName();
文件fl=新文件(mRecordingFile);
如果(fl.exists()&(this.getStatus().equals(“error”)!=true | | this.getStatus().equalsIgnoreCase(“stopped”)!=true)){
fl.renameTo(新文件(“UserSpecifiedPath\\\”+Util.getTime()+“_”+fl.getName());
}
布尔checkDelete=false;
for(字符串键:clientUploadInfo.keySet()){
if(clientUploadInfo.get(key).contains(this)){
checkDelete=true;
打破
}
}
如果(选中删除){
java.nio.channels.FileChannel fc=is.getChannel();
long totalSize=filee.length();
总长度=总尺寸/10000;
java.nio.ByteBuffer bb=java.nio.ByteBuffer.allocate(10000);
字节[]字节;
而(fc.read(bb)>0){
bb.flip();
字节=bb.array();
文件f=新文件(mRecordingFile);
try(FileOutputStream fOut=newfileoutputstream(mRecordingFile,true)){
fOut.写入(字节);
}
bb.clear();
}
}
返回null;
}
}
@凌驾
公共void初始化(URL、ResourceBundle rb){
clientUploadInfo=新建LinkedHashMap();
threadFactory=新的threadFactory(){
@凌驾
公共线程newThread(可运行的r){
t=新螺纹(r);
试一试{
t、 join();
}捕获(中断异常例外){
系统输出打印项次(“inte”);
}
t、 setDaemon(true);
返回t;
}
};
executor=Executors.newSingleThreadExecutor(threadFactory);
}
@FXML
私有void startupload(ActionEvent事件){
ObservableList temp=FXCollections.observableArrayList();
temp.addAll(clientUploadInfo.get(currentClient));
currentView=1;
checkRunning=true;
if(executor.isShutdown()){
执行者=空;
t=零;
threadFactory=null;
threadFactory=新的threadFactory(){
@凌驾
公共线程newThread(可运行的r){
t=新螺纹(r);
试一试{
t、 join();
}捕获(中断异常例外){
Logger.getLogger(UploadManagerController.class.getName()).log(Level.SEVERE,null,ex);
}
t、 setDaemon(true);
返回t;
}
};
executor=Executors.newSingleThreadExecutor(threadFactory);
用于(项目itm:temp){
如果(itm.getStatus().equalsIgnoreCase(“处于活动状态”)| itm.getStatus().equalsIgnoreCase(“排队”)| itm.getStatus().equalsIgnoreCase(“错误”)| itm.getStatus().equalsIgnoreCase(“停止”){
执行人提交(itm);
}
}
}否则{
用于(项目itm:temp){
如果(itm.getStatus().equalsIgnoreCase(“处于活动状态”)| itm.getStatus().equalsIgnoreCase(“排队”)| itm.getStatus().equalsIgnoreCase(“错误”)| itm.getStatus().equalsIgnoreCase(“停止”){
执行人。执行人(itm);
}
}
}
}
@FXML
私有void stopUpload(ActionEvent事件){
checkRunning=false;
执行者。关机现在();
}
}
对不起,我说的是英语,但让我试着解释一下, 在上面的代码中,它们是t
private boolean suspended;

synchronized void suspend() {
    suspended = true;
}

synchronized void resume() {
    suspended = false;
    notifyAll();
}

synchronized void waitWhileSuspended() {
    while (suspended) {
        wait();
    }
}