I'm In a sticky situation. I know this is almost as far as i can get for now, but what i want to do is actually to make an array of thread (or many thread) and accommodate number of threads in queue in the line, so for example i can accommodate 3 thread at a time, i make the first 3 thread run, then make the other wait, when there is free like for example the 1 is free or terminated the other one can start running.
also i want to make sure if the thread can run or not if the thread that is running is the same as the other thread's gender.
Thread myThreads[] = new Thread[LN.GetLine().length()];
int l=LN.GetLine().length();
for (int j = 0; j < LN.GetLine().length(); j++) {
String Name = CR.Gender(LN.GetLine().charAt(j)) + j;
myThreads[j] = new Thread(new MyThread(Name,LN));
myThreads[j].setPriority(l);
System.out.println(myThreads[j].toString());
l--;
}
for(int b=0;b<LN.GetLine().length();b++){
myThreads[b].start();
synchronized(myThreads[b]){
try{
myThreads[b].wait();
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
For now what i can make is accommodate or make 1 thread run at a time.
(Yes this is a Machine bathroom Problem)
My real question is. if i Edit the function run()
in myThread()
that has a wait()
or just plain put a System.out.println(getName() + " is Using");
And how will the thread know in their run()
function that other thread is running.
public class MyThread extends Thread {
public MyThread(String id) {
super(id);
}
public void run(){
System.out.println(getName() + " is Using");
>>>Put wait if other thread running<<<<
>>>If can use or not if same gender<<<<
}
Or should i just implement that outside? or put the Waiting outside? Also i'm really new in Threading so i haven't really explored with Sleep and Interrupt yet.