Solved

stop a thread when the thread still sleep

Posted on 2002-05-20
13
244 Views
Last Modified: 2010-03-31
I have a thread (threadA) that will execute a task at a time (timeA), and i have another class that will stop threadA. threadA will sleep and wait until the time for it to run.

my question is :
1. how can i stop the thread
2. i wonder can i change the sleep time during it is sleeping.

Thanks
0
Comment
Question by:chencc77
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
13 Comments
 

Author Comment

by:chencc77
ID: 7021099
I have done the testing, my code :

//Main class
import java.util.*;
import java.io.*;
import java.text.*;

public class Main{

public static SubmitTask Instance;
     public Main() {}

    public static void main (String args[]) {
          Main ttm = new Main();
          Thread1 t1 = new Thread1();
          Thread2 ct = new Thread2();
          t1.interrupt();

     }

}        

//Thread1
import java.util.*;
import java.io.*;
import java.text.*;

public class Thread1 extends Thread {
     boolean live = true;
     boolean threadAlive = true;
     public static Thread1 Instance;
     public Thread1() {
          Instance = this;
          start();
     }
     
     public void run() {
     
          while (live) {
               try {
                    System.out.println("1");    
                    sleep(10000);
                    if (threadAlive)
                         System.out.println("DO");      
                    else
                         System.out.println("don DO");      
                         
               } catch (Exception e) {
                    System.out.println("Interrupted sleep:Thread1" + e);
                    }
          }
     }

     public void setThreadLive()
     {
          this.interrupt();
          live = false;
                threadAlive = false;

     }
     public void printText() {
          System.out.println("thread is alive " + this.isAlive());
     }
}

//Thread2
import java.util.*;
import java.io.*;
import java.text.*;

public class Thread2 extends Thread{
     int jobID = 0;
     public Thread2() {
     start();
     }

     public void run() {
     
          while (true) {
               try {
                    System.out.println("Thread2");    
                    sleep(2000);
                    Thread2.Instance.setThreadLive();
                    if (!Thread2.Instance.isAlive())
                         break;
                    Thread2.Instance.printText();
                   
               } catch (Exception e) {
               System.out.println("Interrupted sleep:Thread2" + e);}
          }
     }

     
}


//****** END

i noticed that  t1.interrupt() in Main class din stop the thread1 ( should be like that), but when Thread1.Instance.setThreadLive() is calling in Thread2, Thread1 is stop.

0
 
LVL 9

Expert Comment

by:Venci75
ID: 7021244
1. use
Thread1.Instance.destroy();
to stop your thread. Interrupt will cause InterruptedException to be throws by the sleep() method.

2. no way to change the sleep time
0
 

Author Comment

by:chencc77
ID: 7021249
please refer my another question, sorry.

http://www.experts-exchange.com/jsp/qYourQuestion.jsp
0
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
LVL 92

Accepted Solution

by:
objects earned 100 total points
ID: 7022751
> 1. how can i stop the thread

thread.live = false;
thread.interrupt();

public void setThreadLive()
{
   live = false;
   this.interrupt();
   threadAlive = false;
}

(As at 1.3 destroy() is documented as not implemented).

> 2. i wonder can i change the sleep time
> during it is sleeping.

No, you'd need to interrupt the sleep and start a new one.

> but when Thread1.Instance.setThreadLive()
> is calling in Thread2, Thread1 is stop.

Yes, thats because setThreadLive() stops the Thread1 instance regardless of where it's called.

0
 

Author Comment

by:chencc77
ID: 7022839
hi objects,
in setThreadLive, what is the different between

  live = false;
  this.interrupt();

AND

  this.interrupt();
  live = false;


0
 
LVL 92

Expert Comment

by:objects
ID: 7022858
If you don't set live to false before u interrupt the thread, then the thread may not stop.
This is because the thread gets interrupted, and continues executing and may test the value of live before you you set it.
0
 

Author Comment

by:chencc77
ID: 7022892
Thanks objects,
i would like to create a numbers of Thread1.class and put in a Vector, as i to need to put reference to each Thread1, so i have initiate each Thread1 with a name, eg:

1. Vector threadList = new Vector(100);
Thread1 t1 = new Thread1()
threadList.addElement(t1);
Thread1 t2 = new Thread1()
threadList.addElement(t1);
Thread1 t3 = new Thread1()
threadList.addElement(t1);
Question: is there any way to dynamic generate class instance (t1.t2,t3...t100)?

2 . another way to refer to Thread1 is using Vector Instance,
as in.

http://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=java&qid=20302500

Question : which method is better to create instance of Thread1.
0
 
LVL 92

Expert Comment

by:objects
ID: 7022917
You could setup a loop to create them:

Vector threadList = new Vector(100);
for (int i=0; i<100; i++)
{
  Thread t = new Thread1();
  threadList.add(t);
}

The best approach really depends on your requirements.
0
 

Author Comment

by:chencc77
ID: 7022943
My requirement is simple, i just want to create numbers of Instance of Thread1.class, and i can use Thread2 to interrupt the thread1(particular Thread1) with new sleepInterval. Thanks for ur help, i need to test on it 1st. izit  correct i can refer to particular Thread1 by vector index ?

0
 
LVL 92

Expert Comment

by:objects
ID: 7022961
> i just want to create numbers of Instance of
> Thread1.class, and i can use Thread2 to interrupt the
> thread1(particular Thread1) with new sleepInterval

Then just have Thread2 create the Thread1 instances as above, or pass the Vector to it.

>  i can refer to particular Thread1 by vector index ?

Yes.
0
 

Author Comment

by:chencc77
ID: 7023002
thanks, i got what i need, i will close this question and let another open so i can put my question if i face same problems. Thanks again. ^-^
0
 
LVL 92

Expert Comment

by:objects
ID: 7023010
No worries :-)

http://www.objects.com.au
Brainbench MVP for Java 1
http://www.brainbench.com
0

Featured Post

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

695 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question