Redian新闻
>
java 程序中的实时控制
avatar
java 程序中的实时控制# Java - 爪哇娇娃
a*t
1
在java程序中, 比如要调用一个system call, 但是不能确定这个
system call 什么时候才能返回, java程序如何控制system call的
调用时间?
Process p = Runtime.getRuntime().exec(commandLine);
// 调用外部程序
如果调用五秒钟还没有返回,就要放弃这个调用, 夺回程序控制
权该如何夺回?
多谢多谢。
avatar
g*g
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);
: // 调用外部程序
: 如果调用五秒钟还没有返回,就要放弃这个调用, 夺回程序控制
: 权该如何夺回?
: 多谢多谢。

avatar
t*s
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.

avatar
c*y
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内部无法中止。

avatar
t*k
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.

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。