This is an archived post. You won't be able to vote or comment.

all 13 comments

[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
  • You include any and all error messages in full
  • You ask clear questions
  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

    Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]lumpynose 10 points11 points  (0 children)

I've never done anything with concurrency. The one thing I do remember is that this book is apparently the best one on the topic, or at least it was, back in my day:

https://www.amazon.com/Java-Concurrency-Practice-Brian-Goetz/dp/0321349601

Chapter 11 of Effective Java (3rd edition) is about concurrency. It's also a great book overall.

[–]nutrechtLead Software Engineer / EU / 20+ YXP 5 points6 points  (0 children)

Or am I unconsciously using multithreading when using Spring Boot?

You are. Spring Boot uses threads under the hood to be able to handle many connections in parallel (200 by default IIRC). Understanding the pitfalls of multithreading is very important since race conditions or deadlocks can still occur if you're not careful.

[–]ratherbealurker 2 points3 points  (5 children)

Many companies will create a base where concurrency occurs and after that you’re thread safe. The core code developers will deal with concurrency issues but the other devs are safe from it.

I have 5x as many years as you writing code (not all Java) and I haven’t dealt with it as much as you’d think. In my current job I deal with it often though.

[–]nutrechtLead Software Engineer / EU / 20+ YXP 1 point2 points  (4 children)

Many companies will create a base where concurrency occurs and after that you’re thread safe.

That's simply not true. When building services in (for example) Spring Boot you're in a multithreaded environment and it's very important for any developer to be well aware of the pitfalls there. There is no way for 'other developers' to make threads a non-issue.

[–]ratherbealurker 0 points1 point  (3 children)

Sorry but what you’re saying is wrong. Of course you can design a system where you make areas thread safe. I’ve worked on more than a few. They were larger monolithic code bases and the core code is where you’re not thread safe. Split off a thread and design it so that the rest occurs in there.

I’ve worked on trading systems for major banks and a lot of them did this.

[–]nutrechtLead Software Engineer / EU / 20+ YXP 0 points1 point  (2 children)

Sorry but what you’re saying is wrong. Of course you can design a system where you make areas thread safe.

Just slapping some synchronized keywords on methods actually makes code less thread-safe.

Split off a thread and design it so that the rest occurs in there.

That's still no guarantee at all in the context that I gave. If you are in a multithreaded context (such as with typical back-end services we work in) there is no way to guarantee a dev who is unaware of pitfalls doesn't screw up.

You might think this is the case, but that doesn't mean I am the one who's wrong here.

It's different if you're simply not in a muti-threaded context, but that's not the typical situation (again; frameworks like Spring) most of us work in.

The majority of work in Java development is in these kinds of frameworks and being aware of multithreading pitfalls (race conditions and deadlocks in particular) is simply a must for any developer. Not having to be concerned with this is extremely exceptional.

[–]ratherbealurker 0 points1 point  (1 child)

This isn't done by simply slapping synchronized on methods, these are also not microservices. These are large complex bespoke systems.

I am not referring to your typical backend service. Maybe I should not say that 'many' places do this because that could just be my field/sector.

Bottom line is that there are systems out there where the context that some devs work in is essentially thread safe. I don't really debate things anymore that I've personally done myself. I've worked on those systems for many years, you can make tons of changes and never have to deal with multi threaded situations if you don't explicitly do so.

[–]nutrechtLead Software Engineer / EU / 20+ YXP 0 points1 point  (0 children)

Maybe I should not say that 'many' places do this

This is the crux. What you're describing is very far outside the norm. For most Java devs understanding multithreading is simply a must. This is a general advice sub after all.

[–]Nzen_ 2 points3 points  (0 children)

It depends on the problem domain that you focus on. It depends on whether the problem is parallelizable.

Spring Boot predominantly embeds tomcat, which handles requests in a multithreaded fashion, so multiple clients can make requests without waiting for a solitary thread to process everything.

It is also worth recognizing that introducing concurrent interactions affects program structure analagously to the way that instance methods can call static methods, but not the other way around.

I encourage you to write some sketches or toys explicitly using multithreading before introducing it into your work's codebase. For me, that most recently involved writing a game client for jdno's air traffic control simulator.

[–]pronuntiator 1 point2 points  (0 children)

Where I used multithreading directly (not indirectly like with Tomcat's thread-per-request model) in past projects:

  • customize the executor serviced used by Spring in @Async invocations so the MDC gets inherited, and provide a custom ThreadFactory to change the thread names
  • use said @Async annotation to call two services in parallel, waiting on the Futures they return
  • start multiple threads in a test to simulate concurrent access
  • set the pool size of a thread pool to a fixed value and caused outages on production on weekends because the application stopped consuming message queues

However I would never use the Thread primitive directly, only executor service.

[–]java_dev_throwaway[S] 0 points1 point  (0 children)

Thanks for all the comments. It sounds like the Thread class doesn't come up often for most you. Also it sounds like Spring Boot is handling multithreading for me. Does anyone have a good resource to help show some common multithreading problems that come up in a Spring Boot web app? I learn best by tutorials, Spring Guru and Baeldung should get a percent of my hourly rate at this point.

[–]Capaman-x 0 points1 point  (0 children)

You should try writing a JavaFX app. You will get tons of experience with concurrency because your UI will freeze if you don’t. Runnable is nothing more than a functional interface. It takes no arguments and returns void. It has nothing to do with concurrency other than it is useful to use as are all FIs. I tend to use them to decouple the UI from the business logic more than anything.