Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Thread order execution?

I have this simple code : (which i run in linqpad)

void Main()
{
    for ( int  i=0;i<10;i++)
     {
      int  tmp=i;
      new Thread (() =>doWork(tmp)).Start();
     }
}

 public void doWork( int h)
 {
 h.Dump();
 }

the int tmp=i; line is for capture variable - so each iteration will have its own value.

2 problems :

1) the numbers are not sequential , while thread execution is !

2) sometimes i get less than 10 numbers !

here are some executions outputs:

enter image description hereenter image description hereenter image description hereenter image description here

questions :

1) why case 1 is happening and how can i solve it ?

2) why case 2 is happening and how can i solve it ?

like image 639
Royi Namir Avatar asked May 31 '12 11:05

Royi Namir


People also ask

How do you execute a thread order?

Executing threads in Sequence in Java Thing to do here is you start the thread and call the join() method on the same thread. This makes it to wait until the thread stops executing. That way order is ensured.

Can we decide order of execution of threads?

You cannot tell the thread scheduler which order to execute threads in. If you need to ensure that a certain piece of code which is running on thread A must run before another piece of code running on thread B, you must enforce that order using locks or wait() / notify() .

Which thread will execute first?

When multiple threads are ready to execute, the JVM selects and executes the Runnable thread that has the highest priority. If this thread stops or becomes not runnable, the lower-priority threads will execute. In case two threads have the same priority, the JVM will execute them in FIFO order.

What happens when a thread finishes execution?

Thread Termination After finish their work, threads terminate. In the quicksort example, after both array subsegments are sorted, the threads created for sorting them terminate. In fact, the thread that creates these two child threads terminates too, because its assigned task completes.


2 Answers

It should not be expected that they are sequential. Each thread gets priority as the kernel chooses. It might happen that they look sequential, purely by the nature of when each is started, but that is pure chance.

For ensuring that they all complete - mark each new thread as IsBackground = false, so that it keeps the executable alive. For example:

new Thread(() => doWork(tmp)) { IsBackground = false }.Start();
like image 188
Marc Gravell Avatar answered Oct 04 '22 01:10

Marc Gravell


Threads execute in unpredictable order, and if the main thread finishes before others you'll not get all the numbers (dump() will not execute). If you mark your threads as IsBackground = false you'll get them all. There's no real solution for the first one except not using threads (or joining threads, which is same thing really).

like image 22
Fedor Hajdu Avatar answered Oct 03 '22 23:10

Fedor Hajdu