0

For the given program, can you explain because I didn't get what I expected:

package javaapplication14;

class Mythread implements Runnable{
    String myname;
    int myage;

    public Mythread( String par_name, int par_age){
        myname = par_name;
        myage = par_age;
    }
    public void run(){
        try{
          for(int i=1; i<=myage; i++)  {
              System.out.println("Happy birthday "+myname);
              Thread.sleep(2000);
          }
        }catch(Exception e){
            System.out.println();
        }
    }
}
public class JavaApplication14 {

    public static void main(String[] args) {
      Mythread m = new Mythread("Mutinda ", 2);
      Mythread p = new Mythread("Boniface", 2);
      Thread k = new Thread(m);
      Thread q = new Thread(p);
      k.start();
      q.start();
      Thread t = new Thread(m);
      try{
        for( int i=1; i<=5; i++){
          System.out.println("Main thread executing");
          Thread.sleep(1000);          
        }  
      }catch(Exception e){
          System.out.println("Thread interrupted");
      }

    }
}

And this was my output:

Main thread executing
Happy birthday Boniface
Happy birthday Mutinda 
Main thread executing
Main thread executing
Happy birthday Boniface
Happy birthday Mutinda 
Main thread executing
Main thread executing

I expected this:

Main thread executing
Happy birthday Mutinda 
Happy birthday Boniface
Main thread executing
Main thread executing
Happy birthday Mutinda
Happy birthday Boniface
Main thread executing
Main thread executing

I need someone to explain to me the priorites set up for the two threads k and q such that the output of q becomes the first one than k, regardless of the sleep time.

My argument: since k.start() was called before the q.start(), is expect my output to start with k, since I called it first and the two takes the same sleep time.

0

3 Answers 3

3

When you start threads, its because you have independant tasks which have little or no inter-dependence. As such you shouldn't expect a particular order of execution between the two threads, in fact most multi-threaded bugs come from making such assumptions.

If you want things to happen in a particular order use a single thread. If you can assume there is no particular order for tasks, only then use multiple threads.

Sign up to request clarification or add additional context in comments.

Comments

3

Basically your argument is wrong. start() function does not start a tread. It simply puts it into runnable state.

3 Comments

my code seems to be fine to my assumption:: the issue that i have is why is it that k.start() ----- prints my results at the second place other than the first place and since it enters the running start before q.start()???,,,,
Threads have a State concept. When you call start(), it advices the JVM to put it on a Runnable state(not start executing). At a single point of time there may be many threads in Runnable state. JVM picks ona random basis and start that thread.
@user709577 k.start() wont start the thread execution...it will simply say that the thread is ready for execution. JVM can pick it any time to Run it
0

You cannot rely on priorities for the multi-threading sequence execution. Java Threading basic rule is that it will be JVM dependent and that there can be no surety about the execution sequence of each thread. Priorities can influence but not ensure the execution

2 Comments

what if i setup my second thread priority to 10?? and the first thread q to 7??? what will happen??
@user709577 It might print the correct result but there is no guarantee that same will occur on multiple runs

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.