gpt4 book ai didi

java - 将进程传递给子类

转载 作者:行者123 更新时间:2023-12-01 12:07:15 24 4
gpt4 key购买 nike

我想将一个进程传递给子类,这样我可能会杀死它,但我不知道如何传递该进程。我不确定如何存储它,以便我可以将其返回到表单并能够调用子类方法来终止它。这是我的类(class)

package my.mashformcnts;
import java.io.IOException;
import java.util.Scanner;
import java.util.regex.Pattern;


/**
*
* @author brett
*/
public class MashRocks {

public static Process startThread(MashFormCnts mashFormCnts) throws IOException {
ProcessBuilder pb = new ProcessBuilder("ffmpeg", "-i", "C:\\Users\\brett\\Documents\\Telegraph_Road.mp4", "C:\\Users\\brett\\Documents\\out.mp4");

//Here is where i would like to name and store the Process


final Process p = pb.start();
// create a new thread to get progress from ffmpeg command , override
// it's run method, and start it!
Thread t = new Thread() {
@Override
public void run() {
Scanner sc = new Scanner(p.getErrorStream());
// Find duration
Pattern durPattern = Pattern.compile("(?<=Duration: )[^,]*");
String dur = sc.findWithinHorizon(durPattern, 0);
if (dur == null) {
throw new RuntimeException("Could not parse duration.");
}
String[] hms = dur.split(":");
double totalSecs = Integer.parseInt(hms[0]) * 3600 + Integer.parseInt(hms[1]) * 60 + Double.parseDouble(hms[2]);
System.out.println("Total duration: " + totalSecs + " seconds.");
// Find time as long as possible.
Pattern timePattern = Pattern.compile("(?<=time=)[\\d:.]*");
String match;
String[] matchSplit;
//MashForm pgbar = new MashForm();
while (null != (match = sc.findWithinHorizon(timePattern, 0))) {
matchSplit = match.split(":");
double progress = (Integer.parseInt(matchSplit[0]) * 3600 + Integer.parseInt(matchSplit[1]) * 60 + Double.parseDouble(matchSplit[2])) / totalSecs;
int prog = (int) (progress * 100);
mashFormCunts.setbar(prog);
}
}
};
t.start();
return p;
}
public synchronized static void stop(Thread t) throws IOException{
Runtime.getRuntime().exec("taskkill /F /IM ffmpeg.exe");
t = null;
//t.interrupt();



}
}
class killMash extends MashRocks{
public static void Kfpeg(Process p){

p.destroyForcibly();
}
}

这些是我的类(class)。我是个新人。

接下来,表单上有事件监听器,所以当我单击它时,我想使用Thread t终止 ffmpeg 进程:

  private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
// TODO add your handling code here:
Thread n = Thread.currentThread();
System.out.print(n);
try {
//MashRocks.stop(n);
//This isnt working but i think its closer
killMash.Kfpeg(MashRocks.startThread(this));
//Not Sure what to do here
//here is where i want to pass the process sorry for the typo
killMash.kfpeg(p);
} catch (IOException ex) {
Logger.getLogger(MashFormCunts.class.getName()).log(Level.SEVERE, null, ex);
}

}

任何帮助都是很棒的欢呼

最佳答案

我不知道为什么你想让所有东西都是静态的,但如果你需要,一种可能性是将进程存储为类变量:

public class MashRocks {

protected static Process process = null;

public static Process startThread(MashFormCnts mashFormCnts) throws IOException {
ProcessBuilder pb = new ProcessBuilder("ffmpeg", "-i", "C:\\Users\\brett\\Documents\\Telegraph_Road.mp4", "C:\\Users\\brett\\Documents\\out.mp4");

final Process p = pb.start();
MashRocks.process = p;
....
}
}

您的停止方法可能是:

public synchronized static void stop() throws IOException{
if(MashRocks.process != null)
{
MashRocks.process.destroy();
}
}

还有你的子类:

class KillMash extends MashRocks{
public static void Kfpeg() throws IOException{
KillMash.stop();
}
}

关于java - 将进程传递给子类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27500731/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com