Java concurrency and synchronization, Java concurrency Synchronization

Source: Internet
Author: User

Java concurrency and synchronization, Java concurrency Synchronization

In Java, the form of concurrency is nothing more than multi-thread and multi-process. Both forms can use multiple cores to exert computing power.

Concurrency first:

Multi-process means running multiple JVMs at the same time. The cost is usually higher than that of multithreading. Each JVM has its own stack and needs to load its own classes separately. However, the advantage of multi-process is that the isolation is better. A failure occurs when a program in the JVM runs and does not have a great impact on other JVMs. In the case of multiple threads, some fatal errors may cause the entire JVM to crash, causing the overall disaster. Multithreading can easily share the content in the memory, which is convenient but sometimes can cause confusion. Be careful.

Synchronization:

It is very easy to implement synchronization between Java threads. synchronized can be used for pre-implementation, and the locks provided under java. util. concurrent. locks can be used for non-improvement. It should be noted that the locking domain or object is correct, and wait () and other operations will release the synchronized lock.

The synchronization between processes is more difficult, because there is no mechanism such as synchronized between processes to ensure that a program segment or an object is executed and accessed in serial mode. When a process is executing, there is no way to advance what will happen between executing the current command and executing the next command. For example, you have created a file:

File file = new File("123");if (file.exists())   // do something on the file

It seems that you have checked that the file already exists and then performed some read/write operations. But in fact, when the exists () method returns true, the file may be deleted or renamed.

This situation exists for many resources outside the process. This requires mechanisms such as file locks to ensure proper resource sharing. You can use the operating system API or the APIS provided by jdk, for example:

Http://www.concretepage.com/java/example-filelock-java-nio-channels

Https://docs.oracle.com/javase/7/docs/api/java/nio/channels/FileLock.html

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.