Java Thread Programming 1.1-Introduction to Threads
When the Java Virtual Machine (JavaVM, or just VM) is started by the operating system, a new process is created. Within that process, whose threads can be spawned (created ).
Normally, you wowould think of Java code execution starting with the main () method and proceeding in a path through the program until all the statements in main () are completed. this is an example of a single thread. this "Main" thread is spawned by the JavaVM, which begins execution with the main () method, executes all the statements in main (), and dies when the main () method completes.
A second thread is always running in the JavaVM: The garbage collection thread. it cleans up discarded objects and reclaims their memory. therefore, even a simple Java program that only prints Hello world to system. out is running in a multithreaded environment: the two threads are the main thread and the garbage collection thread
1. The operating system creates a process to run the Java Virtual Machine. In this process, multiple threads can be created.
2. Each program has a main thread. It starts and ends with the main thread.
3. There is a resident thread: The Garbage Collector, so even the simplest Hello world is multi-thread
One of the great things about Java is that it has built-in support for writing multithreaded programs. java's designers knew the value of multithreaded programming and wisely decided to include support for threads directly in the core of Java. chapter 7, "Concurrent Access to Objects and Variables," means es how in the Java language, the synchronized keyword is used to lock objects and classes to control concurrent access to data. the classes Thread and ThreadGroup are right in the core API in the java. lang package. the superclass of all classes in Java, Object, has inter-thread communication support built in through the wait () and Policy () methods (see Chapter 8, "Inter-thread Communication "). even if an underlying operating system does not support the concept of threads, a well-written JavaVM cocould simulate a multithreaded environment. in Java, thread support was not an afterthought, but encoded by design from the beginning.
Java supports thread technology from the beginning of Design
1. the keyword synchronized provides a lock mechanism for objects and classes to control concurrent data access.
2. the java. lang package has built-in Thread, and the ThreadGroup class supports Thread technology.
3. hyperclass objects of all java classes are supported by built-in thread communication through the wait () notify () method.