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

  • SEARCH
  • Home
  • 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 8021885
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T22:06:54+00:00 2026-06-04T22:06:54+00:00

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

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

  • 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-04T22:06:55+00:00Added an answer on June 4, 2026 at 10:06 pm

    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.

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

Sidebar

Related Questions

Can any one explain class X x; what actually it mean ...in the program...Please
Here is a simple php program which gives a strange output. Can anyone explain
Given the following program: import java.io.*; import java.util.*; public class GCTest { public static
Given the following simple program: import wx class TestDraw(wx.Panel): def __init__(self,parent=None,id=-1): wx.Panel.__init__(self,parent,id,style=wx.TAB_TRAVERSAL) self.SetBackgroundColour(#FFFFFF) self.Bind(wx.EVT_PAINT,self.onPaint)
I wrote a Multithreaded program in Java given below :- public class Client {
can any one give me the dry output for this program? #include <stdio.h> main()
Hi I am trying to write a program where given a list of lists
Given a method.. public ActionResult Method() { // program logic if(condition) { // external
I want to understand how a C++ program that was given to me works,
I have to write a program in python where the user is given 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.