Does Java share global variables among multiple processes?
This article focuses on "Java multi-process global variable sharing", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "Java multi-process global variable sharing?" it!
Preface
Question: is global variable sharing among multiple processes?
Import multiprocessing as mul_p
Import time
Egg1 = 1
Def write (egg2, Q):
Global egg1
Print ("write Global variable Egg [% s]..."% egg1)
Print ("write Egg [% s]..."% egg2)
Egg1-= 1
Print ("write Global variable Egg [% s]... original Egg [1]"% egg1)
# put the value of the modified egg 1 into the queue
Q.put (egg1)
Def read (egg2, Q):
Global egg1
Print ("read Global variable Egg [% s]..."% egg1)
Print ("read Egg [% s]..."% egg2)
While True:
# extract the value of the global variable Egg 1 in the p1 child process from the queue
Egg1 = q.get ()
Print ("value of global variable egg [1] in write received by read:% d"% egg1)
If q.empty ():
Print ("received...")
Break
Def main ():
# suppose even a process needs to print the following egg 2
Egg2 = 2
# ① create a queue. If you don't need to fill it, the queue can be very large, but there is a limit. Let's not consider it.
# if the number is x, the queue can store x data
Q = mul_p.Queue ()
# ② creates two process objects
P1 = mul_p.Process (target=write, args= (egg2, Q,))
P2 = mul_p.Process (target=read, args= (egg2, Q,))
# ③ makes the two child processes work
P1.start ()
# Let the main process rest for 1 second and let the p1 child process finish first, otherwise the two child processes will scramble to execute the printout.
Time.sleep (1)
P2.start ()
If _ name__ = = "_ _ main__":
Main ()
Running result: write global variable color egg [1].
Write eggs [2].
Write global variable color egg [0]. The original egg [1]
Read global variable color egg [1].
Read eggs [2].
The value of the global variable color egg [1] received by read in write: 0
Reception complete. II. Summary
The values of global variables between ① processes are not shared.
Of
Processes: this is because each process created will copy a copy of the original code (global variable or initial value) for its own use, so the code between processes is the same, but the variables and data are independent.
② processes can pass variables, lists, and string values (including global variable values) through queues created by Queue.
③ parameters in each process task, except global variables (function local variables), the rest of the parameters need to be passed into the internal parameters through external arguments. Queue Q of the previous routine, in particular, should be passed to two processes as arguments, so that communication between the two processes can be realized.
The relationship and difference between ③ process and thread: ① process contains multiple threads ② inter-process does not share variables and resources; shared variables and resources between threads ③ use time.sleep (), you can stop the current process and let other processes start working
At this point, I believe you have a deeper understanding of "global variable sharing among Java multi-processes". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!