I
Icarus
The situation: have a program that consists of some 50+ classes. One
of the methods has to be interrupted after a set period of time. Hence
why I am using a Thread for this. The problems are:
- When the interrupt occurs, the program has to reverse some changes.
Having an InterruptedException thrown anywhere in the code is bad, as
I can't find out on which line exactly the exception happened and
which of the changes have already been made.
- The program calls multiple methods AND has to be as fast as
possible, so doing a manual check to Thread.currentThread
().isInterrupted() in every method is bad.
Below is some code replicating the problem. The goal is that the check-
attribute is set to the value "true" without having to change every
On a side note: the problem could easily be solved by having the
program check the time, i. e. passing the duration and starting time
and replacing
if(Thread.currentThread().isInterrupted()){
with
if(System.currentTimeMillis() - startingtime > duration){
.. However, this will be calculated a few thousand times. Is this
really more resources.heavy than creating a Thread for it?
package threads;
public class ThreadsJoin {
public ThreadsJoin(){
Ausfuehr ausfuehr = new Ausfuehr();
Thread thread = new Thread(ausfuehr);
thread.start();
try {
thread.join(1000);
System.out.println("Executed after join() without
InterruptedException");
} catch (InterruptedException e) {}
thread.interrupt();
System.out.println("Index: " + ausfuehr.index);
System.out.println("Check: " + ausfuehr.check);
}
/**
* This method spends some time manipulating values. It is to
be checked whether the interrupt
* occurs during this manipulation.
*/
public static void manipulate(){
int m=0;
for(int n=0; n<10000000; n++){
m++;
}
}
public static void main(String[] args) {
new ThreadsJoin();
}
class Ausfuehr implements Runnable{
long index;
boolean check;
Thread father;
boolean istUnterbrochen;
Ausfuehr(){
this.index = 0;
}
@Override
public void run() {
istUnterbrochen = false;
this.check = false;
this.runIndef();
this.check = true;
}
void runIndef(){
while(true){
if(Thread.currentThread().isInterrupted()){
return;
}
ThreadsJoin.manipulate();
index++;
}
}
}
}
of the methods has to be interrupted after a set period of time. Hence
why I am using a Thread for this. The problems are:
- When the interrupt occurs, the program has to reverse some changes.
Having an InterruptedException thrown anywhere in the code is bad, as
I can't find out on which line exactly the exception happened and
which of the changes have already been made.
- The program calls multiple methods AND has to be as fast as
possible, so doing a manual check to Thread.currentThread
().isInterrupted() in every method is bad.
Below is some code replicating the problem. The goal is that the check-
attribute is set to the value "true" without having to change every
On a side note: the problem could easily be solved by having the
program check the time, i. e. passing the duration and starting time
and replacing
if(Thread.currentThread().isInterrupted()){
with
if(System.currentTimeMillis() - startingtime > duration){
.. However, this will be calculated a few thousand times. Is this
really more resources.heavy than creating a Thread for it?
package threads;
public class ThreadsJoin {
public ThreadsJoin(){
Ausfuehr ausfuehr = new Ausfuehr();
Thread thread = new Thread(ausfuehr);
thread.start();
try {
thread.join(1000);
System.out.println("Executed after join() without
InterruptedException");
} catch (InterruptedException e) {}
thread.interrupt();
System.out.println("Index: " + ausfuehr.index);
System.out.println("Check: " + ausfuehr.check);
}
/**
* This method spends some time manipulating values. It is to
be checked whether the interrupt
* occurs during this manipulation.
*/
public static void manipulate(){
int m=0;
for(int n=0; n<10000000; n++){
m++;
}
}
public static void main(String[] args) {
new ThreadsJoin();
}
class Ausfuehr implements Runnable{
long index;
boolean check;
Thread father;
boolean istUnterbrochen;
Ausfuehr(){
this.index = 0;
}
@Override
public void run() {
istUnterbrochen = false;
this.check = false;
this.runIndef();
this.check = true;
}
void runIndef(){
while(true){
if(Thread.currentThread().isInterrupted()){
return;
}
ThreadsJoin.manipulate();
index++;
}
}
}
}