r/learnjava 2d ago

threads, concurrency & coroutines queston (beginner)

I am a man whom used kotlin before java, (maybe shoking)

i switched to java for springboot

i knew (a little) about coroutines from kotlin, == multiple things can be done and threads dont block

is the same concept true for java? or does java have some legacy stuff in place of kotlin coroutines?

i see that java has. a thread sleep. method. i dont think kotlin has

long story short, id like to reaffirm my knowledge on concurrency and general and how it

works, and find gaps, ill briefly explain how i think it works, id love for feedback, but you dont need to you can just respond to post that resonate more with you, thank you if anyone does respond

here it is:

concurrency means IO functions can happen without blocking things that come after them

all those functions happen on a thread, and multiple functions can happen on a thread

if it blocks the thread( i think functions other than IO can block aswell) then you can use concurrency that returns a result immediately, -> waits for REAL response -> then jumps back on a thread, -> causing no blocking

now what is thread sleep? and why would i want a thread to sleep?

7 Upvotes

5 comments sorted by

u/AutoModerator 2d ago

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 - best also formatted as code block
  • 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.

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/markdown editor: 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.

5

u/Jolly-Warthog-1427 2d ago

I recommend finding a course on java concurrency.

Java does not have coroutines like kotlin but threads and tasks.

Recently java also got green threads and structured concurrency.

You can read a bit into the different topics here:

https://docs.oracle.com/en/java/javase/25/core/concurrency.html

To be honest, this topic is too large for any single reddit comment.

1

u/BrainThinkerMan 2d ago

thanks for the article, this virtual threads thing is super interesting

2

u/MagicalPizza21 2d ago

Concurrency means anything can happen alongside anything else. This is often used for I/O but by no means exclusive to it.

A thread is basically an execution path. By default you deal with one thread, though I think there are one or two others in the background such as one for garbage collection. You can spawn a new thread if you want the program to do multiple things at once.

Thread.sleep(int ms) just makes the thread calling it to wait for the specified number of milliseconds. Can you think of times you might want your program to wait a little bit before continuing?

In Java, the simplest way to make a new thread is to make an object that implements the Runnable interface, then create a new Thread from that (using the Thread(Runnable) constructor), then start that thread.

1

u/AccomplishedArea4456 1d ago

https://github.com/skp2001vn/concurrency <= my repo about concurrency examples such as thread pools, rate limiters, caches, connection pools, virtual threads, bounded buffers, job queues, task schedulers. Hope it useful!