As a business developer, there are few technologies that can be used at work. Although usually always say what, multi-threading, concurrency, injection, Attack! But in practical work, these things are not necessarily used. Because the framework we used has already done that.
For example, web development, there are a lot of requests coming in, supposedly, we should consider concurrency problems. But in fact, Spring receives the request, after assigning to the controller, it is already thread-safe, so all we have to do is to start from the controller, to the end of the final request response to ensure that the thread is safe.
Multithreading seems to have a lot of things to pay attention to, read the "Java multithreaded Programming Core technology", to do a summary, overall, actually not so much, not so complicated.
1. Multithreading Basics
Java Multi-threading, is reflected in the thread class and Runable interface! Shared data, there is a thread safety issue, and no shared data is thread safe. Many of Java's methods of stopping threads have been abolished and are not recommended for use such as: Resume,stop,suspend methods. Setting the thread priority (setpriority) may increase the speed of thread execution. The existence of the daemon requires at least one non-daemon thread to run, that is, the daemon thread cannot exist independently, its ability is relatively low, such as GC is the Guardian thread, when your code is executing, the GC is running, memory collection at any time, when your program execution completes, the GC thread will not exist.
2. Concurrent access to objects and variables
Synchronized synchronization method, lock object, lock code block, lock method, lock variable,
The Valatile keyword that uses volatile threads to resolve a synchronous dead loop does not stop the problem. Volatile forces the values of variables to be obtained from the public heap so that consistency is maintained.
3. Inter-thread communication
Wait/notify is the most basic way to implement inter-threading communication. It is convenient to implement the consumer/producer model.
After the wait method, the lock is released immediately, and the Notify Lock is not released. After the wait method executes, subsequent concurrent requests can enter the block, and notify will need to wait until the synchronized code block completes before releasing the lock.
Notifyall () wakes up all waiting threads.
The join () method frees the lock waiting for the thread to finish executing. Thread.Sleep () does not release lock waits.
Communicates through pipelines and is passed as a character stream. Pipewriter,pipereader,outputsream.connect (InputStream) links the input stream to the output.
ThreadLocal, can be considered a thread-level global variable, that is, in this thread, anywhere can be taken to this value, without worrying about thread safety issues. Set (), The Get () method for setting and fetching.
4. Use of lock
Reentrantlock,
5. Timers Timer
Timer.schedule (Task, dateref) executes once, and Timer.schedule (task, dateref, period) executes periodically.
6. Single Case mode and multithreading
if NULL ) { synchronized(MyObject. Class) { ifnullnew MyObject (); }}}
Well, it seems that the knowledge of multithreading is still very small. Do not have too many things, fine enough!
Java multithreaded Programming Core technology-notes