Good reading superficial understanding (i) Java multithreaded Programming core technology

Source: Internet
Author: User

1th Chapter Java Multithreading Skills 2nd Chapter object and variable concurrent access 1 Synchronized synchronization method 2 synchronized Sync code block 3 volatile 3rd Chapter thread Communication 1 wait notification mechanism 2 Join Method 3 Threadlocal Class Chapter 4th Lock use 1 Reentrantlock Class 2 Reentrantreadwritelock class Chapter 5th Timer timer 6th Chapter single case mode and multithreading 7th Chapter Addendum Content Summary bad place good place

after reading often not much impression, decided to make a simple record or summary, used to deepen the impression.
1th Chapter Java Multithreading Skills

1 the way to achieve multithreading into two main, one is to inherit the thread class, one is to implement the Runnable interface
2) CurrentThread () method, return the code snippet is being called by which thread. Pay attention to Thread.CurrentThread () The difference between this and this is
3 isAlive () method to determine whether the current thread is active. The active state means that the thread has started and has not yet terminated
the 4 sleep () method, the currently executing thread sleeps at a specified time, and does not release
the lock 5) method of stopping the thread, Normal stop (execution end), stop method (deprecated), interrupt method
6 interrupted (), static method, test when the thread has been interrupted, and clear interrupt ID
7) isinterrupted (), instance method, If the test thread has been interrupted, the thread that does not clear the interrupt identification
8) Sleep can also stop (need to catch exception)
9) Yield () method, let the CPU to other threads, also may give up after themselves and grab
10) thread priority, try not to use
11 The daemon thread, the Java thread is divided into two kinds, the user thread, the daemon thread. The daemon automatically destroys the thread after it has no user threads in the process
Chapter 2nd concurrent access to objects and variables 2.1 Synchronized Synchronization method
1 method inside variable for thread safety (on stack)
2) instance variable non-thread safe (on heap)
3) Synchronized method is locked instance object, multiple instances are not mutually exclusive
4) lock can be reentrant
5) exception, automatically release lock
2.2 Synchronized Sync code block
1 The synchronized code block improves efficiency by reducing the range of synchronization
2 synchronized () if not one, then asynchronous
3 synchronized (Class) Locks the class object =synchronized static method
4) Synchronized code block try not to use string as a lock object
2.3 Volatile
1 volatile modified variables have visibility. That is, the thread is used to read from the main memory.
2 volatile does not have atomic
3) combined with 1, 22 points, volatile is suitable for only one thread to modify the data, or as one thread modifies Flag=false, other threads stop this.
4 atomic classes are not completely secure (methods are not synchronized), similar to Hashtable
Chapter 3rd communication between Threads 3.1 waiting/notification mechanism
1 The Wait () method waits for the thread that is currently executing code. Will immediately release the lock
2 Notify () method notifies a wait state and waits for the lock thread, if multiple is randomly notified one. will release the lock, but not immediately, to wait for synchronization in the region code execution complete
3) The Notifyall () method notifies the wait state and waits for the lock for all threads, releasing the lock and notify method as
4) above the 1,2,3 will release the lock, so need to get the lock 5 before executing the Sleep
() method does not release the lock
6) The thread of Wait (), the call to the interrupt () method throws Interruptedexception
7) (long) is a long time without being awakened, then automatically awakened by
8) Multiple producers, multiple consumers If the use of notify may awaken the same kind, may appear suspended animation. Use Notifyall to solve
9) You can use Pipeinputstream,pipedoutputstream,pipedreader, PipedWriter implementation of communication between threads
3.2 Join Method
1 the function of join () is to make the task in the normal order run () of the thread object to which it belongs, and to make the current thread z block to the thread x destroy and then execute
2 join () is implemented using the Wait () method, synchronized is the object monitor used
3 start thread A in b,b, then A.join (), if B.interrupt (), then B throws an exception, a normal
4) join (long) has more blocking time than join (), and sleep (long) The difference is that the join releases the lock (because it is implemented by a wait)
3.3 Threadlocal class
1 each thread can use ThreadLocal to store private data. Set () setting, Get () return (why not a map that way, OH)
2) The initialization value of get (), through extends ThreadLocal, Override InitialValue () implementation
Chapter 4th use of Lock 4.1 Reentrantlock class
1) synchronized combined with Wait (), notify (), Notifyall () can implement waiting/notification mode, reentrantlock using condition object to implement wait/notification mode
2) use Notify () The thread being notified is randomly selected by the JVM, and the Condition class can select 3 of its own ()
object.wait () equivalent to condition.await (), object.notify () equivalent to Condition.signal ()
4) The await () and the signal () method need to obtain the lock 5 before using the
new Reentrantlock (Boolean Isfair) to specify whether it is a fair lock, but the fair lock is not absolutely fair
6) The following 7-9 are some method descriptions for the Reentrantlock class
7) Getholdcount () returns the number of calls to lock (), Getqueuelength () returns the number of thread estimates that are waiting to get the lock. Getwaitqueuelength (condition) returns the number of threads that are waiting to get this lock this condition estimated
8 hasqueuedtread (thread thread) returns whether the specified thread is waiting to acquire the lock. Hasqueuedthreads () returns whether the thread is waiting to get this lock, haswaiters (condition) returns whether the thread is waiting for this lock and the condition
9 Isfair () returns a fair lock, Isheldbycurrentthread () returns whether the current thread holds this lock and islocked () returns whether the lock is held by any thread
4.2 Reentrantreadwritelock class
1 read sharing, reading and writing mutually exclusive, write mutual exclusion, write read mutual exclusion
The 5th Chapter Timers Timer
The book is just about how to use, you can go directly to the API documentation, and now project development generally use the encapsulated spring task or quartz, so there is no record of
6th Chapter single case mode and multithreading
1 in the blog http://blog.csdn.net/qq315737546/article/details/29387381 in the case of a single example mode has been described
2 it is best to use enumerations to implement a single example, you can use class to repackage a layer ( Example of 6.7)
Chapter 7th supplementary Supplements
1 The Thread.state enumeration lists all states of the thread and has an explanation


2 The thread state is switched by means of the method, and some switches are one-way
3 The thread can be attributed to a group of threads, unified management
4 SimpleDateFormat is non-thread safe
5 The thread exception can be captured through Uncaughtexceptionhandler. You can set the default exception handler Setdefaultucaughtexceptionhandler ()
6) By default, exceptions to one thread in a thread group do not affect other threads, and you can stop all threads within a group by customizing the thread Group + overriding the Uncaughtexception () method
7 Thread exception Handling Thread.setuncaughtexceptionhandler () >thread.setdefaultuncaughtexceptionhandler () > Thread Group exception Handling Content Summary

1.  using synchronized (Sync method, synchronized code block), lock, the focus is on knowing whether the object monitor they are using is a (that is, a locked object) of
2.  Lock is stronger and more flexible than synchronized
bad place.
1.  Most of the examples in the book are used by XX extend Thread to implement, this implementation can directly call the XX instance Xx.start () method to execute. No more thread t = new Thread (XX), I think it would be more appropriate to implement the runnable way of writing. (The thread class itself implements the Runnable interface ...)
2.  some places have a conclusion, there is code, but no explanation, do a bit do not understand
3.  Some places only code ... More difficult to understand
Nice place.
1.  A lot of code examples, the view is through the code to prove
2.  Many times, some knowledge points do not know how to verify, this book can enhance the ability of examples

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.