a***t 发帖数: 12 | 1 在java程序中, 比如要调用一个system call, 但是不能确定这个
system call 什么时候才能返回, java程序如何控制system call的
调用时间?
Process p = Runtime.getRuntime().exec(commandLine);
// 调用外部程序
如果调用五秒钟还没有返回,就要放弃这个调用, 夺回程序控制
权该如何夺回?
多谢多谢。 | g*****g 发帖数: 34805 | 2 Launch a thread to do the job, main thread will sleep 5 seconds, then
wake up and kill the system call.
if the job is done in less than 5 seconds, you can wake up main thread.
【在 a***t 的大作中提到】 : 在java程序中, 比如要调用一个system call, 但是不能确定这个 : system call 什么时候才能返回, java程序如何控制system call的 : 调用时间? : Process p = Runtime.getRuntime().exec(commandLine); : // 调用外部程序 : 如果调用五秒钟还没有返回,就要放弃这个调用, 夺回程序控制 : 权该如何夺回? : 多谢多谢。
| t**s 发帖数: 22 | 3 严格地说一个Thread不能中止另外一个Thread。如果那个System Call是
Synchronize的Call的话,从Java内部无法中止。
【在 g*****g 的大作中提到】 : Launch a thread to do the job, main thread will sleep 5 seconds, then : wake up and kill the system call. : if the job is done in less than 5 seconds, you can wake up main thread.
| c*y 发帖数: 137 | 4 Depends on what you mean by "terminate a thread from another thread". You can
surely not directly call anything like "destroy()" to destroy that method. But
you can always have a flag variable (e.g., cancelled) and a public function
which can change this flag variable, make the flag variable false when the
thread starts, and check this variable while running the thread. And if you
want to terminate the thread from another thread, just set the flag variable
to be true from the main thread.
【在 t**s 的大作中提到】 : 严格地说一个Thread不能中止另外一个Thread。如果那个System Call是 : Synchronize的Call的话,从Java内部无法中止。
| t*******k 发帖数: 20 | 5 It may not work if you want to terminate the call in precisely 5 seconds.
In realtime Java, you might be able to set deadline.
【在 c*y 的大作中提到】 : Depends on what you mean by "terminate a thread from another thread". You can : surely not directly call anything like "destroy()" to destroy that method. But : you can always have a flag variable (e.g., cancelled) and a public function : which can change this flag variable, make the flag variable false when the : thread starts, and check this variable while running the thread. And if you : want to terminate the thread from another thread, just set the flag variable : to be true from the main thread.
|
|