Solved

stop a thread when the thread still sleep

Posted on 2002-05-20
13
236 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
 

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

746 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now