线程优先级C ++ vs Java

时间:2016-03-17 13:22:32

标签: java c++ multithreading thread-priority

在Java中,我知道我们可以设置线程的优先级,例如:

  Thread t1 = new Thread();  
  t1.setPriority(Thread.MIN_PRIORITY);

我正在研究在C ++中处理这种问题的各种方法以及Java和C ++中线程优先级之间的主要区别,线程优先级是否以类似的方式工作,从而使线程能够通过较低线程更多地访问资源?或者在C ++中,它只是指线程有权访问的CPU数量?

这种Java实现在C ++中是否可行?

class thrun implements Runnable
{
      Thread t;
      boolean runn = true;

      thrun(String st,int p) 
      {
    t = new Thread(this,st);
    t.setPriority(p);
    t.start();
      }

      publicvoid run()
      {
    System.out.println("Thread name : " + t.getName());
    System.out.println("Thread Priority : " + t.getPriority());
      }     

}

class priority
{
      publicstaticvoid main(String args[])
      {
     Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
     thrun t1 = new thrun("Thread1",Thread.NORM_PRIORITY + 2);
     thrun t2 = new thrun("Thread2",Thread.NORM_PRIORITY - 2);
     System.out.println("Main Thread : " + Thread.currentThread());
     System.out.println("Main Thread Priority : " + Thread.currentThread().getPriority());
      }
}

0 个答案:

没有答案