我的项目现在有2天这个问题了。而且我不确定是什么错。我的项目包括2个类,main和另一个使用ExecutorService通过ProcessBuilder调用的类。 这是代码。
主要课程:
public class DesktopApplication1View extends FrameView{
public Process process = null;
private executeCode execute = new executeCode();
public DesktopApplication1View(SingleFrameApplication app){
(.........)
jButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
console.setText("");
execute.start();
}
});
}
class executeCode implements Runnable{
private boolean executeStarted = false;
private ExecutorService executecode;
ReadStdout read;
WriteStdin write;
public executeCode(){
executecode = Executors.newSingleThreadExecutor();
read = new ReadStdout();
write = new WriteStdin();
}
public void start(){
if(executeStarted){
try {
// process.getInputStream().close();
process.getOutputStream().close();
process.destroy();
} catch (IOException ex) {}
}
console.append("start\n");//debugging purpose
executecode.execute(this);
}
public void run(){
console.append("Execute thread = " + Thread.currentThread().getName() + "\n");//debugging purpose
executeStarted = true;
try {
ProcessBuilder builder = new ProcessBuilder("java", "-cp", "Project.jar", "project/oddeven");
builder.redirectErrorStream(true);
process = builder.start();
read.startReadStdout();
write.startWriteStdin();
}
catch (IOException e1) {console.append("error io");}
// return;
}
}
class WriteStdin implements Runnable{
private String input = null;
private BufferedWriter writer = null;
private ExecutorService executeWrite = Executors.newSingleThreadExecutor();
public void startWriteStdin(){
writer = new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));
executeWrite.execute(this);
}
public void WriteStdin(){
console.addKeyListener(new java.awt.event.KeyAdapter() {
@Override
public void keyTyped(java.awt.event.KeyEvent e){
//save the last lines for console to variable input
if(e.getKeyChar() == '\n'){
try {
int line = console.getLineCount() -2;
int start = console.getLineStartOffset(line);
int end = console.getLineEndOffset(line);
input = console.getText(start, end - start);
writer.write(input);
writer.flush();
} catch (Exception e1) {}
}
}
});
}
@Override
public void run(){
console.append("Write thread = " + Thread.currentThread().getName() + "\n");//debugging purpose
if(input == null) this.WriteStdin();
}
}
class ReadStdout implements Runnable{
private ExecutorService executeRead = Executors.newSingleThreadExecutor();
private BufferedReader reader = null;
public void startReadStdout(){
reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
executeRead.execute(this);
}
public void run() {
console.append("Read thread = " + String.valueOf(Thread.currentThread().getName()) + "\n");//debugging purpose
String line;
try {
while((line = reader.readLine())!=null)
console.append(line+"\n");
}catch (IOException e) {}
console.append("read done");//debugging purpose
}
}
oddeven class:
public class oddeven{
static double num = 0;
static int even = 0, odd = 0, zero = 0;
public static void main(String[]args) throws Exception{
odd();
}
public static void odd() throws Exception{
try{
BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter numbers\n(Input negative value to end)\n");
num = Double.parseDouble(dataIn.readLine());
while(num>=0){
if(num == 0)
zero++;
else if(num%2==0)
even++;
else
odd++;
num = Double.parseDouble(dataIn.readLine());
}
System.out.print("There are\n"+even+" even numbers\n"+odd+" odd numbers\n"+zero+" zero value");
} catch(NumberFormatException e){
System.out.print("Wrong input, enter again\n");
odd();
}
}
}
因此。如果我单击按钮一次,则输出为:
start
Execute thread = pool-2-thread-1
Read thread = pool-3-thread-1
Write thread = pool-4-thread-1
Enter numbers
(Input negative value to end)
1
2
-1
There are
1 even numbers
1 odd numbers
0 zero value
read done
但如果我关闭然后再次启动应用程序然后单击按钮两次而不在第一次单击时输入任何内容,输出将是:
start
Execute thread = pool-2-thread-1
read doneRead thread = pool-3-thread-1
Write thread = pool-4-thread-1
Enter numbers
(Input negative value to end)
1
2
-1
There are
2 even numbers
2 odd numbers
0 zero value
read done
正如您所看到的,当我在第一次单击时没有输入时单击按钮两次,输出流未关闭,我认为这会导致从流中输入oddeven类寄存器2。但是如果你在第一次点击时输入,那么第二次点击没有得到任何输入,你再次点击该按钮,流将不再注册2输入,它只会注册一个。
这不应该发生,因为首先,如果单击按钮并且再次实例化缓冲区,则会破坏该过程。或者我错了。
我希望你明白我的观点。谢谢。