[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

stop a thread when the thread still sleep

Posted on 2002-05-20
13
Medium Priority
?
255 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 92

Accepted Solution

by:
objects earned 400 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

650 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