The Java code demonstrates how to manually stop a thread using the stopThread() method. However, just like the suspend() and resume() methods, the stop() method used in this code is also deprecated in Java, and it's not recommended for modern applications. The stop() method forcibly terminates the thread, which can lead to potential data corruption and other issues.
Instead of using stop(), a safer and more graceful way to stop a thread is to use cooperative cancellation. This involves setting a flag to indicate that the thread should stop its execution, and then letting the thread exit its run loop based on that flag. Here's a brief explanation of how the code works:
public class ThreadStop { public static void main(String[] args) { MyThread myThread = new MyThread(); myThread.start(); try { Thread.sleep(3000); // Sleep for 3 seconds } catch (InterruptedException e) { e.printStackTrace(); } myThread.stopThread(); // Stop the thread } static class MyThread extends Thread { public void run() { while (!Thread.currentThread().isInterrupted()) { System.out.println("Thread is running..."); try { Thread.sleep(1000); // Sleep for 1 second } catch (InterruptedException e) { Thread.currentThread().interrupt(); // Restore interrupted status } } System.out.println("Thread stopped."); } public void stopThread() { interrupt(); } } }
Thread is running... Thread is running... Thread is running... Thread stopped.
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions