• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

MultiThreaded Programming: Killing Thread

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have encountered a problem while implementing the following requirement,

My requirement:- I have two threads Thread_1 and Thread_2.
Thread_2 performs TASK which can�t be implemented or performed in loop as it is not a single functionality to achieve.
Thread_1 should start the Thread_2 and Thread_2 needs to be stopped after a pre-defined time, say �5 minutes� only in case of Thread_2 has not completed the TASK that it was executing.

I am trying to meet a requirement by following approach

class Thread_2 extends Thread
{
public void run(){
---
---
---
//TASK, tight bound. here for() = =TASK
for (int i=0;i<=100000;i++ )
{
// out of my scope to handle here
}
//end of TASK
-----
}
}

class Thread_1 extends XYZ
//should extend; XYZ implements java.lang.Runnable
{
-----
Public viod run(){
Therad_2 obj2 = new Therad_2();
------
th.start();
// Here is the PROBLEM. I have to stop the thread �obj2� after some �X� time means obj2 thread should run only for �X� time. In short I got to kill the thred.
}// end run()

}// end Thread_1

I would appreciate if any body can have a look.
 
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why dont u create a Runnable with following method:

volatile boolean run = true;

run(){
for (int i=0;i<=100000 && run;i++ )
{

}
}

abort(){
run = false;
}

Also, if the operation is the loop is IO blocked, u have to interrupt the thread. for this u can cache reference to the thrad from run method and then interrupt it in the abort method.

Thread t;

run(){
t = currentThread();
for(){
}
}

abort(){
run = false;
if(t!= null && t.isAlive()){
t.interruprt();
}

Hope that helps.
 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Calling interrupt() will not help if the "TASK" method is blocking for any reason. As per the interrupt documentaion, calling interrupt() will only cause the interrupted flag to be set if the method is blocking.
 
Yaroslav Chinskiy
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sameer Amte,

I am looking at the java docs:

this thread is blocked in an invocation of the wait(), wait(long), or wait(long, int) methods of the Object class, or of the join(), join(long), join(long, int), sleep(long), or sleep(long, int), methods of this class, then its interrupt status will be cleared and it will receive an InterruptedException.

If this thread is blocked in an I/O operation upon an interruptible channel then the channel will be closed, the thread's interrupt status will be set, and the thread will receive a ClosedByInterruptException.

If this thread is blocked in a Selector then the thread's interrupt status will be set and it will return immediately from the selection operation, possibly with a non-zero value, just as if the selector's wakeup method were invoked.

So any IO or wait or sleep will be interrupted. That is why you put try/catch around wait()/sleep().

If one could not interrupt blocking IO then interrupt() whould not make any sense.
 
Sameer Amte
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The documentation mentions only about nio classes that will be interrupted.If a thread is in blocking IO or is in a continuously running loop that does not check Thread.isInterrupted(), it will not get interrupted.
I have tested this in various ways and calling interrupt on a thread does not force it to come out of a blocking IO call or out of a continuously running loop.
If your "TASK" is continuously polling then you might want to check isInterrupted() after a call to your "TASK" method.
 
Yaroslav Chinskiy
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sameer,

Looks like I was wrong in interpretation of the docs.
Simple program proves ur point.


Thanks.
 
Put the moon back where you found it! We need it for tides and poetry and stuff. Like this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic