问题
I want to stop my thread after particular time. Here with i added my sample code. Any one of the thread takes more than 6 seconds means i want to stop the particular thread. Rest of the thread should not be affected. Do i need to use the Timer or some better way can we achieve? Suppose if i used Timer means do i need to create new timer for each thread and do i need to cancel the timer suppose if the job finished before 6secs?
class MyRunnableThread implements Runnable{
public static int myCount = 0;
private String fname = null;
public MyRunnableThread(String fname){
this.fname = fname;
}
public void run() {
try{
//read the content based on text and peforms the operation
//File.read(this.fname)
System.out.println("Run method stuff"+ this.fname);
} catch (Exception iex) {
System.out.println("Exception in thread: "+iex.getMessage());
}
}
}
public class RunMyThread {
public static void main(String a[]){
System.out.println("Starting Main Thread...");
String[] fname = {"file1.txt","file2.txt","file3.txt","file4.txt","file5.txt"};
for(int i=0;i<5;i++){
try{
MyRunnableThread mrt = new MyRunnableThread(fname[i]);
Thread t = new Thread(mrt);
System.out.println("Main Thread start "+i);
t.start();
//stop the thread support run method takes more than 6seconds
System.out.println("Main Thread end "+ i );
} catch (Exception iex){
System.out.println("Exception in main thread: "+iex.getMessage());
}
}
System.out.println("End of Main Thread...");
}
}
回答1:
User a Timer and a flag to tell the thread to stop:
public class MyRunnableThread implements Runnable {
boolean run = true;
String fname;
public MyRunnableThread(String fname) {
this.fname = fname;
}
public void setRun(boolean toSet) {
run = false;
}
public void run() {
while (run) {
doStuff();
}
}
private void doStuff() {
// File.read(fname);
}
}
How to use it:
import java.util.Timer;
import java.util.TimerTask;
public class Tester {
public static void main(String args[]) {
String[] fname = { "file1.txt", "file2.txt", "file3.txt", "file4.txt", "file5.txt" };
for (int i = 0; i < 5; i++) {
try {
MyRunnableThread mrt = new MyRunnableThread(fname[i]);
Thread t = new Thread(mrt);
t.start();
final Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
int i = 6; // Time in seconds
public void run() {
System.out.println(i--);
if (i < 0) {
timer.cancel();
mrt.setRun(false);
}
}
}, 0, 1000);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
Change i to whatever time in seconds you want
回答2:
One thought would be you have an ArrayList containing all your threads.
Then you make each thread have an extra field (Timestamp started)
Then you have a timer looping through the Arraylist and stopping the threads where started > 6 seconds ago
回答3:
Well.. there're a lot of ways to do it, I'll explain you one of those.
You can create a custom class that extends Thread
and then implement the method run()
and CancelCurrentThread()
, your run will start the Thread
and CancelCurrentThread()
will stop it ignoring the exceptions.
Then your code should look like :
mCustomThread thread = new mCustomThread(parametres_if_you_need);
thread.start(); //Start the Thread
thread.join(6000); //Adding 6 seconds
//You can detect if the 6 seconds have passed or not for example using a timer, then if it joins on this if condition you can call
thread.CancelCurrentThread();
来源:https://stackoverflow.com/questions/36151503/stop-the-thread-after-a-particular-time