The threads will have to have some sort of connection for this to work. If thread 1 has a reference to thread 2, the lock and condition variables may be in thread 2 and vice versa.
If not, the variables will have to be in a separate class which both threads have access to. So, you'll have to pass the same instance of that class to both threads, so that instance becomes a shared resource. Following example assumes you have classes Thread1
and Thread2
that extends Thread with a constructor taking SharedResource
as an argument:
SharedResource sr = new SharedResource();
Thread1 t1 = new Thread1(sr);
Thread2 t2 = new Thread2(sr);
t1.start();
t2.start();