In a Java program, you need to create an object, print the output to the screen, and start the thread. How long does it take? To solve this basic problem, we did the following tests:
Test the hardware environment:Lenovo yangtian E3100, AMD Sempron 64-bit 2600 + (about 1.6 Ghz), M memory, 80 GB hard drive
Test software environment:Eclipse3.2.2 + JDK5.0, run directly in Eclipse
Test code:Public class TestCreatObject ...{
Public static void main (String [] args )...{
Long start = System. currentTimeMillis ();
For (int I = 0; I <10000; I ++ )...{
String str = new String ("" + I );
}
Long end1 = System. currentTimeMillis ();
For (int I = 0; I <10000; I ++)
System. out. println (I );
Long end2 = System. currentTimeMillis ();
For (int I = 0; I <10000; I ++ )...{
TestThread tt = new TestThread ();
Tt. start ();
}
System. out. println ("create object time:" + (end1-start ));
System. out. println ("print screen time:" + (end2-end1 ));
System. out. println ("start thread time:" + (System. currentTimeMillis ()-end2 ));
}
}
Class TestThread extends Thread ...{
Public void run ()...{
// Try {
// Sleep (1 );
//} Catch (InterruptedException e ){
// E. printStackTrace ();
//}
}
}
Test results:Create object time: 32
Print screen time: 218
Start thread time: 1844
What is the relationship between the thread running time and startup speed during thread testing? ? The original thread did not execute anything, and now the blocked code is opened. , Respectively set the sleep () time from 1-> 10-> 100- "1000-" 10000, the result is as follows: 1-"start thread time: 211010-" start thread time: 2297100-"start thread time: 25941000-" start thread time: 567210000-"start thread time: 49875
Result Analysis:1. in Java programs, the time it takes to create an object is negligible. it takes a certain amount of time to print the output to the screen Because I/O operations are involved. It takes about 0.0218 ms each time. The system performance requirement is not very high and will not affect the performance of the program. it takes a lot of time to start a large number of concurrent threads. Even though the thread runs for a long time, the time spent is also rising. , It seems that multi-threaded processing is not the more threads, the better, you need to have a proper degree to maximize the process When the thread running time is longer, it may cause memory out-of-bounds exceptions.