java-Preliminary Understanding-14th-inter-thread communication-Example

Source: Internet
Author: User

I. INTRODUCTION

Before about the basic use of threads, selling tickets and saving money. Selling tickets is tantamount to releasing the resources and getting them. To save money is to put the data in.

Now, we've changed the thread. Previously, multiple threads were performing the same action, either inherited or implemented, as a run method. In other words, it is a thread task, multithreaded at the same time executing a task. But they are stored separately in their own different thread stacks, and then run uniformly, but the action is the same, for example, four people are selling tickets.

Now you are talking about inter-thread communication. What is communication, the number of threads, but the tasks that run are different. There is a characteristic that the resources they deal with are the same.

Used to deal with the same resource, the same action, now is not.

Now let's talk about the communication between the threads.

Two. Building and problem of thread communication program

The specifics are clear, but it's a bit strange to call communication .

Let's start with a concrete example and then abstract the example into a program.

This is described in real-world statements, where name and Sex,input are responsible for constantly updating name and sex, while output is responsible for outputting the original name and sex resource. To better handle these two actions, they can switch between each other.

Now how do you turn this situation into a program?

We operate on the name and sex in resource, which is more data and encapsulates objects, so it's easy to handle.

Input at the same time, output at the same time, both operations. If output is not in time, then some of the data will be overwritten. This involves multithreaded technology. In multi-threading here, the task of input and output is not the same. Running at the same time is no problem, but the task is completely different. Tasks are different, which means that we are going to individually encapsulate them. There are two task objects, which indicates that there are two run methods, which are encapsulated in two classes ( why are the tasks individually encapsulated into classes?). Is it OK to set it in the resource class ? I got it. Two run methods cannot be in the same class. If you can, it is overloaded, how is overloading called? )。 So here, there are three classes, the resource class, the input class, and the output class.

In three classes, we first refine the output class. The output is not a single line, so the while (true) is designed, and the reason for this is the design, which will be described later. In the output, name and sex are the output, but both are other classes, so create an object to invoke.

Next, we look at the input, do not switch first, just do a simple action.

Input is also not a single time, but also multiple times. You also create objects to receive the name and sex data. But this is not appropriate, because the input and output are two resources, each with their own new Resource (); This is the problem. What we require is that the inputs and outputs are the same resource.

Some people say simply static, the variable name and sex static (I guess the static of the variable), before the sale of the ticket when discussed this issue, think is set to the object is more reliable. Just static variables are not very good.

What if the resources are different now? It is possible to turn it into a singleton. But the single case has a limitation, that is, only one object can appear, and the same as the sale of tickets, an object or 100 tickets ( I think it may be too restrictive, can only be an object ?) )

It is certain that in the input class, you cannot have a new object (to maintain the same object as the output), but also to manipulate the resource. The way to do this is to pass in the parameter, passing in the parameters.

How do you do it? Make a function pass. Set up the object outside and pass it on to the inputs and outputs, respectively. (This outside of the object, seems to have a lot of role, while taking care of two classes of operations)

There are two ways to receive a parameter, either a generic method value or a constructor. This is the task, the processing of resources, which means that the task of an object initialization is a resource, with the constructor is possible. A construction object, it needs to have something, it must be initialized in the object, just as we say the thread, the thread object is created, it must have a task. This idea comes from the thread.

Here in order to achieve the characteristics of the input, in here to implement the switch, enter two names, to achieve the interval switch, as if the input of many people name this effect.

How do you switch it?

For the two types of x clear, separate to assign values. So how do you switch x?
Some people say x%2, this can achieve 0 and 1 switch, the idea is very good, X + +. Remember, you want to change the model by 2, you have to achieve the X change, x does not change how it is modeled with 2 changes? Also write down this value. The whole loop is in the body of the while (true) method.

Now we begin to describe the main function. First of all have resources, no resources can do nothing (my understanding is to be used for transmission), Resource r=new Resource (), next to have a task, then the task object, the task also has resources to handle, input in=new input (r); Resources have, the task has, you have to have the path, thread t1=new thread (in),

Create a resource → create a task → create a thread, perform a task → open a thread. When there is a task, the resource must be clear and the task must be defined when there is a path.

Stop first, Ctrl+alt+c. There was a problem with the DOS result, and there was nan behind Lily, and behind Mike there was a female woman and woman.

There is a problem, we have to solve it, but first we have to know how to get the problem.

Three. Understanding the cause and resolution of thread communication program problems

The jargon here is called thread-safety issues.

How is the Genesis analyzed?

Does the code in the thread run have shared data? Is there more than one statement that shares data in action? Name and sex are the contents of the resource, and the resource is shared, and the problem arises in this. R is a resource, I am manipulating two properties in the resource, R.name and R.sex, and working in multiple statements, if there is, else also.

In the scramble process, input first entered the Mike Nan, the second time just input Lili, CPU execution is output taken away, is in this problem. ( does this mean to introduce synchronization ?) )

There's still a problem with the DOS result, ( I think the scope of the synchronization code block is too wide ?). )

Now is added synchronization, but did not solve the problem, is my sync add the wrong, or synchronization can not solve the problem?

How to solve the problem? When you hit the thread safety problem, you want nothing but synchronous resolution, you can add synchronization, the problem is still how to do? To consider the premise of synchronization.

The premise of the merger is a sentence, a lock inside whether there are multiple threads. In other words, whether multiple threads are in the same lock.

We look at the above, is synchronous, but synchronous code block inside a thread, why? The output thread is not in the synchronization code block, so we should ensure that the outgoing threads should also be in sync. What's the use of optical sync input when you're not syncing? What do you mean, when you enter half of the time, I can't go over to fetch.

Still have a problem, now is to put the thread in sync inside, but here is a different lock. Now we're not going to write obj. Some people say with This,this, this is two classes, and this represents this class. Some people say, with static lock, input class write input.class, Output write Output.class. It's the same as the pull.

with Input.class is possible, if with resource.class absolutely is no problem, it is unique. Here do not need to use the class bytecode file object, in this only need to ensure that the object only can be used, just the resource is the only one, both of the same resources are processed, put R on the go.

The result of DOS operation is all one kind, have the problem.

In the process of re-operation in addition to the IF,ELSE statement need to synchronize, there are statements in the operation of resources,

The output statement in the operation resource, the following sentence and if the same operation resources.

We should put the sync in there,

Why do you say that? If the synchronization is placed outside the while (true), then the thread comes in and is out of the loop.

After this change, there will be no problem.

Now the problem is gone, but the result is a large, large one. Why is it a continuous piece?

Input thread it gets to execute the right time, it does not throw once, it can execute many times, that means the input thread to get execution first put a Mike Nan, put out after, it also hold the executive right, followed by the assignment of Lili Female, which leads to the previous data is covered. And then to the value of Mike Nan, Lili, and so on, such as the assignment of the right to the output, the resources are either Mike Nan, or Lili, because it is synchronous, certainly will not appear Mike, the output of the time, it is the last assignment of data, because holding the executive right, Therefore, the output is not a single time.

java-Preliminary Understanding-14th-inter-thread communication-Example

Related Article

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.