Introduction and basic concepts of multi-threaded parallel computing serial----

Source: Internet
Author: User

First, background:

Recently, we are interested in parallel and concurrent programming under multicore conditions. In this will learn to comb the knowledge points to write up, if there is something wrong, look at correct.

Environment: Because the Java language has a good support for multithreading, Java is used to express relevant concepts at the time of introduction.

Content:

1. The concept of parallel concurrency, basic knowledge, the introduction of basic syntax in Java

2. Parallel programs:

An array summation algorithm under multi-threading condition: The application of common algorithm and fork-join framework in this paper is introduced. And the thought of divide and conquer

...

3. Concurrent programs:

...

...

Second, parallel, concurrency difference

Parallel: A large task is broken down into multiple subtasks that run on multiple CPU cores. Use additional computing resources to make programs run faster

At the same point in time, there are multiple threads executing

Concurrency: Let multithreaded programs run more correctly and efficiently under limited resource conditions.

The same time period, there are multiple threads executing

Shared Memory:

Single thread: A running program has a call stack, a program counter, a static domain, and an object instance within the stack are all exclusive

Multithreading: Each thread has a call stack and a program counter, and the threads can share static domains and objects (for read and write interactions between threads)

Parallel dilemma----acceleration ratio:

Upgrading from a single processor to n-way multiprocessor does not increase the computing power of N times: Because part of the work must be executed serially.

Speedup is defined as the ratio of the time that a single processor completes a work to the time it takes to complete the work concurrently with N processors.

The Amdahl law gives the maximum speedup that can be obtained when an application is performed in conjunction with N processors:

S = 1/(1-p + P/N)//  P is part of the application that can be executed in parallel / /  N for N processors

To understand the above formula, we can give an example: 10 processors, and the parallel portion is 60%. Acceleration ratio: S = 1/(1-0.6+0.6/10) = 2.17

Iii. Introduction to Java threading

Here is the main outline of the following will be used, the control of the thread of some common ways, if you do not know the Internet can find relevant information:

  Methods in the Thread class:

    -run (), Start ():

Calling start () causes the thread to go into a ready state, the thread goes into the running state of the code in the Run () method, the run () method must be public access, and the return value type is void.

    -yield (), join ():

The join () method causes the thread that invokes the method to complete before the thread that waits for the method finishes executing. Note that this method also needs to catch exceptions; the yield () method is similar to sleep (), except that it cannot be specified by the user for how long, and the yield () method only allows the same priority thread to have an opportunity to execute.

    -sleep ():

Causes the current thread (that is, the thread that called the method) to suspend execution for a period of time, allowing other threads to continue executing, but it does not release the object lock. This means that if there is a synchronized synchronization block, other threads still cannot access the shared data. Note that the method is catching an exception. Sleep () allows a low-priority thread to get the chance to execute and, of course, allow the same priority, high-priority thread to execute.

  How to synchronize:

    -synchronized tagged Methods and objects:

    This keyword is used to protect shared data, but the prerequisite is to distinguish which data is shared. Each object has a lock flag, and when a thread accesses the object, the synchronized-modified data is "locked" and blocked from being accessed by other threads. When the current thread finishes accessing this piece of data, the lock flag is released and other threads can access it.

    -wait ()/notify ()/notifyall ():

    These three methods are used to coordinate the access of multiple threads to shared data, so they must be used within the synchronized statement block. The Synchronized keyword is used to protect shared data and prevent other threads from accessing the shared data, but the process of the program is inflexible and how can other threads have access to shared data when the current thread has not exited the synchronized data block? At this point, use these three methods to control flexibly. The wait () method suspends the current thread and releases the object lock, allowing other threads to enter the synchronized data block, where the current thread is placed in the object waiting pool. When the Notify () method is called, an arbitrary thread is removed from the object's wait pool and placed in the lock flag waiting pool, and only the lock flag waits for the thread to acquire the lock flag, and notify () does not work if there are no threads in the lock flag waiting pool. Notifyall () removes all the threads waiting for that object from the object waiting pool and puts it in the lock flag waiting pool. Note that these three methods are java.lang.Object methods.

-conditions

Introduction and basic concepts of multi-threaded parallel computing serial----

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.