Multi-threaded synchronization mechanism to lock the resources, so that at the same time, only one thread can operate, synchronous to solve the problem that can occur when multiple threads concurrently access.The synchronization mechanism can be implemented using the SYNCHRONIZED keyword.When the Synchronized keyword modifies a method, the method is called a synchronous method.When the
void Run ()
{
while (true)
{
Synchronized (str)
{
if (num>0)
{
Try
{
Thread.Sleep (10);
}
catch (Exception e)
{
E.getmessage ();
}
System.out.println (Thread.CurrentThread (). GetName () + "This is" + num--);
}
}
}
}
}
In the above example in order to create a time difference, that is, the chance of error, the use of Thread.Sleep Java f
In Java, the Synchronized keyword is used to control thread synchronization, that is, in a multithreaded environment, control synchronized code snippets are not executed concurrently by multiple threads. Synchronized can either be added to a piece of code or added to a method.The point is, don't think it's all right to
not synchronous at all. Let's talk a little more: ThreadLocal and Synchonized are both used to solve multi-thread concurrent access. However, ThreadLocal is essentially different from synchronized. synchronized uses a lock mechanism to allow a variable or code block to be accessed by only one thread at a time. ThreadLocal provides a copy of the variable for each thread, so that each thread does not access
main (string[] args) {Example Example = new Example ();
Thread T1 = new Thread1 (example);
Thread t2 = new Thread2 (example);
T1.start ();
T2.start ();
Class Example {public
A keyword in the Java language that, when used to modify a method or a block of code, ensures that at most one thread at the same time executes that segment of code.
First, when two concurrent threads access the synchronized (this) synchronized code block in the same object objects, only one thread can be executed at a time. Another thread must wait until the cu
. For example, P1. the visible synchronization method essentially acts synchronized on the object reference. -- the thread that obtains the P1 object lock can call the synchronization method of P1. For P2, the P1 lock has nothing to do with it, in this case, the program may also get rid of the control of the synchronization mechanism, resulting in data confusion :(
2. Synchronization block. The sample code
area before the lock is released) obtains data from main memory and brushes the master after releasing the lock.order (Mutex):Guarantees that the critical area code threads are mutually exclusive. synchronized the basics of implementing synchronization:
synchronized implements the lock mechanism through object Header (Markwork). Each object in Java can be used a
(); Thread T1 = new Thread1 (example); Thread t2 = new Thread2 (example); T1.start (); T2.start (); }}class example{public synchronized Void execute () {for (int i = 0; i If you remove the Synchronized keyword, the two methods are execute
introductory books on Java programming have used this approach in their multithreaded chapters, using it entirely Lock as an example, only to synchronized as history. But I think it's too much of a good thing to do.And don't abandon synchronized.Although ReentrantLock it is a very moving implementation, it has some important advantages relative to
when syncing, ReentrantLock don't worry about it.Figure 3 and Figure 4 contain the same data as in Figure 1 and Figure 2, just add a dataset for random number datum detection, this time using a fair lock instead of the default negotiation lock. As you can see, fairness comes at a price. If you need fairness, you have to pay the price, but please do not use it as your default choice.
Good everywhere?It seems to be better ReentrantLock in synchronized
"Multi-threading and multi-process (1)--talking about threads and processes from the perspective of an operating system" describes in detail the threads, process relationships, and performance in the operating system, which must be understood as a basis for multithreaded learning. This article goes on to talk about an important conceptual synchronized in Java thread synchronization.Synchronized is a key wor
syncing, ReentrantLock don't worry about it.Figure 3 and Figure 4 contain the same data as in Figure 1 and figure 2, just add a dataset for random number datum detection, this time using a fair lock instead of the default negotiation lock. As you can see, fairness comes at a price. If you need fairness, you have to pay the price, but please do not use it as your default choice.Good everywhere?It seems to be better ReentrantLock in synchronized every
don't mind this when syncing, ReentrantLock don't worry about it.Figure 3 and Figure 4 contain the same data as in Figure 1 and Figure 2, just add a dataset for random number datum detection, this time using a fair lock instead of the default negotiation lock. As you can see, fairness comes at a price. If you need fairness, you have to pay the price, but please do not use it as your default choice.
Good everywhere?It seems to be better ReentrantLock in
about it.Figure 3 and Figure 4 contain the same data as in Figure 1 and Figure 2, just add a dataset for random number datum detection, this time using a fair lock instead of the default negotiation lock. As you can see, fairness comes at a price. If you need fairness, you have to pay the price, but please do not use it as your default choice.
Good everywhere?It seems to be better ReentrantLock in synchronized every way--all
object, as well as the method being executed, without having to gain access to the method again.
We can protect the access of blocks of code (not the entire method) by synchronized keywords. This should take advantage of the Synchronized keyword: The remainder of the method is kept outside the synchronized code block for better performance. Access to a critical
synchronized (this) access to the synchronization code block will be blocked.
4. The third example also applies to other synchronous code blocks. That is to say, when a thread accesses a synchronized (this) synchronization code block of an object, it obtains the object lock of this object. As a result, access by other threads to all the
Original link:Http://www.cnblogs.com/dolphin0520/p/3923737.htmlAlthough multi-threading programming greatly improves efficiency, it also poses some pitfalls. For example, two threads that insert non-duplicated data into a database table can cause the same data to be inserted in the database. Today we come together to discuss thread safety issues and what mechanisms are available in Java to address threading
Java Concurrency Programming: synchronizedAlthough multi-threading programming greatly improves efficiency, it also poses some pitfalls. For example, two threads that insert non-duplicated data into a database table can cause the same data to be inserted in the database. Today we come together to discuss thread safety issues and what mechanisms are available in Java
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.