What the way to stop a long running thread ?
Thread.abort() is used to stop a long running thread
What is thread.join() in threading
Consider two threads t1 and t2 are running parallel. t1 calls t2.join(). at this point t1 will pause its execution and wait for t2 to complete. when t2 completes its execution t1 will resume.
there is one more method thread.join(int)
in this let’s say t1 calls t2.join(100). at this point t1 will pause its execution and wait for t2 to complete. but compared to the previous example t1 will wait only for 100 milliseconds. t1 will resume its execution after 100 milliseconds (even if t2 is executing)
What are daemon threads and how can a thread be created as daemon
Threads which are executing in the background are known as daemon threads.
by setting thread.isbackground = true we can make it as a demon thread
a classic example of a daemon thread is garbage collector.
How do you implement synchronization in threading
We can achieve synchronization by using the below methods
1) lock and synclock
3) wait handlers
4) mutex object
5) interlocked class
6) readwriterlock class
How can we know a state of a thread
Thread.threadstate property gives the information about threads current state
What is use of interlocked class
Interlocked classes are used to establish thread synchronization.
let say a global variable “icounter” is accessed by two threads. both the threads want to increment the value by one after some operation. it is necessary to ensure that both threads do not perform this action at the same time.
interlocked.increment(icounter) will help us to solve this problem.
interlocked class contains some methods like increment, decrement , add , compareexchange etc.
these methods will lock the variable and will perform the necessary operation.
interlocked methods are faster compared to the lock statement