Unable to understand the logic of below output:- - multithreading

public class Test implements Runnable
{
public void run()
{
System.out.printf("%d",3);
}
public static void main(String[] args) throws InterruptedException
{
Thread thread = new Thread(new Test());
thread.start(); //line10
System.out.printf("%d",1);
thread.join();
System.out.printf("%d",2);
}
}
In the above code, at line 10 thread.start(); should spawn a new thread and hence run() should be called.Hence, Output should be 312 but instead on running the program, I got 132.

Main thread and new thread created are running in parallel , so if main thread reaches the line 11 first you will see 1 and then if new thread reaches its statement first you will get 3 and then 1

Start put thread to runnable mode, it does NOT mean call run method directly somewhere inside start, after start (), it counts on scheduler to finally run the thread, so 312 132 123 all are acceptable result.

The very nature of multi-threading is that you cannot usually guarantee or predict which line gets executed before another if they are being executed concurrently.
In this case it may be a little predictable however. Line 10 will need to access the memory (heap) allocation of the Test type, and then access the memory location of the run() method, and then execute the run() method, which then runs printf within that function.
Depending on what language you use, this may vary in optimization; but regardless of how well optimized that is, it will still always be slower than running the very next line after the thread.start() which prints 1 immediately after line 10 without waiting.

Related

How can I solve the problem of script blocking?

I want to give users the ability to customize the behavior of game objects, but I found that unity is actually a single threaded program. If the user writes a script with circular statements in the game object, the main thread of unity will block, just like the game is stuck. How to make the update function of object seem to be executed on a separate thread?
De facto execution order
The logical execution sequence I want to implement
You can implement threading, but the UnityAPI is NOT thread safe, so anything you do outside of the main thread cannot use the UnityAPI. This means that you can do a calculation in another thread and get a result returned to the main thread, but you cannot manipulate GameObjects from the thread.
You do have other options though, for tasks which can take several frames, you can use a coroutine. This will also allow the method to wait without halting the main thread. It sounds like your best option is the C# Jobs System. This system essentially lets you use multithreading and manages the threads for you.
Example from the Unity Manual:
public struct MyJob : IJob
{
public float a;
public float b;
public NativeArray<float> result;
public void Execute()
{
result[0] = a + b;
}
}
// Create a native array of a single float to store the result. This example waits for the job to complete for illustration purposes
NativeArray<float> result = new NativeArray<float>(1, Allocator.TempJob);
// Set up the job data
MyJob jobData = new MyJob();
jobData.a = 10;
jobData.b = 10;
jobData.result = result;
// Schedule the job
JobHandle handle = jobData.Schedule();
// Wait for the job to complete
handle.Complete();
float aPlusB = result[0];
// Free the memory allocated by the result array
result.Dispose();

Give me a scenario where such an output could happen when multi-threading is happening

Just trying to understand threads and race condition and how they affect the expected output. In the below code, i once had an output that began with
"2 Thread-1" then "1 Thread-0" .... How could such an output happen? What I understand is as follows:
Step1:Assuming Thread 0 started, it incremented counter to 1,
Step2: Before printing it, Thread 1 incremented it to 2 and printed it,
Step3: Thread 0 prints counter which should be 2 but is printing 1.
How could Thread 0 print counter as 1 when Thread 1 already incremented it to 2?
P.S: I know that synchronized key could deal with such race conditions, but I just want to have some concepts done before.
public class Counter {
static int count=0;
public void add(int value) {
count=count+value;
System.out.println(count+" "+ Thread.currentThread().getName());
}
}
public class CounterThread extends Thread {
Counter counter;
public CounterThread(Counter c) {
counter=c;
}
public void run() {
for(int i=0;i<5;i++) {
counter.add(1);
}
}
}
public class Main {
public static void main(String args[]) {
Counter counter= new Counter();
Thread t1= new CounterThread(counter);
Thread t2= new CounterThread(counter);
t1.start();
t2.start();
}
}
How could Thread 0 print counter as 1 when Thread 1 already incremented it to 2?
There's a lot more going on in these two lines than meets the eye:
count=count+value;
System.out.println(count+" "+ Thread.currentThread().getName());
First of all, the compiler doesn't know anything about threads. It's job is to emit code that will achieve the same end result when executed in a single thread. That is, when all is said and done, the count must be incremented, and the message must be printed.
The compiler has a lot of freedom to re-order operations, and to store values in temporary registers in order to ensure that the correct end result is achieved in the most efficient way possible. So, for example, the count in the expression count+" "+... will not necessarily cause the compiler to fetch the latest value of the global count variable. In fact it probably will not fetch from the global variable because it knows that the result of the + operation still is sitting in a CPU register. And, since it doesn't acknowledge that other threads could exist, then it knows that there's no way that the value in the register could be any different from what it stored into the global variable after doing the +.
Second of all, the hardware itself is allowed to stash values in temporary places and re-order operations for efficiency, and it too is allowed to assume that there are no other threads. So, even when the compiler emits code that says to actually fetch from or store to the global variable instead of to or from a register, the hardware does not necessarily store to or fetch from the actual address in memory.
Assuming your code example is Java code, then all of that changes when you make appropriate use of synchronized blocks. If you would add synchronized to the declaration of your add method for example:
public synchronized void add(int value) {
count=count+value;
System.out.println(count+" "+ Thread.currentThread().getName());
}
That forces the compiler to acknowledge the existence of other threads, and the compiler will emit instructions that force the hardware to acknowledge other threads as well.
By adding synchronized to the add method, you force the hardware to deliver the actual value of the global variable on entry to the method, your force it to actually write the global by the time the method returns, and you prevent more than one thread from being in the method at the same time.

Why is this code not thread-safe even though an AtomicInteger is used to track progress?

I tried making a class that extends thread which simply takes an array of strings and prints the first 2 strings out alternately for 10000 iterations. I keep track of the index to print from using an AtomicInteger (counter), however the output sometimes prints this:
hello
hello
hello
w
hello
hello
etc.
instead of alternating at each iteration. Why is this and how could I fix it without just putting 'synchronized' in the run method?
public class MyThreadDelegate implements Runnable {
List<String> words;
AtomicInteger counter = new AtomicInteger(0);
public MyThread(List<String> words) {
this.words = words;
}
#Override
public void run() {
for (int i = 0; i < 10000; i++) {
System.out.println(words.get(counter.getAndIncrement()%2) + counter.get());
}
}
public static void main(String[] args) {
MyThreadDelegate myThreadDelegate = new MyThreadDelegate(Arrays.asList("hello", "w"));
Thread t1 = new Thread(MyThreadDelegate);
Thread t2 = new Thread(MyThreadDelegate);
t1.start();
t2.start();
}
}
While the numbers are retrieved one by one, the rest of the method isn't synced up. So sometimes this might happen:
t1: gets value 0 from the counter
t2: gets value 1 from the counter
t2: prints w
t1: prints hello
A quick fix would be to put the whole System.out line in a synchronized block, but that would not guarantee that the threads take turns. It just guarantees that echt value is retrieved, incremented and printed before the next one.
If you want to have the threads actually take turns, you'll have to implement some sort of locking. But if you wan't to have the threads wait for each other, why are you using multiple threads?
EDIT: also, you should probably have MyThread implement Runnable instead of extending Thread if you're going to use it this way. See this link for more: https://www.baeldung.com/java-runnable-vs-extending-thread (Solomon Slow beat me to it :)

Locking instances with synchronized block inside non-static method

Going by the below code, I have two instances of class A - a1 and a2. And calling the method foo() on both the instances separately.
There is a synchronized block within the foo() method, which is locked on the calling object. Since it is an instance-level locking, both the methods should start executing at the same time because they are getting invoked from two separate instances. But, they are getting executed sequentially.
Is it because, both the instances getting invoked from the same Thread main?
Code changes: Made class A implements Runnable, renamed foo() to run(), forked a thread t from main, invoked a1.run() from main thread, invoked a2.run() from the thread t . Though the two A instances - a1 & a2 are getting called from two threads - main & thread t, the synchronized block(this) seems to be getting locked.
My understanding was the 'this' is referring to the calling Runnable instance which is different and even the threads are different. So, Thread.sleep should not make the other thread blocked. Then, why the two invocation of run's not happening in parallel?
Expected Output (should execute parallel)
main <time> Inside A.run
Thread-0 <time> Inside A.run
Thread-0 <time+4s> Exiting A.run
main <time+5s> Exiting A.run
Actual Output (executing sequentially)
main <time> Inside A.run
main <time+5s> Exiting A.run
Thread-0 <time+5s> Inside A.run
Thread-0 <time+9s> Exiting A.run
import java.time.*;
import java.time.format.DateTimeFormatter;
public class Test {
public static void main(String[] args) {
/*A a1 = new A(5000); A a2 = new A(4000);
a1.foo(); a2.foo();*/
A a1 = new A(5000); A a2 = new A(4000);
Thread t = new Thread(a2);
/*a1.run(); t.start();*/
t.start(); a1.run(); // <-- putting t.start() before a1.run() solves the issue
}
}
class A implements Runnable {
public long waitTime;
public A() {}
public A(long timeInMs) {
waitTime = timeInMs;
}
public void run() {
synchronized(this) {
try {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
LocalDateTime time = LocalDateTime.now();
System.out.println(Thread.currentThread().getName() + " " + formatter.format(time) + " Inside A.run");
Thread.sleep(waitTime);
time = LocalDateTime.now();
System.out.println(Thread.currentThread().getName() + " " + formatter.format(time) + " Exiting A.run");
} catch (InterruptedException e) {}
}
}
}
You started to run a1 synchronously before starting the thread, of course you will get a1's output on the main thread because it won't have been able to reach the thread start statement until a1 is finished.
Try starting the thread running a2 first before you run a1 on the main thread, see what you get.
You should also be aware that thread scheduling may be delayed, and calling Thread#start does not immediately begin execution on a separate thread, but rather it queues it for the system thread scheduler. You might also want to consider using a synchronization device such as a CyclicBarrier in order to coordinate between the thread running a2 and the main thread running a1, else you may still get the exact same result even though it appears that you are starting the thread to run a1 before a2.
Is it because, both the instances getting invoked from the same
Thread main?
Yes. Calling Thread.sleep() is synchronous and will block the current thread for the duration unless interrupted. You're directly calling a1.foo() which will block the main thread for the duration, which is the result you're seeing. Create separate threads and call foo() in each of them and you'll see the behavior you're expecting.

Is threading not working properly in Nunit?

I think that nunit isn't function properly when threading code is involved:
Here's the sample code:
public class multiply
{
public Thread myThread;
public int Counter
{
get;
private set;
}
public string name
{
get;
private set;
}
private Object thisLock = new Object();
public void RunConsolePrint()
{
//lock(thisLock)
//{
Console.WriteLine("Now thread " + name + " has started");
for (int i = 1; i<= Counter; i++)
{
Console.WriteLine(name + ": count has reached " + i+ ": total count is "+Counter);
}
Console.WriteLine("Thread " + name + " has finished");
//}
}
public multiply(string pname, int pCounter)
{
name = pname;
Counter = pCounter;
myThread = new Thread(new ThreadStart(RunConsolePrint));
}
}
And here's the test code:
[Test]
public void Main()
{
counter=100;
multiply m2=new multiply("Second", counter);
multiply m1 = new multiply("First", counter);
m1.myThread.Start();
m2.myThread.Start();
}
And the output is a sequential running of m1 and m2, which means that the loop in m1 is always execute first before m2, at least that's what my testing shows. I ran the tests a few times and I always get this.
Is this a bug? Or an expected behavior?
If I copy the above code to a console program and run, I can see the threading effect clearly.
I am using the test using TestDriven.net runner.
The exact interleaving of two or more threads cannot really be predicted.
There are a couple of factors to consider for your example. First of all each thread will execute for its quantum before (potentially) getting switched to another thread. You can't expect thread switches to happen on any special place in your code. I.e. one thread might finish before the other one is started (especially since the task is relatively short in your case).
Secondly, since you're writing to the console, the threads are synchronized on that access. This also affects the interleaving.
The result also depends on the number of available core on your machine (as well as the general load on the machine when you run the code).
In short, you cannot predict how the two threads will run.
It's non-deterministic whether m1 or m2 starts executing first. If the counter executes fast enough, I wouldn't be surprised to see one of them finish before the other starts. Change the count to something very large (e.g. a million) and I'm pretty sure you'll see separate threads executing concurrently.
Any result is possible - what result are you seeing that suggests there's a bug?

Resources