Results 1 to 9 of 9
  1. #1
    sunny_sykes is offline Member
    Join Date
    Mar 2009
    Posts
    4
    Rep Power
    0

    Default JAva thread doubt

    I am stuck with one question.

    I have one main thread which spawns a child thread.
    now main thread is calling some X () function and child thread is busy in some other function(pinging tomcat in infinite loop).

    When my main thread comes back from X() the child thread should also terminate from its function then and there only.

    Is there any way out.For time being I am using flag to do the same but there must be some way to do it at thread level.


    Kindly reply..
    Thanks

  2. #2
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    7

    Default

    Please show some code, your progress - much easier for us to help and it proves you haven't done nothing. xD
    I die a little on the inside...
    Every time I get shot.

  3. #3
    ron87 is offline Member
    Join Date
    Mar 2009
    Posts
    52
    Rep Power
    0

    Default

    provide a code to demonstrate your problem...it will be easy to analyze

  4. #4
    sunny_sykes is offline Member
    Join Date
    Mar 2009
    Posts
    4
    Rep Power
    0

    Default

    -----------main thread---------------------------------------------
    DTomcat task = new DT();
    //tasksubmitter.createTasksForIPsAndAddToTomcat();
    Thread taskSubmitterThread=new Thread(task);
    taskSubmitterThread.start();

    //This will spawn a child thread from the main method



    boolean success = tracker.waitDC();
    tasksubmitter.timeOutFlag=true;//after main thread return it makes this flag true which i want to avoid.




    ------------------------------------------------------------
    Child thread
    -------------
    it is in fuction which is in infinite loop pinging tomcat

    while(){


    if(timeOutFlag){
    timeOutFlag=false;
    break;
    }//I don want this manual check instead of some thread level concept will serve my purpose.
    try {
    Thread.sleep();
    }
    catch (InterruptedException e) {
    e.printStackTrace();
    }



    }
    -----------------------------------------------------------------------

  5. #5
    anjali is offline Member
    Join Date
    Mar 2009
    Posts
    9
    Rep Power
    0

    Default

    hi,
    when main thread returns from the function there u can call cancel method on child thread object.this method will cancel the child thread.it will stop working.try it and let me know.

  6. #6
    sunny_sykes is offline Member
    Join Date
    Mar 2009
    Posts
    4
    Rep Power
    0

    Default

    HI ,
    I think for cancelation of thread you need to use interrupt.

    thread.interrupt () requests the thread to cancel its activities.

    I tried this also but child thread is not coming out .

    Thanks

  7. #7
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Thumbs up

    Quote Originally Posted by sunny_sykes View Post
    Java Code:
    while(){
    
    
    	if(timeOutFlag){ 
    					timeOutFlag=false;
    					break;
    				}//I don want this manual check instead of some thread level concept will serve my purpose.
    IMHO, you DO want the manual check. Attempts to asynchronously kill threads can lead to disaster, which is why those methods were deprecated early on in Java. But you need everything synchronized. Here is a basic construct I frequently use, that ensures there can be no deadlocks or unexpected behavior. You are welcome to use it as long as you do not copyright it. If you do use it in production code, some credit would be nice.

    Java Code:
    abstract class CooperativeThread<T> extends Thread {
    
      /*
       * Demonstrates operation
       */
      public static void main(String[] args) {
        final CooperativeThread<String> ct = new CooperativeThread<String>() {
          public void doWork(String s) {
    	System.out.println(s);
          }
    
          public void handleError(Throwable t) {
    	try {
    	  t.printStackTrace();
    	} catch (Throwable error) {
    	  // if System.out has been overridden, you could end up here
    	}
          }
        };
    
        ct.start();
    
        new Thread() {
          public void run() {
    	for(int i=0; ; i++) {
    
    	  if(!ct.addWork("adding some work "+i))
    	    break;
    
    	  try {
    	    sleep(100l);
    	  } catch (InterruptedException ex) {}
    	}
    
    	System.out.println("Unable to add work. CT has been shutdown.");
          }
        }.start();
        
        // wait somewhere between 1 and 10 seconds
        try {
          Thread.sleep((long) (Math.random()*10000.0+1000));
        } catch (InterruptedException ex) {}
    
        ct.shutdown();
      }
    
      boolean keepGoing = true;
      T work = null;
    
      public synchronized boolean addWork(T w) {
        if(!keepGoing) return false;
    
        work = w;
        notify();
         
        return true;
      }
    
      public synchronized void run() {
        try {
          while(keepGoing) {
    
    	while(keepGoing && work == null) {
    	  try {
    	    wait();
    	  } catch (InterruptedException ex) {}
    	}
    
    	if(!keepGoing)
    	  break;
    
            doWork(work);
    
            work = null;
          }
    
        } catch (Throwable t) {
          handleError(t); // deadlock under no condition
        }  
        keepGoing = false;
        notify();
      }
    
      public synchronized void shutdown() {
        if(!keepGoing) return;
    
        keepGoing = false;
    
        notify();
    
        try {
          wait();
        } catch (InterruptedException ex) {}
      }
    
      /*
       * Here's where you do your work.  
       */
      public abstract void doWork(T work);
    
      /*
       * Code this with extreme caution.  If it throws anything, you will
       * probably end up with a deadlock.
       */
      public abstract void handleError(Throwable t);
    }

  8. #8
    anjali is offline Member
    Join Date
    Mar 2009
    Posts
    9
    Rep Power
    0

    Default

    u can directly call thread.cancel() from where u want to terminate the thread without interrupt.i hv tried it.just chk it out .

  9. #9
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    Quote Originally Posted by anjali View Post
    u can directly call thread.cancel() from where u want to terminate the thread without interrupt.i hv tried it.just chk it out .
    There is no cancel method in the Thread class. If you meant 'stop', that method is deprecated because it's inherently unsafe.

    Note the following comment from the Thread API's stop method:

    Many uses of stop should be replaced by code that simply modifies some variable to indicate that the target thread should stop running. The target thread should check this variable regularly, and return from its run method in an orderly fashion if the variable indicates that it is to stop running.

Similar Threads

  1. Java Swing JTable Simple Doubt
    By hemanthjava in forum AWT / Swing
    Replies: 1
    Last Post: 11-26-2008, 02:46 PM
  2. Replies: 3
    Last Post: 11-06-2008, 05:24 PM
  3. Replies: 0
    Last Post: 01-28-2008, 08:02 AM
  4. A great doubt in Java Applet,will u solve it!!
    By anithababu in forum Java Applets
    Replies: 6
    Last Post: 01-27-2008, 02:20 PM

Posting Permissions

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