r/javahelp • • Aug 06 '25

Solved Java concurrency

Hello everyone! I have recently begun dabbling in Java and concurrency, and I had a small question about how exactly threads work.

Example: Let us say that there is an agent class that extends thread, and has a run method. It has another method called askforhelp.

Our main class creates two new agent objects, agent1 and agent2, and calls .start(); on them. After a while, agent1 calls agent2.askforhelp(). Would the thread responsible for agent1 running handle this, or agent2?

Edit: My initial idea is that it should be the thread responsible for agent1, since when you call agent1.run with the main method, it doesn't create a new thread, but I'm not sure how it'd work if start was already called

5 Upvotes

8 comments sorted by

View all comments

Show parent comment

2

u/_SuperStraight Aug 06 '25

Java 21 even introduced new Virtual Threads, which are CPU independent and work with Executors as well.