Multi threading and Concurrency

Multithreaded

Describes a program that is designed to have parts of its code execute concurrently.

Spawning threads for asynchronous tasks

And let’s say if I want to perform a time consuming task in my Servlet’s doGet() method asynchronously, I start a new thread using Java executors so that lengthy calculations are done in a separate thread and response is sent right away.

Now does that ensure that I have freed the thread which had been processing my HttpServletRequest or is it still being used because a child thread is still running?

Creating a thread inside another thread doesn’t establish any special relationship, and the whole point of doing so in most cases is to let the one thread do more work or terminate while the other thread continues working. In your scenario, using a different thread to do work required by a request will, as you expect, allow the response to be sent immediately. The thread used to serve that request will also be immediately available for another request, regardless of how long your other thread takes to complete. This is pretty much the way of doing asynchronous work in a thread-per-request servlet container.

Caveats

  1. Be careful. The bean scopes session and request do not work in threads spawned! Also other code beased on ThreadLocal does not work out of the box, you need to transfer the values to the spawned threads by yourself.

This does not apply to the new way of doing things with containers. But if it is a really old app using EE containers, take note of the following points.

  1. If you’re in a full Java EE container, threads may be managed for you in a way that makes it a bad idea to spawn your own. In that case, you’re better off asking the container for a thread, but the general principles are the same.

volatile

A Java keyword used in variable declarations that specifies that the variable is modified asynchronously by concurrently running threads.

What mini-project/assignment would you suggest for better understanding of multithreading?

Barry Rountree · December 10, 2017 - HPC is what I do for a living

Consider nanopond (https://github.com/adamierymenko/nanopond)

. It’s a really cool artificial life/evolution simulator. Nice visuals.

The 2.0 version adds multithreading. Does the existing implementation prevent race conditions? Run a single-threaded version for some number of timesteps, and then run a multithreaded version for the same number of timesteps using the same random number seed. Do the results agree? What changes would you need to make to get identical results for serial and parallel versions? “Strong scaling” refers to increasing parallelism for the same problem, while “weak scaling” refers to keeping the problem size the same for each thread — more parallelism allows solving larger problems. How well does your implementation in #2 scale in both the strong and weak sense? What bottlenecks do you run into? Several cloud providers allow renting compute nodes by the hour at very reasonable rates. How well does your implementation scale on compute nodes with much larger numbers of cores? If you were to relax the constraint of “bitwise reproducibility”, how much more performance could you gain? How does relaxing that constraint change the behavior of the simulation? (Given a set of statistics from a race-free and racy implementation, can you tell which is which?) “Overcommitting” refers to creating more threads than there are cores to run them. If you enable hyperthreading on your processor, does overcommitting lead to higher performance? Is there any performance benefit from using hyperthreads? Is there any performance benefit from creating more threads than hyperthreads per core x number of cores? How much performance is lost from choosing the next location at random? How would you use a multithreaded implementation to hide that memory latency? Do the characteristics of the application change with non-random selection (which should give much, much better cache behavior)?

Now that you’ve mastered multithreading on a tiny program, have a look at ReSCAL (http://www.ipgp.fr/~rozier/rescal/rescal.html) .

What’s the best way to implement multi threading in Java aside the usual Runnable interface or Thread base class?

Vladislav Zorov · July 16, 2015 programming enthusiast.

I think it depends on why you need it:

If you just want to apply some transformation to lots of data, parallelStream in Java 8 is your friend - Parallelism (The Java™ Tutorials > Collections > Aggregate Operations) (https://docs.oracle.com/javase/tutorial/collections/streams/parallelism.html)

If you want to have lots of different tasks in-flight, i.e. to speed up some GUI application, you can use futures (JVM concurrency: Java and Scala concurrency basics) or the new, better futures in Java 8 (JVM concurrency: Java 8 concurrency basics - https://www.ibm.com/developerworks/library/j-jvmc2/index.html). The first link explains the differences to manual threads.

If you just want lots of asynchrony, i.e. for building networked GUI apps, there’s FRP and RxJava (Grokking RxJava, Part 1: The Basics - https://blog.danlew.net/2014/09/15/grokking-rxjava-part-1/). There’s also experimental support for parallel observables here - ReactiveX/RxJavaParallel (https://github.com/ReactiveX/RxJavaParallel).

Reading material

KNOWLEDGE GAP - LEARN MORE, IMPLEMENT THIS

https://jenkov.com/tutorials/java-concurrency/producer-consumer.html

https://www.baeldung.com/java-concurrency

https://walivi.wordpress.com/2013/08/24/concurrency-in-java-a-beginners-introduction/

https://www.javacodegeeks.com/2013/01/java-thread-pool-example-using-executors-and-threadpoolexecutor.html

Need research:

https://kislayverma.com/programming/asynchronous-programming-with-thread-pools-2/

https://howtodoinjava.com/java/multi-threading/when-to-use-countdownlatch-java-concurrency-example-tutorial/

http://cr.openjdk.java.net/~iris/se/12/latestSpec/api/java.base/java/util/concurrent/CompletionStage.html

https://cr.openjdk.java.net/~iris/se/11/latestSpec/api/java.base/java/util/concurrent/CompletableFuture.html

Runnable vs. Callable in Java: https://www.baeldung.com/java-runnable-callable

Baeldung projects: https://github.com/eugenp/tutorials/tree/master/core-java-modules/core-java-concurrency-basic

Java/Springboot Blocking vs Non-Blocking REST API Implementation:

https://vimalma1093.medium.com/java-springboot-blocking-vs-non-blocking-rest-api-implementation-fe5643840287

https://segmentfault.com/a/1190000040347139/en#:~:text=2.1%20Java%20thread%20status&text=It%20is%20generally%20called%20%22synchronization,or%20%22non%2Dblocking%22.

https://www.alibabacloud.com/blog/how-java-is-used-for-asynchronous-non-blocking-programming_597808

https://www.javatpoint.com/java-nio-vs-input-output

https://javarevisited.blogspot.com/2016/05/what-is-difference-between-synchronized.html

https://stackoverflow.com/questions/6045648/which-java-collections-are-synchronizedthread-safe-which-are-not

https://www.javatpoint.com/java-collections-synchronizedcollection-method

TODO https://docs.oracle.com/javase/8/docs/technotes/guides/concurrency/index.html

Interview Question: How does Thread Pool Work in Java? https://medium.com/geekculture/interview-question-how-does-thread-pool-work-in-java-9733803db6bf?source=user_profile---------2—————————-

Java Backend Developer Interview Questions(Pt. 11–20) https://medium.com/@wdn0612/java-backend-developer-interview-questions-pt-11-20-edd176b5c425?source=user_profile---------4—————————-

Tags

  1. Collections - Synchronized collections
  2. Executor framework
  3. How are Threads allocated to handle Servlet request
  4. How to write Thread-Safe Code in Java
  5. Java Threads
  6. Monitor
  7. Producer Consumer design pattern
  8. Race condition
  9. Synchronized keyword
  10. Singleton pattern
  11. Thread safety
  12. Thread synchronization
  13. Concurrency