Synchronized and interprocess communication

Source: Internet
Author: User
Tags message queue posix semaphore

About Java multithreaded synchronization

Java multithreaded synchronization relies on several methods and keywords

1 Wait Method:

This method is a method of object, and the Wait method is used to stop the thread that is currently calling the part of the Wait method (the block of code) from executing and to release the lock that is currently obtained by calling the code block where wait is located. and revert to the competitive lock state when another thread calls the Notify or Notifyall method (resumes execution once the lock is acquired).

There are several points to note when calling the Wait method:

1th: Wait is called when it must be in a block of code that has a lock (that is, synchronized-modified).

2nd: After resuming execution, execution starts from the next statement of wait, so the wait method should always be called in the while loop to avoid conditions that continue to execute after resuming execution without satisfying the condition.

3rd: If the wait method parameter takes time, the thread will also be activated to a competitive state, except that notify and Notifyall are called to enter the lock competition in the wait state (waiting state), interrupt it in other threads, or the parameter time is up.

4th: The Wait method is called by the thread that must obtain a lock that was freed before execution to wait to regain its execution.

2 Notify methods and Notifyall methods:

The Notify method notifies the wait method, but a thread that has not yet been activated enters the thread dispatch queue (that is, enters the lock race), noting that it is not executed immediately. and specifically which thread is not guaranteed. Another point is that the thread that is awakened must be waiting for the lock to be released by wait.

The Notifyall method wakes all calls to the wait method, and processes that have not yet been activated enter the race queue.

3 Synchronized Keywords:

1th: When the synchronized is used to identify a common method, it means that a thread is going to execute the method and must obtain the lock of the object on which the method resides.

2nd: When the synchronized is used to identify a static method, it means that a thread executes the method and must obtain the class lock of the class in which the method resides.

3rd: Synchronized modifies a block of code. Similar to this: synchronized (obj) {//code ...}. Indicates that a thread must obtain a lock on obj to execute the code block. The purpose of this is to reduce the granularity of the lock and ensure that the entire object is not locked when the lock required by the different blocks does not conflict. It is very economical to make obj with a byte array object of 0 length.

4 Atomic Action (Atomic operation):

In Java, the following two-point operation is an atomic operation. But that's not true in C and C + +.

1th: Read and write to reference variables and raw data type variables other than long and double.

2nd: Read and write to all variables that are declared volatile (including long and double).

In addition, the java.util.concurrent and java.util.concurrent.atomic packages provide some thread-safe classes and methods that do not rely on synchronization mechanisms.

Appendix:

Inter-process communication

interprocess communication (ipc,inter-process communication), which is a technique or method that transmits data or signals between at least two processes or threads. A thread is the smallest unit of resources allocated by a computer system. Each process has its own part of a separate system resource that is isolated from each other. Inter-process communication is achieved in order to enable different processes to access resources and coordinate work with each other. These processes can run on the same computer or on different computers that are connected to the network.

Interprocess communication technologies include messaging, synchronization, shared memory, and remote procedure calls. IPC is a standard UNIX communication mechanism.

The main IPC methods are

(1) Pipes: Pipelines can be used to communicate between affinity processes, allowing one process and another to communicate with a process that has a common ancestor to it.

(2) Named pipe (named pipe): The named pipe overcomes the limitation of the pipe without name, so that, in addition to having the functionality that the pipeline has, it allows for inter-process communication without affinity. Named pipes have corresponding file names in the file system. Named pipes are created by command Mkfifo or by system call Mkfifo.

(3) signal (Signal): signal is a more complex mode of communication, used to inform the receiving process of an event occurred, in addition to inter-process communication, the process can also send signals to the process itself; Linux in addition to supporting the UNIX early signal semantic function Sigal outside, It also supports the signal function sigaction that is semantically compliant with the POSIX.1 standard (in fact, the function is based on BSD, BSD in order to achieve a reliable signal mechanism, but also can unify the external interface, with the Sigaction function to re-implement the signal function).

(4) Message queue: Message Queuing is a linked table of messages, including POSIX Message Queuing system V Message Queuing. A process with sufficient permissions can add messages to the queue, and a process that is given Read permission can read the messages in the queue. Message queue overcomes the lack of signal carrying information, the pipeline can only carry the unformatted byte stream and the buffer size is limited.

(5) Shared memory: Allows multiple processes to access the same piece of memory space, is the fastest available IPC form. is designed for inefficient operation of other communication mechanisms. It is often used in conjunction with other communication mechanisms, such as semaphores, to achieve synchronization and mutual exclusion between processes.

(6) Memory mapping (mapped memories): Memory mapping allows any number of interprocess communication, and each process that uses the mechanism implements it by mapping a shared file to its own process address space.

(7) Semaphore (semaphore): primarily as a means of synchronization between processes and between different threads of the same process.

(8) Socket: A more general inter-process communication mechanism that can be used for inter-process communication between different machines. Originally developed by the BSD branch of the UNIX system, it can now be ported to other Unix-like systems: both Linux and System V variants support sockets.

Reference: http://blog.sina.com.cn/s/blog_57609a2a0100bcdv.html

Reference: http://lanvis.blog.163.com/blog/static/26982162009798422547/

Reprint please specify source: [http://www.cnblogs.com/dennisit/archive/2013/02/25/2931573.html]

Synchronized and interprocess communication (RPM)

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.