Java multithreaded programming example

I Relevant knowledge:

Knowledge of Java multithreading programming:

(1) Operate on the same quantity

(2) Operate on the same object

(3) Callback method usage

(4) Thread synchronization, deadlock problem

(5) Thread communication

wait

2、 Example 1: three ticket windows sell 20 tickets at the same time;

Program analysis:

1. The same static value shall be used for the number of votes

2. To ensure that the same number of votes will not be sold, Java Multithread synchronization lock is required.

Design idea:

1. Create a platform class station, inherit the thread, override the run method, and execute the ticket selling operation in the run method! Synchronous lock shall be used for ticket sales: when one platform sells this ticket, other platforms have to wait for this ticket to be sold out!

2. Create main method calling class

(1) Create a platform class and inherit thread

(2) Create main method call class

Program running results:

You can see that the number of votes is not wrong!

3、 Example 2: two people AB withdraw money at the counter through an account a and B withdraw money at the ATM!

Program analysis: the amount of money should be set as a static variable. The same object value to be taken by two people

(1) Create a bank class

(2) Create a persona class

(3) Create a personb class

(4) Create the calling class of the main method

Operation results:

You can see that it stops running after fetching.

4、 Example 3: Tortoise rabbit race

Tortoise and rabbit race: 20m / / all distances are shortened in order to see the effect

requirement:

1. The rabbit runs at a speed of 0.5 meters per second and rests for 10 seconds every 2 meters,

2. The tortoise runs 0.1M per second without rest

3. One of them ran to the end and the other didn't run!

Program design idea:

1. Create an animal class, inherit thread, write a running abstract method, rewrite the run method, and call the running method in the run method.

2. Create rabbit class and tortoise class and inherit animal class

3. Two subclasses override the running method

4. The third requirement of this topic involves thread callback. You need to create a callback interface and a callback object in the animal class

(1) Create an animal class

(2) Create rabbit rabbit class

(3) Create the tortoise class

(4) Create a class to stop the animal thread, and implement the callback interface here

(5) Create a main method call class,

Operation results:

You can see that the rabbit won.

Generally speaking, the rabbit wins in the final output,

However, since the thread is always executing, the following occurs:

"The rabbit ran 0.5 meters and was still 0 meters away from the finish line",

And "rabbit won" has been output.

5、 Example 4:

In a KFC, waiters are responsible for producing food and consumers are responsible for consuming food;

When the production reaches a certain amount, you can have a rest until you consume the food, and then produce it immediately, which has been circulating

Contents involved in the procedure:

1. This is designed to the idea of Java pattern: producer consumer pattern

2. Ensure the unity of operation objects, that is, consumers and service providers are related to the same KFC, and KFC can only be new once

3.this. notifyAll(); And this wait(); One is the meaning of all awakening, the other is to let yourself wait;

For example, in this topic, after the producer completes production, first wake up all (including consumers and producers), and then let all themselves (producers) wait

At this time, consumers begin to consume until there are not enough ingredients. First wake up all (including consumers and producers), and then let all themselves (consumers) wait

A loop that always performs the above operations

4. Both producers and consumers should inherit thread to realize multi-threaded startup

Steps and ideas of program design:

1. Create a food class with a method to store / obtain the name of food

2. Create a KFC class with methods for producing and consuming food

3. Create a customer class customer, inherit the thread, override the run method, and consume food in the run method

4. Create a waiter class waiter, inherit the thread, override the run method, and carry out the operation of producing food in the run method

5. Create the calling class of the main method

(1) Create a food class

(2) Create a KFC class

(3) Create a customer class customer

(4) Create a waiter class waiter

(5) Create the calling class of the main method

6、 Example 5: four thread objects are designed to operate on the same data

Two threads perform subtraction and two threads perform addition.

Program analysis:

1. Create a threadadsub class, inherit the thread, and override the run method

2. Add and subtract in the run method, and sleep for 1 second after each operation

3. Create main method calling class

(1) Create a threadaddsub class

(2) Create main method call class

Thread example summary:

Code block locking is an important means to prevent data errors.

The unity of objects is very important, which requires the introduction of objects,

The object to be operated can only be new once. Other operations are performed on the incoming object,

To ensure data consistency, integrity and correctness.

Exercise topic:

1. (multithreading) the code realizes the scene of buying tickets at the same time in four ticket windows of the railway station, and the output example is as follows:

Window 1 selling tickets

Window 2 selling tickets

Window 1 selling tickets

...

2. (thread synchronization) the code realizes the code logic that four windows of the railway station sell 100 tickets at the same time. The same window cannot sell the same ticket

Tickets.

3. (thread communication) Xiao Ming plans to withdraw money from the ATM and finds that there is no money on the card. At this time, he tells his mother to save money, mom

After saving the money, tell Xiao Ming that he can withdraw the money after saving it. (PS: Xiao Ming withdraws money several times, taking 100 each time. When he finds that the money is not enough

100, just wait for his mother to save money. Xiaoming saves 2000 every time. When he finds that the money is less than 100, he saves money, and

Inform Xiaoming to withdraw the money. When it is more than 100, wait for Xiaoming to deposit again if the money is not enough)

4. (thread synchronization) four thread objects are designed to operate on the same data. Two threads perform subtraction and two threads perform subtraction

Add operation.

5. (thread communication) make two thread objects. It is required to run the first thread twice in the way of synchronization block, and then

Block up, wake up the second thread, run the second thread twice, and then block yourself to wake up the first thread

Process... Two threads execute alternately.

6. (thread synchronization) four threads are designed, in which two threads increase 1 for J each time and the other two threads decrease 1 for J each time.

7. (thread communication) the sub thread loops 10 times, then the main thread loops 100, and then returns to the sub thread loop 10 times, and then

Return to the main thread and cycle 100 again, so cycle 50 times.

summary

The above is all about Java multithreaded programming examples in this article. I hope it will be helpful for you to learn Java. Interested friends can continue to refer to this site: talking about the advantages and code examples of Java multithreading, detailed code explanation of Java multithreaded thread communication producer consumer mode and waiting wake-up mechanism, sample code of socket communication realized by Java multithreaded programming, etc, If you have any questions, you can leave a message at any time. Xiaobian will reply to you in time. Thank you for your support!

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
分享
二维码
< <上一篇
下一篇>>