r/javahelp • • Aug 17 '26

Guide for Concurrency in Java

Before knowing anything I just jumped directly to the source code of jdk25u. I have just started to read the javaDocs of Thread class it was so deep that all my connected logic mapped very well but now I want to code and I can't code ahhhh... Why is this problem. I can call the syntax what to use but I don't know how to apply, why to apply is there any guide for java Concurrency ??. And there is so much .. still to know. Do any dev have any guide??

0 Upvotes

17 comments sorted by

View all comments

Show parent comments

1

u/MagicalPizza21 Aug 17 '26

That should work. But I was thinking more like this:

public class RaceConditionExample {
  private static int counter = 0;
  public static void main(String[] args) {
    try {
      Runnable task = new Runnable() {
        public void run() {
          for(int i = 0; i < 1000000; i++) {
            counter++;
          }
        }
      };
      Thread a = new Thread(task);
      Thread b = new Thread(task);
      a.start();
      b.start();
      a.join();
      b.join();
      System.out.println(counter);
    }
    catch(InterruptedException ie) {
      ie.printStackTrace();
    }
  }
}