Java Multithreading Knowledge Point collection-Total

Source: Internet
Author: User

1. Thread-to-process relationships

Process: A process occurs when a program enters memory and starts running. A process is a standalone unit whose resources are allocated and dispatched by the system.

Thread: The thread is the execution unit of the process, and the thread is a separate, concurrent execution flow in the process. When the process is initialized, the main thread (main) is created.

1) threads are part of a process, a process can have multiple threads, and a thread must have a parent process.

2) The thread has its own stack, program counter, and local face, but does not own system resources, and it shares all the resources of the parent process with other threads of the parent process.

3) thread scheduling and management is done by the process itself.

2. How to create Threads

Http://www.cnblogs.com/moonpool/p/5472549.html

There are three ways to create

1) inherit the thread class--cannot share resources, when implementation is convenient

The thread class is an abstract class

2) Implement Runnable interface--can be shared resources, in the new thread object, you need to runnable interface implementation class object as Target

Runnable is an interface class

3) Creating Threads with callable and future-threads with return values

callable and future are interface classes, Futuretask is the implementation class of the future

3. Thread life cycle (image from Java crazy handout)

4. Threading Control Methods

Http://www.cnblogs.com/moonpool/p/5494935.html

1)method in join--thread, called by the program using the thread

-If object a uses object B.join within its own method, then A's thread will be blocked until the b thread dies and a thread can continue.

2) method Setdeamon (True) in the background thread--thread

-Daemon, the background thread dies after all the current threads have died

3)static method of sleep--thread class

--in a blocked state within the set time, when the time is up, it becomes ready, waiting to be called

4) The static method of yield and thread priority class--thread

When this method is used, the current thread becomes ready, and the operating system picks the highest-priority thread in the ready state, assigns IT resources, and allows it. Normal threads are normal, priority and advanced and low-level, with the appropriate method to set.

5. Thread Synchronization Methods

Http://www.cnblogs.com/moonpool/p/5480593.html

1) Synchronizing code blocks- -decorating code blocks with the synchronized modifier when overriding the run () method

2) Synchronization Method- -Modifying a method using the synchronized modifier when defining a class

3) Sync lock lock--lock and Readwritelock two interfaces, the corresponding implementation class is Reentrantlock and Reentrantreadwritelock

6. Thread Communication Methods

Http://www.cnblogs.com/moonpool/p/5496887.html

1) If you are using synchronous code blocks and synchronous methods to achieve thread synchronization, you need to use traditional thread communication methods

Add Wait (), notify (), and Notifyall () to block the calling thread or notify other threads to run in the thread call method (the method is synchronized decorated) .

The three methods of Wait (), notify (), and Notifyall () are provided by the object class

Http://www.cnblogs.com/moonpool/p/5500845.html

2) If you are using a synchronous lock method to achieve thread synchronization, you need to use condition to control thread communication

Similar to the first one, it is necessary to use the Condition object to add Lock.lock (), condition.await (), synchronized in the thread-calling method, which does not need to be decorated by the method . Condition.signal (), Condition.signalall (), and Lock.unlock () to implement thread communication

Here first the Newcondition () method of the lock object is required to return a condition object, and await (), signal (), Signalall () are the methods provided by the condition object

classBoundedbuffer {Final Lock lock = new Reentrantlock ();    Final Condition notfull = Lock.newcondition ();    Final Condition notempty = Lock.newcondition (); Finalobject[] Items =Newobject[100]; intputptr, Takeptr, Count;  Public voidPut (Object x)throwsinterruptedexception { lock.lock (); Try {        while(Count = =items.length) notfull.await (); ITEMS[PUTPTR]=x; if(++putptr = = items.length) putptr = 0; ++count;     notempty.signal (); } finally { lock.unlock (); }   }    PublicObject Take ()throwsinterruptedexception { lock.lock (); Try {        while(count = = 0) notempty.await (); Object x=Items[takeptr]; if(++takeptr = = items.length) takeptr = 0; --count;       notfull.signal (); returnx; } finally {lock.unlock (); }   } }

Http://www.cnblogs.com/moonpool/p/5503287.html

3) Blocking queue (Blockingqueue)

Blockingqueue is a sub-interface of the queue, but is primarily used for thread synchronization

Throw exception Different return values Blocking threads Specify the time-out period
End of Team Insert element Add (E) Offer (e) Put (e) Offer (E,time,unit)
Remove an element from the opponent Remove () Poll () Take () Poll (Time,unit)
Get, not delete elements Element () Peek () No No

----------------------------------------------------------------------------------------------

7. Thread Groups

Thread classification can be managed, and priority settings such as

Threadgroup class

The constructor method provided by the thread class specifies which thread group the new thread belongs to.

8. Thread pool

The purpose of the existence is to save system resources, put new threads into the thread pool, allocate resources and run by the threads pool, and when the thread is sold, the resources allocated to the thread will be reclaimed back to the thread pool.

Static factory methods provided by the Executor factory class to create Executorservice objects

9. Thread-related classes

The Threadlocal class's member variables, when called by a thread, produce a copy that allows the thread to maintain its own copy of the variable at will, ensuring that the thread does not have a resource conflict before it, and that the synchronization method for resource sharing is fundamentally different from the usage scenario.

Thread-Safe Collection Class The normal collection class is thread insecure, where a thread-safe collection class can be called by a thread to produce a copy, which can ultimately guarantee the integrity of the original set of data.

Thinking: Usage scenarios for thread-related classes.

Java Multithreading Knowledge Point collection-Total

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.