r/ProgrammerTIL • • Mar 09 '18

Java [Java] TIL recursive imports are allowed

In the java source, java.util.Arrays imports java.util.concurrent.ForkJoinPool. ForkJoinPool, in turn, imports java.util.Arrays.

Another example:

% tree
.
└── com
    └── example
        └── src
            ├── test
            │  ├── Test.class
            │  └── Test.java
            └── tmp
                ├── Tmp.class
                └── Tmp.java
% cat com/example/src/test/Test.java 
package com.example.src.test;
import com.example.src.tmp.Tmp;
public class Test {}
% cat com/example/src/tmp/Tmp.java 
package com.example.src.tmp;
import com.example.src.test.Test;
public class Tmp {}
29 Upvotes

14 comments sorted by

View all comments

Show parent comment

2

u/MoonlitEyez Mar 10 '18 edited Mar 10 '18

getFoo() and getBar() are mark synchronized. Meaning only one instance of getFoo() can run at any given time, and the same is true with getBar().

I'm not saying it's no longer can happen, but the risk of it is pretty near 0%. I'm not quite sure how Java / jvm handles this keyword so there could be an edge case I'm not aware of. E.g. two threads are executing at the exact same time and isLock() could in theory return false for both. But that has it's own work arounds that I don't know if Java or jvm patch.

3

u/kilogram007 Mar 10 '18

Right, I was explaining to _wannabeDeveloper why it has to be synchronized.