Java-Preliminary Understanding-The 13th Chapter-Multithreading (the phenomenon of thread safety issues)

Source: Internet
Author: User
Tags thread class

I. INTRODUCTION

Then look at the issue of ticket sales, here comes a small problem.

When we were wrapping the thread task and creating the thread object, we found out that 100 tickets were actually sold through four threads. But when selling tickets, will there be some small problems? Analyze the entire program to show you.

Now there are four threads, all running in the Run method. As you all know, each thread has its own run method in the stack area, for example, they are also in the operation heap memory variables, called Num.

Assuming this num has been reduced to 1, this is a prerequisite.

In the case of Num=1, four threads thread-0,thread-1,thread-2,thread-4, all at the same time into the loop. After the Thread-0 thread is holding num=1, after judging if (num>0), the CPU is cut out to execute the IF judgment statement of the other thread. That is to say, there is no enforcement right. After four threads discriminant, the CPU returned to execute the Thread-0 thread, output the DOS information, thread-0,1, at this time num=0.

At the same time there are three threads lying down, and they are no longer judged. When thread 1 executes, the output is thread-1,0. When the thread outputs 0, it's going to be a thing. There are No. 0 votes, the same thread 3 output is thread-3,-1.

After analyzing this procedure, we find that there is a security risk. This is one of the things that we have to pay attention to when we are writing multi-threaded threads. A thread security issue.

Two.

Thread is a complete problem, multithreading is random, it will lead to a number of security risks. These security risks are very serious to us.

Some people say that our program above is not a problem when compiling and running. But that is the ideal state, and the state of our analysis just now is possible.

Now let's do the following to stop the thread from lying down a bit, that is, to sleep. Some people say that sleep is not the release of eligibility? Not afraid, sleep time is very short is OK. It has the same qualifications, no executive power and short sleep time is one thing. Because all during this time, do not run.

Thread How to sleep, you know, to the thread class to find the method.

The following sleep method is nanoseconds. Because the sleep method is static, the class name is called.

Point into the sleep method, and found that there are anomalies. This exception is an interrupt exception, which will be highlighted later.

Now that we have called a method that throws an exception, what should we do? Either declare throws, or try-catch handle it. If you can't catch it, declare it.

In the IF code block of Thread.Sleep (10); statement, encountered an exception, then we will choose to throw,

But here is not out, you can not declare, because this run is covered by the interface method, the interface does not declare an exception, so when overwriting, you can not declare the exception.

You can only try-catch, to be exact, you can only catch, not declare. This is the case that can only catch and cannot be declared.

We can't handle this anomaly at the moment, and haven't started to learn how to deal with this anomaly.

There is no processing, but the real catch inside must have to deal with, and is targeted to deal with.

So, let's see if the wrong number of tickets happened?

This results only out of-1, no out-2, just want to say this thing, their own time to run, did not see -1,-2, because to this situation, when it becomes 0, the other line friend come in, a judgment is not satisfied will not run.

Now the results will be discussed before the security problem occurred, like this problem, we do not do the following sentence, the code runs many times no problem, but as long as the occurrence of a serious. It doesn't happen all the times, but it doesn't mean it has no security problems.

Therefore, we should consider thread security when we are writing multi-thread. What is the cause of the security problem?

Java-Preliminary Understanding-The 13th Chapter-Multithreading (the phenomenon of thread safety issues)

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.