J
James Gralton
Hi all,
I have a question and am not sure if it is even possible but would appreciate any advice. First I
will show my small self contained example of my issue. This isn't my application just a very
simplistic view of what I am trying to achieve.
public class Main {
public static void main(String[] args) {
Main m = new Main(true);
}
public Main(boolean flag){
this.flag = flag;
}
private boolean flag = false;
private boolean terminate = false;
public void setTerminate(boolean terminate){
this.terminate = terminate;
}
public void Task1(){
Thread t = new Thread(new Runnable() {
public void run() {
while(!terminate){
try{
Thread.sleep(1000);
}
catch(Exception e){
e.printStackTrace();
}
}
//If I get here I want to return from task 1 regardeless of whether
//Tasks 2, 3 or 4 have finished.
}
});
System.out.println("Doing Task 1");
}
public void Task2(){
System.out.println("Doing Task 2");
if(flag){
}
else{
}
}
public void Task3(){
for(int i=0;i<5;i++){
System.out.println("Doing Task 3");
try{
Thread.sleep(1000);
}catch(Exception e){
e.printStackTrace();
}
}
}
public void Task4(){
for(int i=0;i<7;i++){
System.out.println("Doing Task 4");
try{
Thread.sleep(1000);
}catch(Exception e){
e.printStackTrace();
}
}
}
}
Essentially what I would like to to is kick of a Thread in Task1 which monitors a terminate flag
which is set elsewhere in my application (set when a user wants to cancel a long operation). When
the terminate flag is set I would like to stop the tasks I am performing (regardless of whether I am
currently executing Task 2, 3 or 4 in the chain) and return from Task1 to the calling method. I
can't simply System.exit() as the user will likely have cancelled the operation so they can carry on
with something else in the application.
Is there anyway of doing this?
All help/advice is gratefully appreciated.
James
I have a question and am not sure if it is even possible but would appreciate any advice. First I
will show my small self contained example of my issue. This isn't my application just a very
simplistic view of what I am trying to achieve.
public class Main {
public static void main(String[] args) {
Main m = new Main(true);
}
public Main(boolean flag){
this.flag = flag;
}
private boolean flag = false;
private boolean terminate = false;
public void setTerminate(boolean terminate){
this.terminate = terminate;
}
public void Task1(){
Thread t = new Thread(new Runnable() {
public void run() {
while(!terminate){
try{
Thread.sleep(1000);
}
catch(Exception e){
e.printStackTrace();
}
}
//If I get here I want to return from task 1 regardeless of whether
//Tasks 2, 3 or 4 have finished.
}
});
System.out.println("Doing Task 1");
}
public void Task2(){
System.out.println("Doing Task 2");
if(flag){
}
else{
}
}
public void Task3(){
for(int i=0;i<5;i++){
System.out.println("Doing Task 3");
try{
Thread.sleep(1000);
}catch(Exception e){
e.printStackTrace();
}
}
}
public void Task4(){
for(int i=0;i<7;i++){
System.out.println("Doing Task 4");
try{
Thread.sleep(1000);
}catch(Exception e){
e.printStackTrace();
}
}
}
}
Essentially what I would like to to is kick of a Thread in Task1 which monitors a terminate flag
which is set elsewhere in my application (set when a user wants to cancel a long operation). When
the terminate flag is set I would like to stop the tasks I am performing (regardless of whether I am
currently executing Task 2, 3 or 4 in the chain) and return from Task1 to the calling method. I
can't simply System.exit() as the user will likely have cancelled the operation so they can carry on
with something else in the application.
Is there anyway of doing this?
All help/advice is gratefully appreciated.
James