Thread Multithreading Summary

Source: Internet
Author: User
Tags stub

Purely personal understanding, but also ask you to give a lot of cattle advice.

Basic concepts of threads: First look at the basic concepts of the process: A program that is running on a computer that contains the system resource threads required by process Control blocks and programs: An execution path in the process, no independent system resources, shared process resources, but the ability to include a thread-running model in its own stack Java:      Each program has its own main memory, and each thread has its own working memory (registers and caches). When a thread is started, the thread needs to go to a shared variable, copy it to its working memory, and then use it, and then write it back into the main memory after the assignment. Typically, when assigned, the shared variable is written back to the main memory. But this process is not atomic, that is, when we change the value of the variable, not yet come and write back its new value, another thread also accesses the shared variable, which causes a read error. For example: The original is 1000 yuan a thread read plus 100, no writeback, B-thread read, or 1000 This is the other operation so that the results are not correct
Volatile: The introduction of variables is to solve this problem, but it will affect a certain performance. It means to be visible.    When we use this keyword for a shared variable (in fact, a very often time member variable), it means that when we make a change to the variable, we immediately write back to the main memory, an operation that is not divisible by atoms. Another mechanism-synchronization mechanism thread safety: when we access the same piece of code does not produce different results.    The thread is unsafe and the opposite is true.    Synchronization: means that a piece of code can be executed according to a dispatch mechanism to ensure thread safety the problem of bank withdrawals is a typical synchronization problem. Description: When making a withdrawal to determine the balance, when multiple accounts go to the time (that is, multiple threads) will appear negative
The above picture is the state change of the thread (a bit ugly). Need to focus on the blocking queue (block): Now I understand the extent of the understanding that there are roughly three 1. Normal blocks: Enter the queue when a thread is sleep () and join (). Wait until the time wait time is over. Wait for the column to run 2. Wait for block: I like to call it a wait pool, and when a thread waits () it goes into the queue. Lock the queue after wake up 3. Lock BLOCK: I call it the lock pool. When a thread acquires a sync lock, it enters the queue and enters the ready queue when it acquires the sync lock.
For each object the understanding of threading mechanism and common methods: 1. There is a monitor monitor in each object.   When you call Wait (), the current thread is added to the waiting pool and monitored by the Monitor (synchronized lock).   Executing notify (), Notifyall () wakes up the thread that the monitor is in charge of, bringing it into the lock pool and waiting for execution when the lock is acquired. In addition, the Wait () and notify Notifyall methods can only appear in the synchronized code block.  This is also well understood, as using these methods requires monitor monitoring. 2. Thread.Sleep (long) causes the current thread to sleep, into the normal lock pool.  and wait () The main difference is that it does not release the sync lock, and it can call 3.t.join anywhere (): literal understanding is the meaning of merging, in fact, the current thread waits for the thread that calls this method to finish executing. 4.t.interrupt (): The thread that calls this method breaks the thread if it is in a wait () sleep () join (), and the interrupt exception 5.t.setdaemon (TRUE) is set to the daemon. The simple understanding is that the guard line Cheng terminates after all other threads have been executed. The JVM's garbage collection is a daemon thread.  Calling the Main method is also a daemon thread. 6.t.setpriority () sets the priority. A value of (0 to 10) will play a role in the preemption CPU the following is a simple example: (after understanding the above method, write a complete example) Public classThreadtestfun { Public Static voidMain (string[] args) {//TODOauto-generated method stub Thread T1 =NewThread (NewThread1 ()); Thread t2 =NewThread (NewThread2 (Thread.CurrentThread ()));            T1. Start ();            T2. Start (); T1. SetPriority (10);Try{t1. Join ();           T1. Yield (); }Catch(Interruptedexception e) {                //TODOAuto-generated Catch block E. Printstacktrace (); }      } }classThread1ImplementsRunnable {@Override Public voidRun () {//TODOauto-generated Method Stub * * try {thread.sleep (10000);} catch (Interruptedexception e) {//TODO* auto-generated Catch block e.printstacktrace ();            } *//synchronized (this) {//try {//wait (); The catch (Interruptedexception e) {////TODOAuto-generated Catch block//E.printstacktrace (); // }            // } while(true) {notify ();           Notifyall (); }      } }classThread2ImplementsRunnable {thread thread; PublicThread2 (thread thread) {Super(); This.      thread = thread; } @Override Public voidRun () {//TODOauto-generated Method StubTry{thread.sleep (1000); }Catch(Interruptedexception e) {                //TODOAuto-generated E. Printstacktrace ();            }//synchronized (this) {//try {//wait (); The catch (Interruptedexception e) {////TODOAuto-generated Catch block//E.printstacktrace (); }//} thread

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.