Trying to figure out how to do concurrency properly in Java. I have two groups of tasks. Tasks within a group do not interfere with one other, and so may be run concurrently without problems. However, tasks of one group should not be running while any task of the second group are running.

First group runs every 10 minutes. 
Second group runs every hour.
I am using two ScheduledExecutorService to schedule the two groups. 

Ideally, every task would need to check if any of the tasks in the second group are running, wait or them to finish, and then run.

I tried solving this with locks, and came up with something like this: 

```
    lock1.lock();
    try {
        while (!lock2.tryLock()) {
            wait();
        }

        for (int i = 0; i < 20; i++)
            System.out.println("Test1 @@@@@@@@@");
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    finally {
        lock1.unlock();
    }
}

```

Where lock1 would be shared by the first group, lock 2 by the second. But this way, I am forcing all the tasks of the first group to wait for the common lock. 

What would be an elegant way to go about this problem?