Results 1 to 5 of 5
  1. #1
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default Terminating a Thread

    I want to terminate Thread-0 via the main Thread. Is it possible? When I tried using the thread.destroy() method, it said this method is deprecated. Also, destroy() does not cleanup (what does that mean? Would cleanup effect the classes below?)

    ClassA
    Java Code:
    public class ClassA {
    
    	
    	public int runInfinitely(long num) {
    			System.out.println(num);
    			return runInfinitely(num - 1);
    	}
    	
    }
    ClassB
    Java Code:
    public class ClassB implements Runnable {
    	
    	public void run() {
    		ClassA test = new ClassA();
    		test.runInfinitely(System.currentTimeMillis());
    	}
    }
    DriverProgram
    Java Code:
    public class DriverProgram {
    	public static void main(String[] args) throws InterruptedException {
    		Thread thread1 = new Thread(new ClassB());
    		thread1.start();
    		Thread.sleep(1000);
    		thread1.interrupt();
    		thread1.join();
    	}
    
    }
    Is it possible without doing this?

    ClassA
    Java Code:
    public class ClassA {
    
    	
    	public int runInfinitely(long num) throws InterruptedException {
    			Thread.sleep(1);
    			System.out.println(num);
    			return runInfinitely(num - 1);
    	}
    	
    }
    ClassB
    Java Code:
    public class ClassB implements Runnable {
    	
    	public void run() {
    		ClassA test = new ClassA();
    		try {
    			test.runInfinitely(System.currentTimeMillis());
    		} catch (InterruptedException e) {
    			return;
    		}
    	}
    }
    DriverProgram
    Java Code:
    public class DriverProgram {
    	public static void main(String[] args) throws InterruptedException {
    		Thread thread1 = new Thread(new ClassB());
    		thread1.start();
    		Thread.sleep(1000);
    		thread1.interrupt();
    		thread1.join();
    	}
    
    }
    Last edited by Lil_Aziz1; 01-30-2010 at 11:37 PM.
    "Experience is what you get when you don't get what you want" (Dan Stanford)
    "Rise and rise again until lambs become lions" (Robin Hood)

  2. #2
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    I did some research on terminating it a different way. So my new question is: Would the code below be the most congruous way of terminating a thread?

    Java Code:
    public class ClassA {
    	
    	private volatile boolean stopA = false;
    	
    	public synchronized void stopRecursion() {
    		stopA = true;
    	}
    	
    	public int runInfinitely(long num) {
    		while (!stopA) {
    			if (num == 0)
    				return 0;
    			else {
    				System.out.println(num);
    				return runInfinitely(num - 1);
    			}
    
    		}
    		return 0;
    	}
    	
    }
    Java Code:
    public class ClassB implements Runnable {
    	
    	private ClassA test;
    	
    	public ClassB() {
    		test = new ClassA();
    	}
    	public synchronized void stopRun() {
    		test.stopRecursion();
    	}
    	
    	public void run() {
    		test.runInfinitely(System.currentTimeMillis());
    	}
    }
    Java Code:
    public class DriverProgram {
    	public static void main(String[] args) throws InterruptedException {
    		ClassB b = new ClassB();
    		Thread thread1 = new Thread(b);
    		thread1.start();
    		Thread.sleep(50);
    		b.stopRun();
    
    	}
    
    }
    "Experience is what you get when you don't get what you want" (Dan Stanford)
    "Rise and rise again until lambs become lions" (Robin Hood)

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,776
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Lil_Aziz1 View Post
    I did some research on terminating it a different way. So my new question is: Would the code below be the most congruous way of terminating a thread?
    Yes, if a thread is to be stopped by another thread the first thread has to cooperate, i.e. the other thread can only signal the first thread that it has to stop itself. Otherwise resources (such as mutexes etc.) might end up in an inconsistent state or be lost for the posterity. All of the Thread's stop() or kill() or whatever methods were a mistake more or less.

    kind regards,

    Jos

  4. #4
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    Thank you. :) One more thing, can this thread be moved to Threads and Synchronization?
    "Experience is what you get when you don't get what you want" (Dan Stanford)
    "Rise and rise again until lambs become lions" (Robin Hood)

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Lil_Aziz1 View Post
    Thank you. :) One more thing, can this thread be moved to Threads and Synchronization?
    Yes it can.

Similar Threads

  1. Replies: 1
    Last Post: 10-08-2009, 05:45 PM
  2. RMI Server Randomly Terminating
    By danberg in forum Networking
    Replies: 2
    Last Post: 04-26-2009, 04:36 AM
  3. terminating the page if user sits idle.
    By shivakumari in forum Java Servlet
    Replies: 9
    Last Post: 04-11-2009, 08:46 AM
  4. forcibly terminating io operations
    By arnab321 in forum CLDC and MIDP
    Replies: 2
    Last Post: 11-15-2008, 09:51 PM
  5. terminating a while loop with a string
    By tkdvipers in forum New To Java
    Replies: 3
    Last Post: 07-10-2007, 12:23 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •