0

Thread not starts while other is too busy

I have multi thread application. I'm analyzing workflow of two threads. Thread_1 has cycle for(...). Thread_2 has some small job. In some cases Thread_2 not starts it's job while cycle for(...) not finished in Thread_1. Is it possible that system decides to put all resources for Thread_1 ? How to give possibility to start Thread_2 while Thread_1 is in for(...). Should I put something like Thread.sleep(100) there? Everything is in Java 1.4.

vico
  • 17,051
  • 45
  • 159
  • 315
  • 1
    Your application should not depend on any execution order constraints it doesn't enforce. Does it? – John Dvorak Aug 10 '16 at 09:12
  • You could try to use `Thread#yield()` but it's generally not advicable to use that when not needed. Is it really important to have both threads run in parallel? And if yes, what are they doing and what synchronization/messaging is happening between them? Your description also sounds like thread 1 might be a lower priority background thread while thread 2 should have a higher priority so try using `Thead#setPriority()`. – Thomas Aug 10 '16 at 09:13
  • That is the thing with threads; you don't have much control there. In that sense: it seems that you already started **measuring**. So why don't you go one step further and start making experiments to see how adding a sleep for example would change things. Then, java 1.4 ; seriously? You know that version isn't even listed any more on the table where Oracle keeps track of "end of life" dates for java versions. – GhostCat Aug 10 '16 at 09:14
  • my application not depends on execution order, but I just wondering why in some cases Thread_2 is not started while Thread_1 is not finishing it's job. That means that in some cases when Thread_1 has noncontinuous job Thread_2 will not start. I need to avoid that. Threads are independent of each other. – vico Aug 10 '16 at 09:32
  • Did you take thread dumps to study the behaviour? Maybe this question is related: http://stackoverflow.com/questions/38354595/java-code-execution-yields-to-different-results-in-debug-without-breakpoints-and#comment64148191_38354595 – Marco A. Hernandez Aug 10 '16 at 09:42
  • What precisely is thread 2 doing when it doesn't start its job? Does it do something else? Does it block on some synchronization objection? Or what? – David Schwartz Aug 10 '16 at 10:06

2 Answers2

1

It will be great if you share some code piece, it's difficult to debug the code without looking logic. Ideally thread_1 and thread_2 should run independently. thread_2 can't wait to finish for loop in thread_1. Example:

class RunnableDemo implements Runnable {
   private Thread t;
   private String threadName;

   RunnableDemo( String name){
       threadName = name;
       System.out.println("Creating " +  threadName );
   }
   public void run() {
      System.out.println("Running " +  threadName );
      try {
         for(int i = 4; i > 0; i--) {
            System.out.println("Thread: " + threadName + ", " + i);
            // Let the thread sleep for a while.
            Thread.sleep(50);
         }
     } catch (InterruptedException e) {
         System.out.println("Thread " +  threadName + " interrupted.");
     }
     System.out.println("Thread " +  threadName + " exiting.");
   }

   public void start ()
   {
      System.out.println("Starting " +  threadName );
      if (t == null)
      {
         t = new Thread (this, threadName);
         t.start ();
      }
   }

}

public class TestThread {
   public static void main(String args[]) {

      RunnableDemo R1 = new RunnableDemo( "Thread-1");
      R1.start();

      RunnableDemo R2 = new RunnableDemo( "Thread-2");
      R2.start();
   }   
}

Output:

Creating Thread-1
Starting Thread-1
Creating Thread-2
Starting Thread-2
Running Thread-1
Thread: Thread-1, 4
Running Thread-2
Thread: Thread-2, 4
Thread: Thread-1, 3
Thread: Thread-2, 3
Thread: Thread-1, 2
Thread: Thread-2, 2
Thread: Thread-1, 1
Thread: Thread-2, 1
Thread Thread-1 exiting.
Thread Thread-2 exiting.
Prabhat Kumar
  • 256
  • 2
  • 9
-1

you can make the first thread for loop pause for after a given number of iterations and set the static var th2_done to false to not waist Thread_1 time once the Thread_2 is done

Thread_1: for (...){ if(num_it % cycle && th2_done==false) sleep(100);}

Thread_2: for (...){} th2_done = true

whyn0t
  • 301
  • 2
  • 14