0

次の 3 つのスレッドがあります。

// These are the two resource objects we'll try to get locks for
final Object resource1 = "resource1";
final Object resource2 = "resource2";
final Object resource3 = "resource3";
// Here's the first thread.  It tries to lock resource1 then resource2
Thread t1 = new Thread() {
  public void run() {

    // Lock resource 1
    synchronized(resource1) {
      System.out.println("Thread 1: locked resource 1");

      // Pause for a bit, simulating some file I/O or something.
      // Basically, we just want to give the other thread a chance to
      // run.  Threads and deadlock are asynchronous things, but we're
      // trying to force deadlock to happen here...
      try { Thread.sleep(50); } catch (InterruptedException e) {}

      // Now wait 'till we can get a lock on resource 2
      synchronized(resource2) {
        System.out.println("Thread 1: locked resource 2");
      }
    }
  }
};

// Here's the second thread.  It tries to lock resource2 then resource1
Thread t2 = new Thread() {
  public void run() {

    // This thread locks resource 2 right away
     synchronized(resource2) {
      System.out.println("Thread 2: locked resource 2");

      // Then it pauses, for the same reason as the first thread does
      try { Thread.sleep(50); } catch (InterruptedException e) {}

      // Then it tries to lock resource1.  But Thread 1 locked
      // resource1, and won't release it till it gets a lock on
      // resource2.  This thread holds the lock on resource2, and won't
      // release it 'till it gets resource1. 
      synchronized(resource1) {
        System.out.println("Thread 2: locked resource 1");
      }
    }
  }
};


// t3 tries to lock resource3 then resource2
Thread t3 = new Thread() {
  public void run() {
      for(int i=1; i<=5;i++)
        System.out.println("t3 "+i);
    synchronized (resource3) {
      System.out.println("Thread 3: locked resource 3");

      try {
        Thread.sleep(50);
      } catch (InterruptedException e) {
      }

      synchronized (resource2) {
        System.out.println("Thread 3: locked resource 2");
      }
    }
  }
};

今私がやろうとしているのは、デッドロックをシミュレートすることです。プログラムが停止し、デッドロックが発生したと思います。

synchronized問題が発生した後、関数の前に各スレッドでテキストを出力する for ループを追加しました。しかし、各スレッドの優先度を設定するとsetPriority、スレッドが優先度に従ってジョブを実行することがわかりません。

たとえば、最初の前の各スレッドに次の forloops がありますsynchronized

for(int i=1; i<=5;i++)
   System.out.println("t2 "+i);

など、t2 はスレッド 2 を表します。私がやろうとしているのは、優先順位が機能していることを確認することだけです。非デッドロック プログラムで優先順位に従ってスレッドを実行しようとはまだ試みていません。

OSまたはCPUがこのような問題の原因である可能性がありますか?

最後に、私の最後の質問は、リソース、印刷、および実行時間に関して優先効果を示すことができるかということです。

ありがとう :)

4

1 に答える 1

2

ほとんどの汎用オペレーティング システムのスレッドの優先度は絶対的なものではなく (たとえば、実行可能な最も優先度の高いスレッドが常に CPU を取得します)、むしろ量子を計算するスケジューリング アルゴリズムへの入力、またはスレッドが実行できる時間の長さです。制御を別のものに放棄します。

したがって、優先順位を設定したとしても、スレッドが実行される順序については確実ではありません。安全に想定できるのは、最も優先度の高いスレッドが、優先度の低いスレッドよりも多くの CPU 時間を共有する可能性が高いということだけです。ただし、プログラムが繰り返し IO (ブロック) を実行してからスリープ状態になると、スレッドがクォンタムに近づかず、50 ミリ秒のスリープが終了するとすべてのスレッドが同等に実行できるようになる可能性があります。

オペレーティング システムのクラスであるリアルタイム オペレーティング システム (RTOS) は、(タイム スライスではなく) 優先度に基づくプリエンプティブ スレッド動作を実装しており、高度に予測可能なスレッド順序を生成できます。

于 2013-01-13T00:59:24.513 に答える