在运行while循环时终止Java进程
我有一个JFramePanel,其中有两个按钮运行此代码:在运行while循环时终止Java进程,java,Java,我有一个JFramePanel,其中有两个按钮运行此代码: public void runProc (){ while(true){ System.out.println("Running..."); PrintWriter outputStream = null; try { outputStream = new PrintWriter (FILENAME);
public void runProc (){
while(true){
System.out.println("Running...");
PrintWriter outputStream = null;
try {
outputStream = new PrintWriter (FILENAME);
}
catch (FileNotFoundException e) {
try {
Process failedToWriteFile = Runtime.getRuntime().exec(DIALOGBOX);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
e.printStackTrace();
}
outputStream.println("hello");
outputStream.close();
System.out.println("");
try {
TimeUnit.SECONDS.sleep(2);
}
catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Path d= Paths.get(FILENAME);
try {
Files.delete(d);
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
TimeUnit.SECONDS.sleep(3);
}
catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void killProc(){
System.exit(0);
}
当我按Start时,代码运行,但当我按Stop时,代码不会停止
开始按钮仍被单击,停止按钮无效
我做错了什么?只有一个线程处理GUI操作,比如单击按钮。虽然此线程尚未完成其工作,但GUI已冻结,不会对其他输入做出反应
如果我想让一个按钮启动一个较长的任务,那么它应该在一个额外的线程(如SwingWorker)内完成,并且只有这个线程的启动应该在GUI线程内完成(例如actionPerformed方法)。我假设start按钮直接调用runProc,这意味着它正在事件调度线程上运行。 然后,该线程卡在while(true)循环中,永远不会返回到应用程序。也就是说,停止按钮永远没有机会运行 您的开始按钮应该使用SwingWorker和SwingUtilities来调用EDT之外的方法,以及一个布尔变量start和stop the thread vs System.exit(0)
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入javax.swing.JButton;
导入javax.swing.JFrame;
导入javax.swing.JPanel;
导入javax.swing.SwingUtilities;
导入javax.swing.SwingWorker;
公共类TestProc{
布尔值isRunning=false;
公共TestProc(){
JFrame f=新的JFrame();
JButton b=新JButton(“开始”);
b、 addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件e){
新SwingWorker(){
公共无效doInBackground(){
如果(!正在运行){
isRunning=true;
runProc();
}
返回null;
}
}.execute();
}
});
JButton b1=新JButton(“结束”);
b1.addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件e){
SwingUtilities.invokeLater(新的Runnable(){
公开募捐{
killProc();
}
});
}
});
f、 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f、 setContentPane(新的JPanel());
f、 getContentPane()。添加(b);
f、 getContentPane()。添加(b1);
f、 包装();
f、 setVisible(真);
}
public void runProc(){
同时(正在运行){
系统输出打印项次(“.”);
试试{Thread.sleep(100);}catch(InterruptedException e){}
}
}
公共无效killProc(){
isRunning=false;
System.out.println(“死了!”);
}
公共静态void main(字符串参数[]){
新的TestProc();
}
}
我们怎么知道你的按钮为什么在做什么?您不认为我们需要查看按钮/面板后面的代码来回答您的问题吗?你认为我们是巫师吗?
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;
public class TestProc{
boolean isRunning = false;
public TestProc(){
JFrame f = new JFrame();
JButton b = new JButton("Start");
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new SwingWorker<Void, Void>() {
public Void doInBackground() {
if(!isRunning){
isRunning = true;
runProc();
}
return null;
}
}.execute();
}
});
JButton b1 = new JButton("End");
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
killProc();
}
});
}
});
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setContentPane(new JPanel());
f.getContentPane().add(b);
f.getContentPane().add(b1);
f.pack();
f.setVisible(true);
}
public void runProc (){
while(isRunning){
System.out.println(".");
try { Thread.sleep(100); } catch (InterruptedException e) {}
}
}
public void killProc(){
isRunning = false;
System.out.println("DEAD!");
}
public static void main(String args[]){
new TestProc();
}
}