Solved

stop a thread when the thread still sleep

Posted on 2002-05-20
13
239 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
  • 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:chencc77
ID: 7021252
0
 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
java jdbc batch example 8 41
tomcat startup error 5 106
Delete Android all or certain applications data programmatically 9 76
jmss example java 2 23
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This video teaches viewers about errors in exception handling.

828 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