Я запускаю два процесса с разными приоритетами и жду когда значение в одном догонит - в другом. Почему приоритет процессов так важен в многопоточной архитектуре, если процессы независимо выполняются для разных ядер? Зачем им спорить за ресурсы процессора? почему они не могут полностью параллельно выполняться?
public class ScheduleThreads
{
static Counter low;
static Counter high;
public static void main(String[] argv) throws InterruptedException
{
low=new Counter(200);
high=new Counter(0);
low.setPriority(Thread.MIN_PRIORITY);
high.setPriority(Thread.MAX_PRIORITY);
System.out.println("high priority is "+high.getPriority());
System.out.println("low priority is "+low.getPriority());
low.start();
high.start();
int diff=low.getCount() - high.getCount();
while(high.getCount()<low.getCount())
{
System.out.println("high : " + high.getCount() + " low: " + low.getCount());
if(low.getCount() - high.getCount() < diff)
{
diff = low.getCount() - high.getCount();
low.suspend();
high.suspend();
System.out.println("difference is : " + diff );
Thread.sleep(3000);
low.resume();
high.resume();
}
}
low.stop();
high.stop();
}
}
Класс, процессов гонщиков:
public class Counter extends Thread
{
int Count;
public Counter()
{
Count=0;
}
public Counter(int x)
{
Count=x;
}
public void run()
{
while(true)
{
Count++;
try {
sleep(10);
} catch(InterruptedException e){}
}
}
public int getCount()
{
return Count;
}
}