package java.lang;
public class Runtime
Every Java application has a single instance of class Runtime
that allows the application to interface with the environment in which the application is running. The current runtime can be obtained from the getRuntime
method.
An application cannot create its own instance of this class.
모든 java application은 running중인 환경과 통신할수있도록 Runtime class의 single Instance를 가지고있다.
getRuntime() 을 이용하여 current Runtime을 획득할수있다.
application은 자신만의 Runtime class 의 객체를 만들수없다.
getRuntime
public static Runtime getRuntime()
Returns the runtime object associated with the current Java application. Most of the methods of classRuntime
are instance methods and must be invoked with respect to the current runtime object.- Returns:
- the
Runtime
object associated with the current Java application.
현재 java application과 연관있는 runtime object를 반환한다.
Runtime class 대부분의 method들은 instance method이며 현재 runtime object에 대하여 invoke한다.
아래와 같은 식으로 getRuntime을 가지고와서 command에 "ping -t www.~~"등의 명령을 날릴 수 있다.
example)
Process p = Runtime.getRuntime().exec(command);
내가 궁금 했던것은 waitfor이다.
여기서 가져와 실행한 명령어 p는
자신만의 SubProcess를 실행한후 Main으로 다시 넘어와버린다. 만약에 Subprocess를 다 완료한후 main으로 넘어오게 하고 싶으면
p.waitfor()를 호출해주면된다. (Thread의 t.join()과 비슷하게 보인다.)
waitFor
public abstract int waitFor() throws InterruptedException
Causes the current thread to wait, if necessary, until the process represented by thisProcess
object has terminated. This method returns immediately if the subprocess has already terminated. If the subprocess has not yet terminated, the calling thread will be blocked until the subprocess exits.- Returns:
- the exit value of the subprocess represented by this
Process
object. By convention, the value0
indicates normal termination. - Throws:
InterruptedException
- if the current thread is interrupted by another thread while it is waiting, then the wait is ended and anInterruptedException
is thrown.
필요에 의해 Object Process에의해 표현된 Process가 종료되기전까지 현재 대기중인 쓰래드를 Causes(야기한다,초래한다) 기다리게 한다. 만약 Subprocess가 이미 종료되었다면 이메소드는 즉시 리턴을 한다.
만약에 Subprocess가 아직 종료되지 않았다면 Subprocess를 빠져나올때까지까지 호출할 쓰래드는 잠금상태가된다.
아래의 그림은 A'p 와 B'p를 보면 이해 가 가려나? A'p가 wiatfor를 부른 녀석이다.
A A'p B B'p
| |
|___ |_____
| | |
____| | |
| | |
|. | |
'2014년 > JAVA' 카테고리의 다른 글
How to use Reflection to call Java method at runtime (0) | 2015.09.17 |
---|---|
[eclipse] jad plugin (0) | 2013.08.07 |
[pmd] 사용자 정의 룰셋 javaRuleset 안됨 (0) | 2013.05.10 |
VisualVM 으로 모니터링 하기 (0) | 2013.05.10 |
[checkStyle] Eclipse 적용 (0) | 2013.04.23 |