multithreading - Use of lock keywork c# -


i working on c# coding exercise code:

class program {     static object sync = new object();      static void saferun()     {          lock (sync)          {               thread.sleep(1000);          }     }      static void main(string[] args)     {          lock (sync)          {               saferun();          }           console.write("safe");      }    } } 

what printed?

  1. nothing, deadlock occurs.
  2. it not compile.
  3. "safe" print.

i thought deadlock occur, when run code "safe" printed.

so, can explain me why 3 correct , why 1 not correct?

thank you!

for deadlock occur need @ least 2 threads want access resources locked between them.

example:

you have 2 running thread , 2 list<t>.

thread a locked list a. thread a want take value list b

thread b locked list b. thread b want take value list a

now both threads try resources locked between them.


Comments

Popular posts from this blog

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - SSE Emitter : Manage timeouts and complete() -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -