The Java multithreaded synchronization method will also be robbed, that is, only the synchronization method when other threads to access the same instance of the synchronization method ....

Source: Internet
Author: User

Ackage cn.twj.rtti.t;//: Concurrency/atomicitytest.java

Import java.util.concurrent.*;

public class Atomicitytest implements Runnable {
private int i = 0;

public int GetValue () {
return i;
}

Setting this method as a synchronous method does not mean that other threads will wait for this method to complete after entering this method.
In a thread that does not enter this method, it will still be preempted with the current thread
It is possible to be robbed by another thread when the first sentence is executed. However, because the synchronization block ensures the visibility of the variable, it leads to the execution of the first sentence (also non-atomic operation, or the other thread has not finished executing the first sentence is snatched by other threads, and has not been self-added),
The value of I is flushed to main memory and the other thread gets the i variable 0.
This method hangs in the time that other threads execute
It is also possible to be robbed of execution after the first statement is executed, so the value of I is an odd number
So the main method returns an odd number and then exits the thread

Result 1: It is only the synchronous method that suspends direct execution when other threads are accessing the same instance's synchronization method, whereas for non-synchronous methods such as GetValue, other threads can enter when a thread executes a synchronous method of the same instance
Non-synchronous method because the method does not have a lock (this)
Result 2: The synchronization method is also robbed, except that when the same method is shared, its operation is similar to atomicity, and other threads must wait for the lock's thread to release the lock before executing
Private synchronized void Evenincrement () {
i++;
i++;
}

public void Run () {
while (true)
Evenincrement ();
}

public static void Main (string[] args) {
Executorservice exec = Executors.newcachedthreadpool ();
Atomicitytest at = new Atomicitytest ();
Exec.execute (at);
while (true) {
int val = At.getvalue ();
if (val% 2 = 0) {
System.out.println (Val);
System.exit (0);
}
}
}
}

The Java multithreaded synchronization method will also be robbed, that is, only the synchronization method when other threads to access the same instance of the synchronization method ....

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.