Comparison of multi-process multithreaded computing methods for Python and Java

Source: Internet
Author: User
Tags thread class

Please see the original.

Comparison of multi-process multithreaded computing methods for Python and Java

One of the problems that big data has to face is parallel computing . Just like performing a task, we all work together at the same time, only to be efficient, and to get results soon. The so-called "crowds" ~

For parallel computing, there are a lot of tall concepts, I do not understand all. Here's just a list of my understanding and summary of multi-process and multithreaded computing.
In a computer, a task can be handled, either in a process, or in a thread, or, to be exact, executed by thread. When we do something, we often need to do multiple tasks at the same time to achieve the effect we want, such as watching movies, it is necessary to let the computer realize the task of "look", but also to achieve the task of "listening to". Depending on the CPU, the computer will open multiple processes, and each process can have multiple threads.


Say "multi-process" first:

In Python, it's easier to implement multiple processes. We can use multiprocessing to create processes, such as
Import multiprocessing as MPP = MP. Process (Target=run_proc, args= (' fireling ',), name= ' run_procprocess ') P.start () P.join ()

This creates a process, denoted by P, where run_proc represents a function that you run with a child process.
If you don't feel like it, you can also use a process pool to create multiple processes, involving two usages: pool-apply usage and pool-map usage, essentially the same as creating a single process.
Or to use the multiprocessing package, create a process pool first
P = MP. Pool () P.map (Run_proc, [I for I in range (m)]) P.close () P.join ()

In Java, the program is run on the JVM, a JVM takes up a process, so the concept of multi-process should not exist.


Say "multithreading":

Similar to the idea of multi-process, we can also implement the creation of threads, in Python, using the threading package implementation. For example
Import Threadingt = Threading. Thread (Target=run_thread, args= (' fireling ',), name= ' Run_threadthread ') T.start () T.join ()

This creates a thread, denoted by T, where run_thread represents a function that you run with a child thread.
However, because of multithreading tasks, there are often variables that are shared by all threads, which are called global variables, which are stored in only one copy of all threads. Therefore, multithreading tasks, especially for global variable modification, we often need to add the lock, to ensure that when a global variable is modified, only one thread touches it.
First, you declare the thread lock,
Lock = Threading. Lock ()

In the function definitions called by these threads, we can add two words:
Lock.acquire () # Gets the thread lock xxxxxxxxxxx A number of codes are omitted here Lock.release () # Release lock

Global locks are for global variables for all threads, so what if we are going to handle the local variables of a single thread? The Threadlocal method can be used.
Parallel computing in Java involves multithreading. Similarly, in a JVM process space, there are multiple stacks to record calls from multiple threads, but these threads share the objects in the heap, that is, modifications to those objects also require the lock mechanism.
There are two main ways to implement multithreading in Java: Inherit the thread class to create threads and provide the run () method, or implement the Runnable interface to create threads and provide the run () method.
If both exist, it will first look for the subclass's Run method, and if the subclass does not rewrite run, then look for the run method of the Runnable interface. The following example illustrates this conclusion, and the final output is the output of the subclass rewrite Run method.
public class Test{public static void Main (string[] args) {thread thread = new Thread (new Runnable () {@Overridepublic void R Un () {while (true) {try {Thread.Sleep],} catch (Interruptedexception e) {e.printstacktrace ();} System.out.println ("Runnable1:" + thread.currentthread (). GetName ());//system.out.println ("Runnable2:" + This.getname ());}}) {@Overridepublic void Run () {while (true) {try {Thread.Sleep],} catch (Interruptedexception e) {e.printstacktrace ();} System.out.println ("Thread1:" + thread.currentthread (). GetName ()); System.out.println ("Thread2:" + this.getname ());}}; Thread.Start ();}}

As with Python, the Java thread lock is implemented through a synchronization mechanism, which is the synchronized method. The synchronized method of the same object can be called only by one thread at a time. Other threads must wait for the thread call to end to run, excluding the possibility of multithreading while modifying global variables.
It is worth mentioning that the corresponding global variables in Python are represented by the keyword global, while Java and C/s are represented by static.


About the "co-process":

Python can implement multiple threads, but it cannot actually take full advantage of system resources because Python has a global locking mechanism, and simply, it is the same time that only one thread in a process can manipulate the data. So it is better to realize parallel effect and adopt multi-process method.
co-processSolved this problem to some extent. The mechanism of the process is that in the course of running a task, we can interrupt at any time to perform another task, or we may return to perform the old task at any time. This is useful in the network transmission, IO process, especially for two unrelated tasks, the use of the coprocessor can achieve the effect of asynchronous execution.
Java, do not know if there is no such mechanism. Although XXXX, but Java is also good ~ ~ ~ in the Tiobe programming language ranking has been firmly seated in the top spot, which, nothing but Android development to Java this advantage.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Comparison of multi-process multithreaded computing methods for Python and Java

Related Article

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.