Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Question about threading.

  1. #1
    Junior Member
    Join Date
    Dec 2021
    Posts
    26
    Thanks
    17
    Thanked 0 Times in 0 Posts

    Default Question about threading.

    From what I can gather, the following code is supposed to execute each thread in a random order and different each time. For me the output is as if I'm not threading at all. First prints 5 times, second prints 5 times, third prints 5 times. I can't see anything wrong with the code.

    // second program to demonstrate creation of thread by extenting thread class
     
    class First extends Thread{      // FirstThread extended from java.lang.Thread
     
       public void run(){            // run method of class
     
          for(int i =1 ; i <=5; i++){   // when run, cycle 5 times
     
             System.out.println("First Thread is running.");
          }
       }
    }
     
    class Second extends Thread{        // second thread
     
       public void run(){               // run method of class
     
          for(int j = 1; j <=5; j++){      // run 5 times
     
             System.out.println("Second Thread is running.");
          }
       }
    }
     
    class Third extends Thread{         // third thread
     
       public void run(){               // run method of class
     
          for(int k = 1; k<=5; k++){    // run 5 times
     
             System.out.println("Third Thread is running.");
          }
       }
    }
     
    public class ThreadCreateExtend2{
     
       public static void main(String args[]){
     
          First f1 = new First();          // create object of First named "f1"
          f1.start();                      // call start() method inherited from thread class
          Second s1 = new Second();
          s1.start();
          Third t1 = new Third();
          t1.start();
       }
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Question about threading.

    Modern computers are very fast. When the code calls the first start() the thread is started and executes the loop before the code can call the second start.
    To cause the printed output to be interleaved, add a call to sleep to slow down the loop and allow the next start to be executed before the current loop ends.

    Also to see what is happening, print out the value of System.currentTimeMillis() with each print statement.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    FightingIrishman (January 14th, 2022)

Similar Threads

  1. Threading not functioning
    By feonx in forum AWT / Java Swing
    Replies: 1
    Last Post: October 31st, 2011, 02:56 PM
  2. Possible threading issue
    By Kerr in forum Threads
    Replies: 3
    Last Post: March 6th, 2011, 05:24 PM
  3. java threading execution time question
    By centenial in forum Threads
    Replies: 4
    Last Post: September 8th, 2010, 11:32 PM
  4. problem in Threading !!!
    By roadies07 in forum Threads
    Replies: 4
    Last Post: July 14th, 2010, 10:21 AM
  5. Threading question....
    By neo_2010 in forum Threads
    Replies: 1
    Last Post: September 2nd, 2009, 02:38 AM