I tried creating a shared lock in the Application class and passing it to the method parameter that creates a thread and uses this lock. I want a thread created by an instance of a class to synchronize with a thread created by an instance of another class.
Class Alpha has a start(Object sharedLock) which creates a new thread.
public abstract class Alpha {
public abstract void test();
public void start(Object sharedLock) {
Thread t = new Thread(() -> {
while(true) {
synchronized(sharedLock) {
test();
}
try {
TimeUnit.SECONDS.sleep(db.timeInterval)
} catch(InterruptedException e) {
Thread.currentThread().interrupt();
}
}
})
}
}
Class Alpha has two base classes Beta and Gama, which invokes start() method from Application class. Below is the code snippet
@Override
public void run(String... args) {
beta.start(sharedLock);
gama.start(sharedLock)
}
test() is overridden in child classes - beta and gama.
And here is the implementation of test()
public class Beta extends Aplha {
@Override
public void test() {
ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
final int[] count = {1};
Runnable task = new Runnable() {
@Override
public void run() {
System.out.println(count[0]);
count[0]++;
if (count[0] > 20) {
executor.shutdown();
}
}
}
executor.scheduledAtFixedRate(task, 0, 1, TimeUnit.SECONDS);
}
}
public class Gama extends Alpha {
@Override
public void test() {
System.out.println("Gama thread")
}
}
Only one thread should be able to execute test() at a time. So if beta thread calls start() first then it should print 1 to 20 and then gama thread execute start()
Also, when a thread is seeping, the other thread can execute by having access to lock
How can I achieve this behaviour?
Try a semaphore: