1. Read the following code first
Import Java.io.FileInputStream;
public class TTT {public
static void Main (string[] args) throws Exception {for
(int i = 0; i < i++) {
final String threadId = "Thread_" + i;
Thread thread = new Thread (new Runnable () {public
void run () {
System.out.println (threadId + "started!");
try {
FileInputStream fis = new FileInputStream ("/opt/test.log");
Thread.Sleep (* 1000);
} catch (Exception ex) {
ex.printstacktrace ();
}
System.out.println (threadId + "stopped!");
}
);
Thread.Start ();
}
Thread.Sleep (* 1000);
}
2. Compile and run this class on Linux, and then use the Linux command/usr/sbin/lsof-p <pid> to view the file information opened by this program
$/usr/sbin/lsof-p ' Ps-ef | grep java | grep TTT | awk ' {print $} ' | grep "Test.log"
java 21562 fkong 3r REG 253,0 0 35471424/opt/test.log
java 21562 fkong 4r REG 253,0 0 35471424/o Pt/test.log
java 21562 fkong 5r REG 253,0 0 35471424/opt/test.log
java 21562 fkong 6r REG 253,0 0 35471424/opt/t Est.log
java 21562 fkong 7r REG 253,0 0 35471424/opt/test.log
java 21562 fkong 8r REG 253,0 0 35471424/opt/test. Log
java 21562 fkong 9r REG 253,0 0 35471424/opt/test.log
java 21562 fkong 10r REG 253,0 0 35471424/opt/test.log
java 21562 fkong 11r REG 253,0 0 35471424/opt/test.log
java 21562 fkong 12r REG 253,0 0 35471424/opt/test.log
Whether the 10 threads are running or running, the same results are viewed using the lsof command, and you can see that 10 file streams are not closed.
3. I have made some changes to this code, that is, after the thread is finished, all threads are set to NULL, as follows
import java.io.FileInputStream; import java.util.ArrayList; import java.util.List; public class TTT {public static void main (string[] args) throws Exception {list<thread> threads = new ArrayList
<Thread> ();
for (int i = 0; i < i++) {final String threadId = "Thread_" + i;
Thread thread = new Thread (new Runnable () {public void run () {System.out.println (threadId + "started!");
try {fileinputstream fis = new FileInputStream ("/opt/test.log");
Thread.Sleep (60 * 1000);
catch (Exception ex) {ex.printstacktrace ();
} System.out.println (ThreadId + "stopped!");
}
});
Thread.Start ();
Threads.add (thread);
} thread.sleep (2 * 60 * 1000);
for (thread thread:threads) {thread = null;
} System.out.println ("Clean up threads!");
Thread.Sleep (10 * 60 * 1000); }
}
Once again, the 10 threads are running and after running, using Lsof view, the results are still similar, or 10 file streams are not closed.
I made some changes again, adding (or urging the JVM) to do a few GC operations after all threads were set to NULL, as follows:
import java.io.FileInputStream; import java.util.ArrayList; import java.util.List; public Class TTT {public static void main (string[] args) throws Exception {list<thread> threads = new Arraylist<thr
Ead> ();
for (int i = 0; i < i++) {final String threadId = "Thread_" + i;
Thread thread = new Thread (new Runnable () {public void run () {System.out.println (threadId + "started!");
try {fileinputstream fis = new FileInputStream ("/opt/test.log");
Thread.Sleep (60 * 1000);
catch (Exception ex) {ex.printstacktrace ();
} System.out.println (ThreadId + "stopped!");
}
});
Thread.Start ();
Threads.add (thread);
} thread.sleep (2 * 60 * 1000);
for (thread thread:threads) {thread = null;
} System.out.println ("Clean up threads!");
System.GC ();
System.GC ();
System.GC ();
System.out.println ("Finished gc!");
Thread.Sleep (10 * 60 * 1000); }
}
Using Lsof again, you can still see that there are 10 file streams open on the run, but in "finished gc!" After that, the result is that the 10 open file streams are closed.
Finally, I simply deleted those statements that set thread to NULL, and the results were consistent with the results of the GC operation above.
Eventually, in the JVM, for those IO files that have not been turned off, they will not be used again until the next time the full GC is done, but let the JVM do the total return is not good, or the old saying, own things to do themselves.