Java thread and java Multithreading

Source: Internet
Author: User

Java thread and java Multithreading
Thread and process concepts:
Process: in the Java language, when the Java command is used to execute the. classfile or the. exe file, the program code is first loaded to the memory area, and then the operating system finds the program code entry (main method) to start executing the program. The above is about the memory Analysis of the Java program execution process. When the program code is loaded to the memory area, it is already considered a process, and it is not executed. Here we emphasize that the process isStatic. I personally think that if we emphasize that it is a static concept, we will split the process from the thread. It is not necessary to interpret the process as an application running in the memory. Although we often say that the process is started, it is essentially the main thread in the program. Thread:
Is a different execution path in a program. Multi-process:
Run multiple applications in the memory at the same time. Multithreading:
For a single CPU, there is essentially no multithreading. The CPU can only rotate and process multiple threads. At a certain time point, only one thread can be processed. However, because the processing speed is extremely fast, no one can feel the time difference between the rotating and processing threads, therefore, we feel that "at the same time" is executing multiple threads, that is, time-sharing. Multiple Threads exist for multiple CPUs.
Thread creation and startup:
Thread definition and thread instantiation (two methods) 1. subclass inherits the Thread and overwrites the Run method ------> new subclass () 2. implement the Runnable interface ------> call the Thread constructor new Thread (Runnable target) Thread to Start and call the Start method of the Thread object
Thread status conversion:
New State: the thread object has been created, but the start () method is not called. ready state: Call the start () method, however, the scheduler has not selected it as the running thread (that is, the Run method has not been called). The running state is selected as the currently executed thread, run the Run method to block/Wait/sleep status: when an event occurs, the thread enters the blocking status, with blocking and relief, and returns to the ready status, waiting for scheduling. Dead state: When the thread's Run method is completed, it is considered dead.
Thread control method:
1. sleep Thread. sleep (): Pause the running Thread and start sleep. After the sleep duration, the Thread automatically wakes up and enters the ready state instead of the running state. 2. Set thread priority
SetPriority () 3. thread concession Yield () is theoretically an opportunity for a thread with the same priority to run. The current thread returns to the ready state from the running state, but may be selected again, the real concession cannot be guaranteed. 4. Merge threads into A thread. If thread B is added to thread A, thread A will wait until thread B is executed.

Thread Synchronization and lock mechanism:
Synchronized is a keyword of the Java language. It can be used to lock objects and methods or code blocks. When a method or code block is locked, only one thread can execute this code segment at a time. When two concurrent threads access the lock synchronization code block of the same object, only one thread can be executed within a time period. The other thread must wait until the current thread finishes executing this code block before executing this code block. However, when a thread accesses a lock code block of an object, the other thread can still access the non-lock code block of the object.
When a thread accesses a synchronized (this) synchronization code block of the object, access by other threads to all other synchronized (this) synchronization code blocks of the object will be blocked.
Wait releases the lock obtained by the current thread on this object. When a thread acquires the lock on the object, the lock will not be released even if the thread is in Sleep.
Summary:
The difficulty in this chapter is to understand and use thread synchronization. However, to understand the scenarios where thread synchronization occurs and grasp the role of the synchronized keyword, that is, the above several items, it is easier to understand.




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.