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 4 of 4

Thread: A question about threading.

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

    Default A question about threading.

    In the following code, when using the .yield() method, the main method doesn't appear to be a thread. So, Is the yield happening between methods that are calling the same class/object method i.e. System.out.print?

    One call to system.out.print is yielding to the call to the same method in the main class?

    // program to demonstrate the yield() method of Thread class
     
    class YieldClass extends Thread{
     
       public void run(){                                 // run method overrides Thread run()
     
          for(int i = 0; i < 5; i++){                     // loop 5 times when method called
     
             System.out.println("Thread 2 is running.");
             Thread.yield();                              // 
          }
       }
    }
     
    public class ThreadYield{
     
       public static void main(String args[]){
     
          YieldClass t = new YieldClass();
          t.start();
     
          for(int i = 0; i < 5; i ++){
     
             System.out.println("Main Thread is running.");
          }
       }
    }
    Last edited by FightingIrishman; January 15th, 2022 at 12:32 PM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    On vacation
    Posts
    24,712
    Thanks
    57
    Thanked 2,678 Times in 2,628 Posts

    Default Re: A question about threading.

    main method doesn't appear to be a thread.
    All code is executed in a thread. To get the name of the thread execute this statement:
       System.out.println(Thread.currentThread().toString());
    See the API doc for a description.

    Is the yield happening
    The yield happens when the yield method is called.
    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 15th, 2022)

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

    Default Re: A question about threading.

    But multithreading is possible? That's what's confusing me.

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    On vacation
    Posts
    24,712
    Thanks
    57
    Thanked 2,678 Times in 2,628 Posts

    Default Re: A question about threading.

    Most modern PCs have multithreading.

    A simple way to think of threads and the OS's scheduler:
    Take a room full of childern(threads), a toy(using the CPU) and a teacher(OS's scheduler).
    The teacher somehow choses a child and gives it the toy to play with.
    The child will continue playing until he yields the toy back to the teacher
    or the teacher takes the toy from the child(preemptive).
    Now the toy is available for another child. The teacher will look around and find a child that is ready to play with the toy.
    A child might not be ready for the toy because it needed to do something else.
    The child is given the toy and processing then loop back to where the teacher gets the toy again either by preemptive or yield.

    If the PC has two processors, that is the same as having two toys.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Question about threading.
    By FightingIrishman in forum Threads
    Replies: 1
    Last Post: January 14th, 2022, 08:42 AM
  2. Threading In java
    By pranav.bpatil in forum Threads
    Replies: 2
    Last Post: February 7th, 2012, 01:23 AM
  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