Qus:-How to stop a thread in java? Explain about sleep () method in a thread? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 6

Qus:-How to stop a thread in java? Explain about sleep () method in a thread?

19th May 2019, 9:52 AM
Deepika Mourya
Deepika Mourya - avatar
3 Answers
+ 1
what do you mean by stop thread??
19th Jul 2019, 7:18 PM
Sai Ram
+ 1
sleep(1000) that makes your thread to sleep or wait for 1000milli seconds(1sec) before processing.
19th Jul 2019, 7:19 PM
Sai Ram
+ 1
thread is automatically destroyed when the run() method has completed. But it might be required to kill/stop a thread before it has completed its life cycle. Previously, methods suspend(), resume() and stop() were used to manage the execution of threads. But these methods were deprecated by Java 2 because they could result in system failures. Modern ways to suspend/stop a thread are by using a boolean flag and Thread.interrupt() method. Using a volatile boolean flag: We can also use a volatile boolean flag to make our code thread safe. A volatile variable is directly stored in the main memory so that threads cannot have locally cached values of it. A situation may arise when more than one threads are accessing the same variable and the changes made by one might not be visible to other threads. In such a situation, we can use a volatile boolean flag.
30th Jul 2019, 7:51 PM
Aditi
Aditi - avatar