Java understands multithreading by selling tickets
Take ticket selling as an example to introduce multithreading and resource sharing. Let's take ticket selling as an example.
Ticket selling is a process that includes a series of actions, including various operations, such as ticket query, money collection, money counting, ticket issuing, etc. one operation is to subtract 1 from the total number of tickets each time you sell one ticket. There are 10 tickets. If a person sells tickets, first check tickets, collect money, count money and other operations, and then subtract 1 from the total number of tickets, the efficiency is very low. If more than one person sells tickets, everyone does the same operation, counting and checking the money, and finally reducing the total number of votes by 1, which is efficient. However, there is a problem. If two people reduce the total number of votes by 1 at the same time, for example, a and b read that the total number of votes is 10 at the same time, a subtracts 1 from it, and B also subtracts 1 from it. The total number is 9. In fact, there are only 8 votes. Cause data error.
According to normal logic, only one person is allowed to subtract 1 from the total number of votes at the same time. In the process of a reading the total number of votes and then subtracting 1, B must wait until a completes the operation. In fact, tickets are shared resources, which can only be accessed by one person at a time. Here we need to use the synchronization mechanism, that is, the lock mechanism. Using the keyword synchronized will read the total number of votes and subtract the operation lock of 1, so that only one person can access it at a time. Each conductor is a thread, and multiple conductors carry out the same ticket selling task.
The principle of synchronized is that the object lock must be required when executing the synchronized part of the code, and an object has only one lock. Only when the lock is released after executing the synchronized code can other threads obtain the lock, so it ensures that only one thread accesses the synchronized code at the same time. The key to resource sharing is that there is only one instance. Synchronized uses the same lock. Use the instance lock or define an instance. This requires the implementation of the runnable interface to implement multithreading, so that an instance is passed in. In the way of inheriting thread, multiple instances are passed in, and each instance has a lock, so control cannot be realized.
The specific codes are as follows:
Here is the call:
Output:
This is the case when multiple threads complete the same task, that is, multiple threads call the same instance through the implementation of the runable interface. Multiple threads can do other things in this task asynchronously, but the access to shared resources can only be operated in a synchronous manner, that is, shared resources can be accessed one by one, and other resources can be accessed in parallel.
Another way to implement multithreading is to inherit thread. When calling, multiple instances need to be passed. This is the case of multiple threads and multiple instances. Each thread processes an instance independently, and each thread cannot share resources.
summary
The above is all about understanding multithreading through ticket selling examples. I hope it will be helpful to you.