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: The yield() function in Java does not work.

  1. #1
    Junior Member
    Join Date
    Sep 2021
    Location
    India
    Posts
    20
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default The yield() function in Java does not work.

    I'm using Netbeans 6.9.1 on Ubuntu 10.04 x86 64 to work with Threads in Java. I'm having trouble with the yield() function because when I use it, the current thread continues to run instead of pausing and allowing other threads to execute.

    The code below is a simple example copied from here (https://www.scaler.com/topics/yield-method-in-java/) of using yield to run two threads. Instead of running the first thread, printing one line, and then stopping it, the programme finishes thread 1 and then runs thread 2, since the yield method is not invoked. I tested this code on Windows and it works flawlessly! So I'm wondering if there are any issues with using this strategy on Ubuntu or 64bit systems.
    //ThreadTest.java
    public class ThreadTest extends Thread{
        public ThreadTest (String name){
            super(name);
        }
        public void run(){
            for (int i=0;i<5;i++){
                System.out.println(getName()+" - "+i);
                yield();
            }
            System.out.println(" END "+getName());
        }
    }
     
    //Main.java
    public class Main {
       public static void main(String[] args) {
            ThreadTest t1 =new ThreadTest("Thread1");
            ThreadTest t2 =new ThreadTest("Thread2");
            t1.start();
            t2.start();
       }
    }
    Any suggestion, please?

  2. #2
    Member
    Join Date
    Jan 2024
    Posts
    75
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default Re: The yield() function in Java does not work.

    It seems like you're experiencing an issue with the `yield()` method not functioning as expected in your Java threads on Ubuntu 10.04 x86 64. The `yield()` method is supposed to pause the currently executing thread to allow other threads of the same priority to execute, but you're observing that the current thread continues to run without yielding.

    Firstly, let's address the specific issue you're encountering. The behavior you're observing might be due to differences in how the JVM (Java Virtual Machine) handles thread scheduling on different operating systems or architectures. While your code may work as expected on Windows, it appears to behave differently on Ubuntu 10.04 x86 64.

    However, relying on `yield()` for thread synchronization and control is not recommended for most applications because it's non-deterministic and platform-dependent, as you're experiencing. Instead, you should consider using other mechanisms provided by the `java.util.concurrent` package, such as locks, semaphores, or explicit thread coordination using `wait()` and `notify()`.

    For your specific code, if you want to ensure that the threads alternate execution, you can utilize `join()` method or `Thread.sleep()` method with proper synchronization to achieve the desired behavior.

    Here's a modified version of your `ThreadTest` class using `sleep()` for demonstration:

    ```java
    public class ThreadTest extends Thread {
    private static Object lock = new Object(); // Lock for synchronization
    private static int turn = 1; // Variable to control thread turn

    public ThreadTest(String name) {
    super(name);
    }

    public void run() {
    synchronized (lock) {
    for (int i = 0; i < 5; i++) {
    // Check if it's this thread's turn
    while (!getName().equals("Thread" + turn)) {
    try {
    lock.wait(); // Wait until it's this thread's turn
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    System.out.println(getName() + " - " + i);
    turn = (turn == 1) ? 2 : 1; // Switch turn
    lock.notifyAll(); // Notify other threads
    }
    System.out.println(" END " + getName());
    }
    }
    }
    ```

    In this modified version, each thread checks if it's its turn to execute. If not, it waits until it's notified. This ensures that the threads take turns executing. The `turn` variable controls which thread should execute next.

    If you need further help with Java assignment, exploring different synchronization mechanisms and thread control techniques, such as the ones discussed here, can be quite beneficial. Additionally, seeking guidance from reputable educational resources, online assignment help services like ProgrammingHomeworkHelp.com or consulting with experienced professionals in the field could provide valuable insights to enhance your understanding.

Similar Threads

  1. Replies: 2
    Last Post: March 6th, 2014, 05:35 AM
  2. [SOLVED] super() function how to make my program work without it
    By chalupabatman in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 3rd, 2014, 09:53 PM
  3. Function work in IE 8 or 9 but not in Chrome
    By GAMC in forum Other Programming Languages
    Replies: 2
    Last Post: August 3rd, 2012, 02:53 PM
  4. [SOLVED] Sine function, can't get factorial to work
    By Actinistia in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 27th, 2012, 11:24 AM
  5. Replies: 1
    Last Post: August 7th, 2008, 02:09 AM

Tags for this Thread