1

I have a shared boolean static variable uninitialized and created two threads, started one thread, then sleep of two seconds and then started second thread.

I have print statements basically in both the threads based on the shared variable condition but I am getting different output when ran in c# and java

C# code:

class ThreadCache
    {
        public static bool shared;
        public static void Main()
        {
            new Thread(Meth1).Start();
            Thread.Sleep(2000);
            new Thread(Meth2).Start();
        }
        public static void Meth1()
        {
            while (!shared) { }
            Console.WriteLine("Hello");
        }
        public static void Meth2()
        {
            shared = true;
            Console.WriteLine("Hi");
        }
    }

Java Code:

public class HiHello
{
    static boolean S1 = false;

    public static void main(String[] args) throws InterruptedException {
        new Thread(new Thread1(), "Thread 1").start();
        Thread.sleep(2000);
        new Thread(new Thread2(), "Thread 2").start();
    }

    private static class Thread1 implements Runnable
    {
        public void run()
        {
            while (!HiHello.S1) {}
            System.out.println("HELLO!");
        }
    }

    private static class Thread2 implements Runnable
    {
        public void run()
        {
            HiHello.S1 = true;
            System.out.println("HI!");
        }
    }
}

In C# whatever number of times I run the code I get both outputs "Hi" and "Hello" but when I run in java only "Hi" gets printed and the programs never terminates(exits). Please explain me the reason of getting different outputs

1 Answer 1

1

Both programs have multi-threading / concurrency bugs. The difference in behavior has to do with details of the underlying implementation of how C# and Java deal with threads, their memory models, how they interact with your hardware, and luck.

Specifically, in both Java and C# - to my knowledge - on a multicore processor, there is no guarantee that one thread sees what the other does unless you have a read memory barrier which you can accomplish either through a synchronized read (which also includes mutual exclusivity and is more expensive, which you do not need here) or a volatile write/read (cheaper and only gives other thread visibility and happens-before guarantees without mutual exclusivity. It's great if you have writers who only write, readers who only read, and if the field you are modifying/reading is an atomic value which is the case in your example).

Volatile vs. Interlocked vs. lock

Happens-before rules in Java Memory Model

guarantee that up-to-date value of variable is always visible to several threads on multi-processor system

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

Comments

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.