У меня есть два класса, расширяющих поток. Класс Один и класс Два.
В моем классе драйверов я создаю три потока класса Один и сто потоков класса Два.
Каждый класс Один поток должен уведомить каждый класс Два поток, который доступен для взаимодействия. После того как два потока взаимодействуют, поток Один переходит к другому потоку класса Два, который не имел взаимодействия с потоком класса Один.
Как мне соединить потоки двух типов? Как мне соединить потоки класса 1 и 2. Как поток, использующий класс 1, позволяет каждому потоку, использующему класс 2, знать, что он доступен для взаимодействия?
ex code:
public class Driver {
public static Semaphore sem = new Semaphore(1);
public static void main(String[] args) {
// TODO Auto-generated method stub
Teller tellerOne = new Teller(1);
Teller tellerTwo = new Teller(2);
Teller tellerThree = new Teller(3);
tellerOne.start();
tellerTwo.start();
tellerThree.start();
Client[] clients = new Client[10];
for(int i = 0; i<10; i++){
clients[i] = new Client(i);
clients[i].start();
}
System.out.println("Bank closes");
//end main method. do not write past this line
}
public class Teller extends Thread {
public int id;
public boolean bankOpen;
public Semaphore tsem;
Teller(int id){
this.id = id;
}
public void run(){
System.out.println("Teller " + id + " is available");
//end of run
}
//method to notify availability to client
public void notifyAvailabilityToClient(){
}
//end teller class, do not write past this line
public class Client extends Thread {
public int id;
public String status;
public Semaphore csem;
Client(int id){
this.id = id;
}
public void run(){
Random rand = new Random();
int withdrawOrDeposit = rand.nextInt(100);
withdrawOrDeposit = withdrawOrDeposit%2;
//if wORd%2 = 0, withdraw, else deposit
if(withdrawOrDeposit==0){
status="Withdraw";
}
else{
status="Deposit";
}
System.out.println("Client " + id + " waits in line to make a " + status);
//end of run method, do not write past this
}
//method to select an open Teller
public void selectAvailableTeller(){
}
//end of client class, do not write past this line
Кстати, я могу использовать классы: семафор и потоки