Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: set time limit on running of a thread

  1. #1
    Junior Member
    Join Date
    Jul 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default set time limit on running of a thread

    hi all,
    I want to run a program in a new thread and stop it if it takes more time than expected, so there are two threads in my application the parent is some thing like this:

           thread2.start();
     
    	    while(thread2.isAlive())
    		{
    			Calendar currentCalendar = new GregorianCalendar();
    			currentMilliSecond = currentCalendar.get(Calendar.MILLISECOND);
    			if(currentMilliSecond-startMilliSecond > threadTimeLimit || 
    					currentCalendar.get(Calendar.SECOND) > startCalendar.get(Calendar.SECOND))
    			{
    				thread2.stop();
     
    			}
                     }
    and the other thread (thread2) runs a simple program with infinite loop for some specific input. I observed some abnormal behavior from this application, i think some thing is wrong with birth and deaths of threads, is it safe to use this code for restricting the life time of a thread?
    Thanks in advance,
    Zahra


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,310
    Thanks
    56
    Thanked 2,373 Times in 2,344 Posts

    Default Re: set time limit on running of a thread

    Read the API doc for the stop() method. Its all there.

    The stop() method is depreciated and NOT recommended.
    Recommended way is to use a flag to communicate to the thread that it is to exit.

  3. #3
    Junior Member
    Join Date
    Jul 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: set time limit on running of a thread

    Thanks for your advise but i have a limitation that is i can not change the code associated with second thread to have or check any flag because it is generated automatically and i should run for lots of automatically generated code so i need a mechanism to terminate a thread that is not aware of its status.

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: set time limit on running of a thread

    If there is no thread awareness, you cannot safely stop/suspend/resume a thread in general. Depending on your current situation, this may or may not matter (though, it sounds like from your situation you might not have this luxury and will need to safely remove any object locks that thread is holding).

    If you want more information about the problems associated with stop/suspend/resume, see this page: Java Thread Primitive Depracation. They also suggest some alternatives to using these methods, but they are all similar to what we have provided as alternatives here.

Similar Threads

  1. Problem running .jar in Windows 7
    By SpiceProgrammer in forum Java Theory & Questions
    Replies: 3
    Last Post: December 21st, 2011, 01:28 AM
  2. Limit File Size or Request Size
    By tarek.mostafa in forum Java Servlet
    Replies: 3
    Last Post: June 12th, 2010, 05:28 PM
  3. Limit File Size or Request Size
    By tarek.mostafa in forum JavaServer Pages: JSP & JSTL
    Replies: 3
    Last Post: June 11th, 2010, 08:21 AM
  4. Replies: 4
    Last Post: April 27th, 2010, 02:18 AM
  5. Replies: 1
    Last Post: March 3rd, 2009, 08:04 AM