Solved

stop a thread when the thread still sleep

Posted on 2002-05-20
13
238 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
split53 challenge 7 95
servlet web applications   metadata-complete="true" or false 3 37
hibernate example issues from command prompt 10 40
null output 3 24
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

805 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