Take bank withdrawal as an example to simulate the complete code of Java Multithread synchronization

Briefly understand the differences between processes and threads in the operating system:

Process: each process has an independent code and data space (process context). Switching between processes will have a large overhead. A process contains 1-N threads. (process is the smallest unit of resource allocation)

Threads: the same kind of threads share code and data space. Each thread has an independent running stack and program counter (PC), and the thread switching overhead is small. (threads are the smallest unit of CPU scheduling)

Like a process, a thread is divided into five stages: create, ready, run, block and terminate.

Multi process means that the operating system can run multiple tasks (programs) at the same time.

Multithreading means that there are multiple sequential streams executing in the same program. First, the operation of saving and withdrawing money should be thread operation, which can have many customers. This means that there must be multiple threads. Multiple threads operate a bank together, and the amount of the bank needs to be synchronized. To ensure thread safety.

So, let's put the example of this code here. If there is something wrong, please point it out. Because an old fellow asked the code for multithreading.

The first is the creation of the bank object model.

There are two methods of saving and fetching in the code. These two methods, as well as a total amount, contain some commented out code, which is easy to understand and easy to understand. Multi threads are locked and mutually exclusive to ensure synchronization between threads.

However, this is an uncommon method. The common method is to use the keyword synchronized to modify the synchronization method.

Model of customer object

Customer object, because many customers can access a bank at the same time, the operation of saving and withdrawing money is realized by threads.

Property is passed to the constructor.

main method

The actual running effect of the above code is shown in the figure below.

If the number of times to access money is small, you may see that the two threads have a sequence. Therefore, let's count more times. Then, you can see the situation as shown in the figure. Thread 1 withdraws money and thread 0 saves money. You can see that the two threads execute alternately. There are withdrawals and no rules.

This ensures data synchronization.

As for how to be out of sync, that is, abnormal phenomena,

You can remove the synchronized keyword of the add method, reduce the number of times to 3 times, and set the initial value of sum to 0 You try the code again,

You will find the so-called asynchronous phenomenon.

On the right side of the figure above is the result of out of sync. Two people save 100 each time and save it three times. Is the total amount 100, 200, 300, 400, 500, 600 It takes a long time.

However, the running result is not,

At this time, if you add synchronized to the add method, the result of the figure on the left will appear. This is the correct result.

I added another method because I wanted to have something to take. The code will look like the above.

Almost all are examples of synchronization between threads.

I'll simply record the code. When you use it, you can take it out in minutes.

summary

The above is all about the complete code of simulating Java Multithread synchronization by taking bank withdrawal as an example. I hope it will be helpful to you. Interested friends can continue to refer to this website:

Java multithreaded programming example

Principle and implementation of Java Multithread timer

Java understands multithreading by selling tickets

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>