Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9118049
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:02:59+00:00 2026-06-17T05:02:59+00:00

I have the 3 following threads: // These are the two resource objects we’ll

  • 0

I have the 3 following threads:

// 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. But when I set the priority for each thread, with setPriority I don’t see the threads do their job according to their priority.

for example, I have these forloops in each thread before the first synchronized:

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

Finally, my last question is can I show the priority effect in terms of resources, printing and executing time ?

Thanks 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T05:03:00+00:00Added an answer on June 17, 2026 at 5:03 am

    Thread priorities on most general purpose operating systems are not absolute (e.g. 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.

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

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following django method: def setCurrentSong(request, player): try: newCurrentSong = ActivePlaylistEntry.objects.get( song__player_lib_song_id=request.POST['lib_id'],
I have the following situation: I have two threads thread1 , which is a
I have this following code fragment which is accessed by different threads. try {
I have std::list<Info> infoList in my application that is shared between two threads. These
if there are two threads as producer/consumer is it good idea to have following
If I have the following code : threads = [] (1..5).each do |i| threads
I am having problems with passing arguments to new threads, I have the following
I have a web service for a forum, and I have the following two
I have an application in java that uses two different threads.And for sharing data
I have two tables. One that keeps all of my threads started on a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.