The company's needs, the current import an Excel function, the process is: read Excel data, passing a background check every piece of data to determine whether the import requirements, return to the front, import preview show. (Distal waiting for a response, difficult). Users then click the Import button, asynchronous import (front end not wait, better to do). Current data interface supports only 300, now requires me to support the 3000 data.
Problem solving, critical thinking is.
First, look at the interface, find the location of the reading table, see the judgment, if the data is greater than 300, direct return. Put 300 into 3000.
Then, import the data checksum analysis, and what are the data check, data are from the database. Each time from a database query, it certainly is slow. Even Redis query cache, but also a network consumption, increase the cache pressure. Although there are stand-alone Redis query performance 120 000 times / sec, 120 000 3000 was divided by 40, if this play, it caused the collapse of 40 individuals using the system. The same data, have to check 3000, it is not stupid? ? ? So reducing every query to the database query add Redis cache, the cache found in Redis data, create concurrency-safe container ConcurrentHashMap store data in a method, to avoid duplication of search operation, only check once a method call until the end.
Objects created inside the method, when the method call completes, push the stack, release your reference, it will free up memory. In the process of verification in 3000, Object object is in jvm memory, easy to be quickly reused, rather than the need to obtain from the database or cache again. This is the way the stack level cache, JVM cache, the local cache.
This is the most important ideas, thinking. To achieve a method, as little as possible of the query, the query results reuse.
When I'm done ConcurrentHashMap in the methods of data cache, they were tested.
Results: Supports up to 800 data import check. Distal requested more than 10 seconds, the request will timeout.
How to do it? ? ?
Product, you can not handle the demand ah. Can not be achieved ah. . . . . . Wrangling in. . . . . Wrangling invalid.
Then use multithreading optimization techniques.
1. Create a thread pool
2. Create a thread pool task for receiving the return value of an ordered set, in order to facilitate obtaining results.
3. Get the thread pool
4. Read Excel spreadsheet data, through each row, each row of data submitted to a multi-threaded task.
The traversal futureList acquisition result.
6. Thus, the combination of all the results returned. To complete the thread pool using this method of transformation.
7. At this time, a problem has emerged, 3000 data, each data has an id, how multiple threads, let the treated id not repeat, duplication can mark it? ? ?
This time I used a concurrent security Set ===> ConcurrentSkipListSet
Let's look at the source ConcurrentSkipListSet the add () method:
The above description with Google translate:
if the specified element does not already exist, add it to this collection.
More formally, the specified element {@code e} is added to this set if
the set does not contain elements {@code e2} to {@code e.equals (e2)}.
If the set already contains the element, the call will exit the set
unchanged and returns {@code false}
Description id here we go heavy usage is entirely correct.
Let us look at the source code Future of get () method:
Translation: Waits if necessary for the computation to complete, and then retrieves its result.
Wait for the completion of the required calculation, and then retrieve the result
So, Future's get () method is blocked waiting.
This, I'm done from the beginning of the data 300, 800 to 10 seconds in response to the data, optimized data 7 to 3000 second response.
To complete the task, but also improves performance.
By this time using the thread pool, Future, Callable security and concurrent container classes ConcurrentHashMap, ConcurrentSkipListSet technology,
Greatly improved my multithreaded, concurrent programming techniques. There are methods stack level data cache, JVM cache, which is a leap of thought.