How does static code run with multiple threads?

后端 未结 6 893
青春惊慌失措
青春惊慌失措 2021-01-04 06:38

I was reading Threading from within a class with static and non-static methods and I am in a similar situation.

I have a static method that pulls data from a resourc

6条回答
  •  难免孤独
    2021-01-04 07:16

    One thing you should keep in mind when executing static methods concurrently are static fields, which only exist one time. So, if the method reads and writes static fields, concurrence issues can occur.

    However, there is an attribute called ThreadStaticAttribute which says that for each thread there is a separate field. This can be helpful in some particular scenarios.

    Local variables are separte for each thread, so you don't need to care about this. But be aware of external resources like files, which can be problematic when accessed concurrently.

    Best Regards,
    Oliver Hanappi

提交回复
热议问题