https://luckytoilet.wordpress.com/2010/05/21/how-system-out-println-really-works/
How System.out.println() really works
System.java
public final static PrintStream out = nullPrintStream();
private static PrintStream nullPrintStream() throws NullPointerException {
if (currentTimeMillis() > 0) {
return null;
}
throw new NullPointerException();
}
FileOutputStream fdOut = new FileOutputStream(FileDescriptor.out);
setOut0(new PrintStream(new BufferedOutputStream(fdOut, 128), true));
JNIEXPORT void JNICALL
Java_java_lang_System_setOut0(JNIEnv *env, jclass cla, jobject stream)
{
jfieldID fid =
(*env)->GetStaticFieldID(env,cla,"out","Ljava/io/PrintStream;");
if (fid == 0)
return;
(*env)->SetStaticObjectField(env,cla,fid,stream);
}
.....
대충 넘기고.. 결국..
A tour through java.io
public void println(String x) {
synchronized (this) {
print(x);
newLine();
}
}
public void print(String s) {
if (s == null) {
s = "null";
}
write(s);
}
private void write(String s) {
try {
synchronized (this) {
ensureOpen();
textOut.write(s);
textOut.flushBuffer();
charOut.flushBuffer();
if (autoFlush && (s.indexOf(' ') >= 0))
out.flush();
}
}
catch (InterruptedIOException x) {
Thread.currentThread().interrupt();
}
catch (IOException x) {
trouble = true;
}
}
걍 그후는 링크가서 봐라.
ㅇㅇ
저거 다 이해가요? ㅇㅇ
못갈게 딱히 있냐 ?
JNI부분은 어떻게 해석?
그것은 그 OS에 맞는 C로 짜여있겠지.
링크 쭉 보니 아래에 그림이랑.. 그후에 JNI c로 구현된 부분까지 잘 설명되어 있넹. ㅇㅇ
ㅇㅅㅇ
버퍼를 비운다는 이해가 가는데 그래서 출력하는 코드는 어디?
출력은 JNI 구현 부분 말하냐 ? 그 부분은 링크의 상부에서 설명 되고 있고, 링크 내용 아래쪽은 자바에서의 println에 대한 이야기인데.. write 부분 구현 된 부분 읽어보면 되지 않음 ? ㅇㅇ 구글 번역기로 보고 있는데. ㅇㅇ
근데 왜 buffer 스트림 write랑 flush랑 연결이안대있어요?
Internally, the PrintStream object (System.out) contains three different objects to do its work: --> 이 부분 아랫쪽 이야기 ?
원문이라서 바로 안와닫으면 구글 번역기로 보면 되지 않니 ? 그기에.. write 자체는 버퍼에 쌓아두기만 했다가 나중에 PrintStream.wrie에서 플러시 수행한다고. 그것때문에 synchronized 사용한 것 같은데.
abstraction이 이렇게 중요합니다