简体   繁体   English

Java - 在3个线程之间模拟死锁并设置优先级

[英]Java - Simulating a deadlock beween 3 threads and setting priorities

I have the 3 following threads: 我有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");
      }
    }
  }
};

Now what I'm trying to do is simulating a deadlock, the program stops which I guess has caused a deadlock. 现在我要做的是模拟死锁,程序停止,我猜这已导致死锁。

After the problem is, I added a for loop to print some text in each thread before the synchronized function. 问题出现后,我添加了一个for循环,在synchronized函数之前在每个线程中打印一些文本。 But when I set the priority for each thread, with setPriority I don't see the threads do their job according to their priority. 但是当我为每个线程设置优先级时,使用setPriority我没有看到线程根据它们的优先级来完成它们的工作。

for example, I have these forloops in each thread before the first synchronized : 例如,我在第一次synchronized之前在每个线程中都有这些forloops:

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

and so on, t2 stands for thread 2. All I'm trying to do is making sure that priorities are working. 等等,t2代表线程2.我所要做的就是确保优先级正常。 I didn't try to run threads according to their priorities in a non deadlock program yet. 我没有尝试在非死锁程序中根据它们的优先级运行线程。

The OS or CPU might be the cause of such an issue? 操作系统或CPU可能是导致此类问题的原因?

Finally, my last question is can I show the priority effect in terms of resources, printing and executing time ? 最后,我的最后一个问题是,我可以在资源,打印和执行时间方面显示优先级效果吗?

Thanks :) 谢谢 :)

Thread priorities on most general purpose operating systems are not absolute (eg the highest priority thread ready to run always gets the CPU) but rather an input to a scheduling algorithm that computers a quantum - or length of time that the thread is allowed to run before relinquishing control to another one. 大多数通用操作系统上的线程优先级不是绝对的(例如,准备运行的最高优先级线程总是获取CPU)而是计算量的一个调度算法的输入 - 或者允许线程运行的时间长度放弃对另一个人的控制。

Therefore there is no certainty as to the sequence your threads will run, even when you do set the priorities. 因此,即使您设置了优先级,也无法确定线程运行的顺序。 All you can safely assume is that the highest priority thread is likely to get a larger share of CPU time than the lower priority one. 您可以放心地假设,优先级最高的线程可能比较低优先级的线程获得更大的CPU时间份额。 However, as your program is performing repeated IO (which blocks) and then sleeping, odds are that the threads get nowhere near their quantum anyway and will all be equally eligible to run when the 50ms sleep ends. 但是,当你的程序执行重复的IO(阻塞)然后再睡觉时,可能的情况是线程无论如何都无法接近他们的量子,并且当50ms睡眠结束时,所有人都同样有资格运行。

A class of operating systems - Real Time Operating Systems (RTOSs) - do implement priority-driven pre-emptive threading behaviour (rather than time-slicing) and can yield highly predictable thread ordering. 一类操作系统 - 实时操作系统(RTOS) - 确实实现了优先级驱动的抢占式线程行为(而不是时间切片),并且可以产生高度可预测的线程排序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM