All I want to do is be able to start and stop threads
I am trying to make a thread scheduler. I have a scheduler thread, alarm thread, sensor thread, and idle thread. The sensor, alarm and idle thread all have the following form
publicclass Idleextends Thread{
publicvoid run(){
while(true){
System.out.print("I");
yield();
}
}
}
Where the class name changes and the letter printed changes (to A or S or Alarm or Sensor respectively).
The main body of the Scheduler currently looks something like this.
sensor.setPriority(2);
alarm.setPriority(2);
idle.setPriority(2);
alarm.start();
sensor.start();
idle.start();
while(true){
for(int i=0;i<schedule.length;i++){
if (schedule[i] == 1){
if (alarm.isAlive()) alarm.interrupt();
if (idle.isAlive()) idle.interrupt();
alarm.setPriority(2);
idle.setPriority(2);
sensor.setPriority(3);
sensor.resume();
}
elseif (schedule[i] == 2){
if (sensor.isAlive()) sensor.interrupt();
if (idle.isAlive()) idle.interrupt();
idle.setPriority(2);
sensor.setPriority(2);
alarm.setPriority(3);
alarm.resume();
}
else{
if (sensor.isAlive()) sensor.interrupt();
if (alarm.isAlive()) alarm.interrupt();
alarm.setPriority(2);
sensor.setPriority(2);
idle.setPriority(3);
idle.resume();
}
System.out.println(" " + i);
try{
sleep(10);
}catch (InterruptedException e){
e.printStackTrace();
}
//IF activetask = schedule[i] do nothing
//ELSE IF schedule[i] = 0 do nothing
//ELSE stop current task and start task[schedule[i]]
//possibly modify priorities
}
sensor.stop();
alarm.stop();
idle.stop();
System.out.println();
System.out.println("************************");
break;
}
The scheduler thread is set to priority 4. The behavior that I WANT is for the scheduler to run every 10 milliseconds (as controlled by the sleep(10) line in the scheduler) and to have the appropriate other thread run in the intervening period. Since the Scheduler thread is always a higher priority I would think that it should run when it wakes up from its "sleep" and stop stop the running task and start the correct task.
Thanks,
Sean>

