Stopping a Java thread
I came across a question of how to stop a Java thread. I suddenly felt about the stop and other methods in the Thread class. Then i slowly remembered that they are no longer available and they are deprecated.
Then i started searching in net. Then i found a solution(i was not known that time that it was an ancient technique). It said we have to keep a volatile flag variable and we can check the status in the run(). It sounded good to me.
But when i was reading till end of the document, i came across how to stop a thread in non-running situations (wait or sleep implemented thread, I/O blocked thread i.e. thread reading or writing files). In those situations we can use interrupt method to do it efectively.
Hmmm its been so long since i used threads in my application. I used thread last when i was developing my network file transfer application. The application came very well than i expected. It doesnt even needed any file/directory access permission. I used a special mechanism in it. In that i was using multithreaded server. That was the last time i used effectively. Sounds like i have to brush up core java a bit.
I came across concurrent package when reading about this interrupt method. Hope to write about it soon.
I noted many people getting confused with volatile and transient variables in java.
volatile variable is a kind of static variable which shares its value among the threads. To be precise, the value of a volatile variable can be shared among threads and can be simulataneusly accessed or modified.
transient variable is a used to restrict a field from being serialized.