I have 2 queues A and B. Main thread is responsible for filling the queues. There is a threadpool of 3 threads responsible for reading from queues. Both Queues are ArrayBlockingQueue.
Lets say main thread is filling the queues in this way (A1,B1),(A2,B2),(A3,B3),(A4,B4) and so on. A1 and B1 collectively makes a whole data.
Is it possible for a any thread to read data like (A3,B4) at any point. If yes then how can i avoid it. I want every thread to read data the altogether.
e.g thread1 should read both (A1,B1) and thread2 (A2,B2) and so on.
As so often, the answer is: depends.
Of course you can implement something that would follow the outlined approach and that in the end is "correct". But: getting there will be hard.
A much more straight forward way: fix your broken design. If the two entries within the two queues are only meaningful together - then create a class that wraps around one A and one B - and put such objects into a single queue.
Related
I am kind of new at concurrency (I believe i am doing good with Monitors, Semaphores and reentrantLocks) so i can't use them.
I have to use any class or interface from concurrent package.
Point 1.- Few threads must access an array and make a sumatory of its elements (I used Atomic Variables here, there is an atomic variable used as a pointer for the array).
Point 2.- Here, a number of Threads (cars), in a shore have, to cross a river. There is a Thread that simulates a boat. When 10 cars are in the boat it sails to the other shore. This secuance is repeated until all cars crosses the river. I don't really know which ones to use here.
Point 3.- Here, some Threads have to read some information and other Threads have to modify that information (any number of times). I believe here I have to use ReadWriteLock.
Point 4.- A producer / consumer like problem. Here I used BlockingQueue.
Point 5.- Made up an exchanger like problem and solve it (done, really simple one... 2 threads exchanging a String variable).
If you have any recomendation on which method to use in a certain points (like "No, use atomic variables in point 3 and cyclicBarrier in point 1"), will help me a lot!
Thanks!
Atomic Variables
Atomic Variables are proceed with CAS algorithm and they have can be consumed by multiple threads safely :
CAS :
A typical CAS operation works on three operands:
The memory location on which to operate (M) The existing expected
value (A) of the variable The new value (B) which needs to be set The
CAS operation updates atomically the value in M to B, but only if the
existing value in M matches A, otherwise no action is taken.
You can use Exchanger which could be used to swap , exchange some
information between two threads
https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Exchanger.html
Blocking queue is consumer producer model a you mentioned , So for
using it you need to create two threads one will be Producer it will
put to the Blocking queue , consumer will consume (read) from it .
There are many implementations of BlockingQueue
Here is more
detailed information:
http://tutorials.jenkov.com/java-util-concurrent/blockingqueue.html
So from you list is all on their places.
Also you can reed something here about java.util.concurrent
I think this will be helpful :
http://tutorials.jenkov.com/java-util-concurrent/index.html
Point 2: if the thread is a boat, it should take cars on one bank of the river and unload them on the other bank. Looks like most natural representation of cars on a bank is a BlockingQueue and ArrayList for cars on the boat. Generally, always consider BlockingQueue first.
Point 3: this is usually done by synchronized methods, or ReentrantLocks.
I am working on a simple project that is going to send multiple HTTP requests to retrieve some data, parse the response from each URL, and return response that contains the original URL and some information about the data(The reason for threads usage is obviously the multiple HTTP requests).
I am wandering if there is best practice for this scenario, here are the options that pops into my mind :
1. Have each thread send an HTTP request. parse the data to get the required information and return the information itself (by a Future<SomeDataType>, or a simple DataType getInformation() call to be done after the thread is complete), then create the URL-SomeDataType pair in the original thread
2. Having each thread take an additional argument of a synchronized list/map, which the thread will add the URL-Information pair to (the same instance of the list/map will be shared across all threads).
3. Less likely option - having each thread just pull the information, and return in in either way mentioned in 1/2, than parsing all the information in the main thread (which will reduce performance but will require almost 0 synchronisation handling, which is nice)
Is there a best practice for a similar scenario?
Thanks!
In my opinion, Option 1 is the cleanest and aligns with the best practice. Preferred way to implement it would be to use the executor framework (thread-pools and Callables). Reasons for the choice -
Separation of concerns - each thread returns the results of its' work independently. After that, it's the main thread's job to take that result and process it further the way it likes (e.g. put it in a map OR merge it into something else). In the future, if you found a better/cleaner way of aggregating the results - that change would most-likely not impact what the worker threads themselves do or return.
Option 2 would involve unnecessary synchronization (although you could use ConcurrentHashMap to make it minimal). Bigger problem - it mixes the concerns among the main thread and the worker threads. The worker threads now "know" a bit about what to with the result (their concern should only be - getting the results)
Option 3, as you said, would degrade performance. If the information fetched by each thread is independent from each other, it makes sense to let each thread parse that info and then return it.
So let me give you an idea of what I'm trying to do:
I've got a program that records statistics, lots and lots of them, but it records them as they happen one at a time and puts them into an ArrayList, for example:
Please note this is an example, I'm not recording these stats, I'm just simplifying it a bit
User clicks -> Add user_click to array
User clicks -> Add user_click to array
Key press -> Add key_press to array
After each event(clicks, key presses, etc) it checks the size of the ArrayList, if it is > 150 the following happens:
A new thread is created
That thread is given a copy of the ArrayList
The original ArrayList is .clear()'ed
The new thread combines similar items so user_click would now be one item with a quantity of 2, instead of 2 items with a quantity of 1 each
The thread processes the data to a MySQL db
I would love to find a better approach to this, although this works just fine. The issue with threadpools and processing immediately is there would be literally thousands of MySQL queries per day without combining them first..
Is there a better way to accomplish this? Is my method okay?
The other thing to keep in mind is the thread where events are fired and recorded can't be slowed down so I don't really want to combine items in the main thread.
If you've got code examples that would be great, if not just an idea of a good way to do this would be awesome as-well!
For anyone interested, this project is hosted on GitHub, the main thread is here, the queue processor is here and please forgive my poor naming conventions and general code cleanliness, I'm still(always) learning!
The logic described seems pretty good, with two adjustments:
Don't copy the list and clear the original. Send the original and create a new list for future events. This eliminates the O(n) processing time of copying the entries.
Don't create a new thread each time. Events are delayed anyway, since you're collecting them, so timeliness of writing to database is not your major concern. Two choices:
Start a single thread up front, then use a BlockingQueue to send list from thread 1 to thread 2. If thread 2 is falling behind, the lists will simply accumulate in the queue until thread 2 can catch up, without delaying thread 1, and without overloading the system with too many threads.
Submit the job to a thread pool, e.g. using an Executor. This would allow multiple (but limited number of) threads to process the lists, in case processing is slower than event generation. Disadvantage is that events may be written out of order.
For the purpose of separation of concern and reusability, you should encapsulate the logic of collecting events, and sending them to thread in blocks for processing, in a separate class, rather than having that logic embedded in the event-generation code.
That way you can easily add extra features, e.g. a timeout for flushing pending events before reaching normal threshold (150), so events don't sit there too long if event generation slows down.
In our multithreaded java app, we are using LinkedBlockingDeque separate instance for each thread, assume threads (c1, c2, .... c200)
Threads T1 & T2 receive data from socket & add the object to the specific consumer's Q between c1 to c200.
Infinite loop inside the run(), which calls LinkedBlockingDeque.take()
In the load run the CPU usage for the javae.exe itself is 40%. When we sum up the other process in the system the overall CPU usage reaches 90%.
By using JavaVisualVM the run() is taking more CPU and we suspect the LinkedBlockingDeque.take()
So tried alternatives like thread.wait and notify and thread.sleep(0) but no change.
The reason why each consumer having separate Q is for two reason,
1.there might be more than one request for consumer c1 from T1 or T2
2.if we dump all req in single q, the seach time for c1 to c200 will be more and search criteria will extend.
3.and let the consumer have the separate Q to process thier requests
Trying to reduce the CPU usage and in need of your inputs...
SD
do profiling and make sure that the queue methods take relatively much CPU time. Is your message processing so simple that is compared to putting/taking to/from queue?
How many messages are processed per second? How many CPUs are there? If each CPU is processing less than 100K messages per second, then it's likely that the reason is not the access to the queues, but message handling itself.
Putting in LinkedBlockingDeque creates an instance of a helper object. And I suspect, each new message is allocated from heap, so 2 creation per message. Try to use a pool of preallocated messages and circular buffers.
200 threads is a way too many. This means, too many context switches. Try to use actor libraries and thread pools, for example, https://github.com/rfqu/df4j (yes, it's mine).
Check if http://code.google.com/p/disruptor/ would fit for your needs.
I'm making a little java game in which I would have two threads (well as the FIRST step towards multithreading...), one for the logic and one for the drawing.
So my question is: How can I make those two communicating which each other?
Requirements:
accessing variables and object from a another thread
syncing them so they each complete a same number of "loops" in the same time.
(the logic calculates and then the another one draws the results and the loop begins again...)
So how is this achievable in java?
Thanks in advance!
1. Create a Class with logic and drawing methods.
Whose object is accessible by both the threads.
2. Now please do synchronize the atomic statements or methods.
3. So its like an object is shared between 2 threads.
Methods are methods, within a thread or not. Just create an object that is visible to all of your Threads, and they'll both be able to access it.
One easy structure to use to communicate between threads is the BlockingQueue.
I often find if you use a BlockingQueue it will focus you on making the threads work together correctly. For example, they will not provide the facilities you are asking for because actually those facilities are not what you want.