Java Multithreading Basics Summary

Source: Internet
Author: User
Tags semaphore volatile

I. Threading and process relationships

Ii. Ways of Creation
1. Inherit the thread class, overriding the Run method
2. Implement the Runable interface, overriding the Run method
3. Using Anonymous internal classes

Third, API interface
Start ()
CurrentThread () Gets the current thread object
GetID () Gets the current thread ID thread-number starting from 0
GetName () Gets the current thread name
Sleep () Hibernate thread
Stop () stopping the thread

Iv. Instantiating Threads
Thread () assigns a new thread object
The thread (String name) assigns a new thread object with the specified name as its name
Thread (runable R) assigns a new thread object
Thread (runable R, String name) assigns a new thread object

Five, The Guardian process
User threads do not stop with the main thread stopping
Daemon thread, stop with main thread, Setdaemon (true), set daemon thread
GC thread belongs to daemon thread

Six, multi-threaded running state

New state thread thread = new Thread ()
Ready state, waiting for CPU scheduling Thread.Start ()
Run Status Thread.run ()
Blocking state Thread.Sleep ()
Death Status Thread.stop ()

Vii. Join () method
The role of Join () is to make other threads wait until the current thread finishes executing.

VIII. Thread Safety issues
Multiple threads are shared at the same time, the same global variable or static variable, write operations, may occur data conflict problems.

Nine, Thread safety solutions
Synchronized or use lock lock

1. Synchronous code block, can be any object
Synchronized (same data) {
Thread conflict issues may occur
}
2. Sync function, use this lock.
3. Static sync function, use bytecode file lock.

Nine, deadlock problem
Nested synchronization, which prevents the lock from being released

Ten, multithreading three major characteristics
Atomicity, visibility, order.

Xi. Java Memory model (JMM)
Shared variables between threads are stored in main memory, each thread has a private inland memory, and local memory stores the thread to read/write a copy of the shared variable. Local memory is just a concept that covers caching, write buffers, registers, and other hardware and editor optimizations.

12. Volatile
The volatile keyword resolves visibility issues between threads. Volatile forces the thread to fetch values from the main memory each time the value is read.

13, Atomicinteger Atomic class
You can manipulate the add and subtract in a thread-safe manner.

14. The difference between volatile and synchronized
Volatile alone does not guarantee thread security. (Atomic nature)
①volatile is lightweight and can only modify variables. Synchronized heavy-weight, also can be modified method
②volatile can only guarantee the visibility of the data and cannot be used for synchronization because multiple threads concurrently accessing volatile modified variables are not blocked.
Synchronized not only guarantees visibility, but also guarantees atomicity, because only the thread that obtains the lock can enter the critical section, guaranteeing that all statements in the critical section are executed. Blocking occurs when multiple threads scramble for synchronized lock objects.
Thread Safety
Thread safety includes two aspects, ① visibility. ② Atomic nature.
Using volatile alone does not guarantee thread safety. Synchronized, however, can implement thread security.

XV, inter-thread communication, wait () and notify (), Notifyall ()
belongs to the object method
Wait () allows the current class-locked thread to become dormant from the running state, freeing the lock's resources.
Notify () allows the current class-locked thread to change from hibernation to running state.

In general thread synchronization, the same lock resource is used.

16, Lock Lock (jdk1.5 and contract)
Lock lock = new Reentrantlock ();
try{
Lock.lock ();
Lock.unlock ();
}catch () {
}

17, condition implementation and wait (), notify () the same function
await (); with wait ()
Signal (); with Notify ()

18. How to stop a thread
It is not recommended to use Stop (),
1, design while, variables to determine the exit cycle.
2, interrupt () Let the thread throw an exception.

19, Threadlock
Provide a local variable for each thread.
The underlying implementation
Map.put (Thread.CurrentThread (), Object);

20. Java and contract
Atomic class, Lock, concurrency class

21. Vector and ArrayList
The implementation principle is through the array realization, the query speed is fast, the increment, the modification, the deletion speed is slow. Difference
Vectors are thread-safe and ArrayList are thread insecure.

22, HasTable and Hasmap
The implementation principle is all through the linked list + array
HasTable is thread-safe and hasmap is thread insecure.

23, Collection.synchroniedmap ()
Switch the non-secure map collection to a secure map. (proxy mode)

24, Concurrenhasmap
A segmented lock that splits a whole into multiple small hastable, with a default of 16 segments.

25, Countdownlatch
counters, you can limit the order of execution.
Countdownlatch Countdownlatch = new Countdownlatch (2);
Countdownlatch.countdown (); minus 1 each time
Countdownlatch.countdown ();
Countdownlatch.await (); Not waiting for 0 o'clock

26, Cyclicbarrier
The counter, when the number of threads reaches a certain number, executes concurrently.
Cyclicbarrier cyclicbarrier = new Cyclicbarrier (5);
cyclicbarrier.await; Wait for threads to reach 5, execute concurrently with each other

27, Semaphore
Counter
Semaphore Semaphore = new Semaphore (5); Maximum number of resources
Semaphore.availablepermits (); Judging Resources
Semaphore.acquire (); Get Resources
Semaphore.release (); Freeing resources

28. Concurrent Queues
Bounded and unbounded
Blocking queues and non-blocking queues
Concurrentlinkeddeque, unbounded non-blocking queue
Blockingqueue, bounded blocking queue

Reprint please indicate the source.
Wuxiwei
Source: http://www.cnblogs.com/wxw16/p/8915834.html

Java Multithreading Basics Summary

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.