Advices on concurrency JAVA










2















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!










share|improve this question



















  • 1





    A better way to approach this would be to identify what each of these concurrency primitives is intended for, for instance by reading their Javadoc. Then, you could answer not only this question, but pick the right tool for every concurrency problem you'll encounter in your career.

    – meriton
    Nov 13 '18 at 23:27











  • I been reading a lot about it but I am a bit lost. Theacher gave us this project last fryday and must be delivered on fryday 23th. And they gave us almost no information about this classes... I know I must keep reading, but anything could be very handy at this ponit hehe

    – Glexnor
    Nov 13 '18 at 23:41















2















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!










share|improve this question



















  • 1





    A better way to approach this would be to identify what each of these concurrency primitives is intended for, for instance by reading their Javadoc. Then, you could answer not only this question, but pick the right tool for every concurrency problem you'll encounter in your career.

    – meriton
    Nov 13 '18 at 23:27











  • I been reading a lot about it but I am a bit lost. Theacher gave us this project last fryday and must be delivered on fryday 23th. And they gave us almost no information about this classes... I know I must keep reading, but anything could be very handy at this ponit hehe

    – Glexnor
    Nov 13 '18 at 23:41













2












2








2


1






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!










share|improve this question
















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!







java multithreading concurrency






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 17:04







Glexnor

















asked Nov 13 '18 at 23:23









GlexnorGlexnor

112




112







  • 1





    A better way to approach this would be to identify what each of these concurrency primitives is intended for, for instance by reading their Javadoc. Then, you could answer not only this question, but pick the right tool for every concurrency problem you'll encounter in your career.

    – meriton
    Nov 13 '18 at 23:27











  • I been reading a lot about it but I am a bit lost. Theacher gave us this project last fryday and must be delivered on fryday 23th. And they gave us almost no information about this classes... I know I must keep reading, but anything could be very handy at this ponit hehe

    – Glexnor
    Nov 13 '18 at 23:41












  • 1





    A better way to approach this would be to identify what each of these concurrency primitives is intended for, for instance by reading their Javadoc. Then, you could answer not only this question, but pick the right tool for every concurrency problem you'll encounter in your career.

    – meriton
    Nov 13 '18 at 23:27











  • I been reading a lot about it but I am a bit lost. Theacher gave us this project last fryday and must be delivered on fryday 23th. And they gave us almost no information about this classes... I know I must keep reading, but anything could be very handy at this ponit hehe

    – Glexnor
    Nov 13 '18 at 23:41







1




1





A better way to approach this would be to identify what each of these concurrency primitives is intended for, for instance by reading their Javadoc. Then, you could answer not only this question, but pick the right tool for every concurrency problem you'll encounter in your career.

– meriton
Nov 13 '18 at 23:27





A better way to approach this would be to identify what each of these concurrency primitives is intended for, for instance by reading their Javadoc. Then, you could answer not only this question, but pick the right tool for every concurrency problem you'll encounter in your career.

– meriton
Nov 13 '18 at 23:27













I been reading a lot about it but I am a bit lost. Theacher gave us this project last fryday and must be delivered on fryday 23th. And they gave us almost no information about this classes... I know I must keep reading, but anything could be very handy at this ponit hehe

– Glexnor
Nov 13 '18 at 23:41





I been reading a lot about it but I am a bit lost. Theacher gave us this project last fryday and must be delivered on fryday 23th. And they gave us almost no information about this classes... I know I must keep reading, but anything could be very handy at this ponit hehe

– Glexnor
Nov 13 '18 at 23:41












2 Answers
2






active

oldest

votes


















0














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






share|improve this answer

























  • The thing is I must use those 5 classes I mention

    – Glexnor
    Nov 13 '18 at 23:38











  • So actually all is fine from my side , if you have some assumptions what to use when fill free to ask

    – Mykhailo Moskura
    Nov 14 '18 at 0:03






  • 1





    This is really helpful, thank you a lot! I'll read that jenkov.com tutorials!

    – Glexnor
    Nov 14 '18 at 0:27











  • So, the teacher send us an e-mail telling us that we can use ANY class from concurrent package.

    – Glexnor
    Nov 14 '18 at 15:59


















0














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.






share|improve this answer























  • I can't use reentrantLocks, Semaphores or Monitors. And I should try another method because I have already used BlockingQueues on the producer/consumer point.

    – Glexnor
    Nov 14 '18 at 16:53










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53290957%2fadvices-on-concurrency-java%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














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






share|improve this answer

























  • The thing is I must use those 5 classes I mention

    – Glexnor
    Nov 13 '18 at 23:38











  • So actually all is fine from my side , if you have some assumptions what to use when fill free to ask

    – Mykhailo Moskura
    Nov 14 '18 at 0:03






  • 1





    This is really helpful, thank you a lot! I'll read that jenkov.com tutorials!

    – Glexnor
    Nov 14 '18 at 0:27











  • So, the teacher send us an e-mail telling us that we can use ANY class from concurrent package.

    – Glexnor
    Nov 14 '18 at 15:59















0














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






share|improve this answer

























  • The thing is I must use those 5 classes I mention

    – Glexnor
    Nov 13 '18 at 23:38











  • So actually all is fine from my side , if you have some assumptions what to use when fill free to ask

    – Mykhailo Moskura
    Nov 14 '18 at 0:03






  • 1





    This is really helpful, thank you a lot! I'll read that jenkov.com tutorials!

    – Glexnor
    Nov 14 '18 at 0:27











  • So, the teacher send us an e-mail telling us that we can use ANY class from concurrent package.

    – Glexnor
    Nov 14 '18 at 15:59













0












0








0







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






share|improve this answer















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







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 14 '18 at 0:07

























answered Nov 13 '18 at 23:31









Mykhailo MoskuraMykhailo Moskura

838113




838113












  • The thing is I must use those 5 classes I mention

    – Glexnor
    Nov 13 '18 at 23:38











  • So actually all is fine from my side , if you have some assumptions what to use when fill free to ask

    – Mykhailo Moskura
    Nov 14 '18 at 0:03






  • 1





    This is really helpful, thank you a lot! I'll read that jenkov.com tutorials!

    – Glexnor
    Nov 14 '18 at 0:27











  • So, the teacher send us an e-mail telling us that we can use ANY class from concurrent package.

    – Glexnor
    Nov 14 '18 at 15:59

















  • The thing is I must use those 5 classes I mention

    – Glexnor
    Nov 13 '18 at 23:38











  • So actually all is fine from my side , if you have some assumptions what to use when fill free to ask

    – Mykhailo Moskura
    Nov 14 '18 at 0:03






  • 1





    This is really helpful, thank you a lot! I'll read that jenkov.com tutorials!

    – Glexnor
    Nov 14 '18 at 0:27











  • So, the teacher send us an e-mail telling us that we can use ANY class from concurrent package.

    – Glexnor
    Nov 14 '18 at 15:59
















The thing is I must use those 5 classes I mention

– Glexnor
Nov 13 '18 at 23:38





The thing is I must use those 5 classes I mention

– Glexnor
Nov 13 '18 at 23:38













So actually all is fine from my side , if you have some assumptions what to use when fill free to ask

– Mykhailo Moskura
Nov 14 '18 at 0:03





So actually all is fine from my side , if you have some assumptions what to use when fill free to ask

– Mykhailo Moskura
Nov 14 '18 at 0:03




1




1





This is really helpful, thank you a lot! I'll read that jenkov.com tutorials!

– Glexnor
Nov 14 '18 at 0:27





This is really helpful, thank you a lot! I'll read that jenkov.com tutorials!

– Glexnor
Nov 14 '18 at 0:27













So, the teacher send us an e-mail telling us that we can use ANY class from concurrent package.

– Glexnor
Nov 14 '18 at 15:59





So, the teacher send us an e-mail telling us that we can use ANY class from concurrent package.

– Glexnor
Nov 14 '18 at 15:59













0














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.






share|improve this answer























  • I can't use reentrantLocks, Semaphores or Monitors. And I should try another method because I have already used BlockingQueues on the producer/consumer point.

    – Glexnor
    Nov 14 '18 at 16:53















0














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.






share|improve this answer























  • I can't use reentrantLocks, Semaphores or Monitors. And I should try another method because I have already used BlockingQueues on the producer/consumer point.

    – Glexnor
    Nov 14 '18 at 16:53













0












0








0







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.






share|improve this answer













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.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 14 '18 at 5:33









Alexei KaigorodovAlexei Kaigorodov

10k11029




10k11029












  • I can't use reentrantLocks, Semaphores or Monitors. And I should try another method because I have already used BlockingQueues on the producer/consumer point.

    – Glexnor
    Nov 14 '18 at 16:53

















  • I can't use reentrantLocks, Semaphores or Monitors. And I should try another method because I have already used BlockingQueues on the producer/consumer point.

    – Glexnor
    Nov 14 '18 at 16:53
















I can't use reentrantLocks, Semaphores or Monitors. And I should try another method because I have already used BlockingQueues on the producer/consumer point.

– Glexnor
Nov 14 '18 at 16:53





I can't use reentrantLocks, Semaphores or Monitors. And I should try another method because I have already used BlockingQueues on the producer/consumer point.

– Glexnor
Nov 14 '18 at 16:53

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53290957%2fadvices-on-concurrency-java%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

Museum of Modern and Contemporary Art of Trento and Rovereto