Read Java multithreaded Programming Core Technology summary __ Programming

Source: Internet
Author: User

Chapter One Java multithreading skills

A process is the structural foundation of an operating system; A thread can be viewed as a subtask of a process. Threads can be implemented either by inheriting the thread class or by implementing the Runnable interface. The implementation of the interface is better than inheritance.

Stop threads can be thrown by throwing exceptions, return, or brute force means stop (), stop stop thread is too violent, easy to cause data loss, so do not recommend

pausing thread suspend () and resume methods using thread-safe implementation through the synchronized synchronization method to synchronize quickly through synchronize (monitor object) {}

Dirty reads: When a thread reads an instance variable, this value has been changed by another thread

Locks are automatically released when an exception occurs in a synchronous hurry

The synchronized block can effectively reduce the range of synchronization, shorten the running time and improve the efficiency.

Static synchronization methods lock the Class class. The static synchronization method is synchronous for all threads that execute the method

The reason for the deadlock is

mutually exclusive conditions. That a resource can only be occupied by one process over a period of time

Cannot preempt the condition. Resource applicants cannot forcibly seize resources from the resource owners until the resources obtained by the process have been used.

Possession and application conditions. The process has at least one resource, but it is requesting a new resource; Because the resource is already occupied by another process, the process blocks

Loop wait condition. There is a process wait sequence {P1,P2,...,PN}, where P1 waits for P2 to occupy a resource, P2 waits for P3 to occupy a resource, and so on


As long as the monitor object has not changed, the monitor object's properties have changed and the results of the run are synchronizedvalatile keywordValatile mainly guarantees the member variable's one visibility, mainly uses the scene is the cyclic condition key word valatile and the synchronized comparison: 1, Valatile is the thread synchronization lightweight implementation, in the performance certainly is better than the synchronized 2, Multithreading access Valitile will not be blocked, and synchronized will be blocked 3, Valatile can guarantee the visibility of the data, but can not guarantee the atomicity, and synchronized can guarantee the atomicity, but also can indirectly guarantee visibility of 4, Valatile solves the visibility of variables across multiple threads, while the Synchronized keyword addresses the synchronization of access resources between multiple threads
Communication between ThreadsWait () causes the thread to stop running, and notify causes the stopped thread to continue.
The wait () method executes immediately to release the lock notify () executes the sync code block before releasing the lock and releasing the lock when an exception is encountered during the execution of the synchronized code block
Communication between Threads PipedInputStream and PipedOutputStream Pipedreader and pipedwriter by byte stream
Use Inputstream.connnect (OutputStream) to generate a communication link between two stream.
You can use the Valatile modifier variable to use this variable to do a switch to achieve the purpose of cross running two pieces of code. The join () method releases the lock of the current thread, and sleep () holds the lock and is not released.
Class Threadlocal solves the isolation of variables between different threads, that is, different threads have their own values, and the values of different threads can be saved in threadlocal for saving
Use of lockReentrantlock is more powerful in function, for example, with sniffer function, Multiplex branch notification function, more flexible to use
Reentrantlock call Lock () lock, be sure to call the Unlock () method in the finally{} module to unlock
Use condition to implement wait/notify: You can invoke the condition await () method and the signal () method to implement fair and unjust locking by passing in true and false when invoking the Reentrantlock constructor Use multiple condition in the condition implementation order, with each condition serving a thread. A static variable is used to control the wake-specified thread.
Using Reentrantreadwritelock can speed up the efficiency of the operation, and in some methods that do not need to manipulate instance variables, you can use read-write locks to elevate the code run of the method completely.

TimerTimer timer = new timer () Timer schedule (TASK,DATEREF): Running a task at a specified time Timetask is executed in one order in the queue, so execution time may not coincide with the expected time, Because the previous task takes a long time, the later task runs. Timer Schedule (Task,datefirst,period) performs tasks periodically after a specified time




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.