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>

[3897 byte] By [begleysma] at [2007-11-27 1:23:45]
# 1

> 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.

Bad assumption. There's nothing in the rules that forces the operating system to make a lower-priority thread defer to a higher-priority thread. And there's nothing in the rules that says the operating system can't run two threads on two different processors at the same time.

If you want to do A, B, A, B, A, B... then you should have one thread that does that, not two. Or if you do have two, then you need an object for them to synchronize on so they aren't both running at the same time.

An easier way to make a thread that does the same thing every 10 milliseconds is to use a java.util.Timer object; you can cancel it whenever you like without having to write your own code.

DrClapa at 2007-7-12 0:13:49 > top of Java-index,Java Essentials,Java Programming...
# 2

Guess my assumption was bad.

I need the behavior to follow a pattern that I define (ABABA or AABBAAABAB etc). I need them to be in seperate threads. The printing is really just a quick and diryty example that easily shows me what thread is running. It looks like I will need to use synchronization. I am reading about synchronization right now.

Perhaps a Timer would be applicable for the Scheduler task.

Thanks,

Sean

begleysma at 2007-7-12 0:13:49 > top of Java-index,Java Essentials,Java Programming...