What is the skeleton of your run()?
like:
public void run() {
...
extern_func1(...);
...
}or:
public void run() {
while(true) {
...
extern_func1(...);
...
}
}
like:
public void run() {
...
extern_func1(...);
...
}or:
public void run() {
while(true) {
...
extern_func1(...);
...
}
}
解决方案 »
- 项目里面的txt文件每次都要刷新才能加载
- 急救啊,struts2一个问题搞了好久没搞出来!!!!!!
- 如何用JAVA监听键盘输入啊。并写入到TXT文件。
- socket编程中InputStream提供的方法效率会不会太低?
- 扫雷的展开怎么做啊?怎么递归?【超级菜鸟】
- 关于SimpleDateFormate.parse(String para)不能检查日期的合法性问题
- 这个异常怎么解决
- 狂郁闷!!用java提交http请求返回错误,把字符串贴到ie里就返回ok!!!!!!!
- 关于浮点数
- 请教:怎样在页面timout时自动关闭页面。
- 常常在Java程序中见到this,请问这个this是做什么用的?
- 好就没来csdn了,愿大家都能欢欢喜喜的过个大年!
...
extern_func1(...);
...
}extern_func1(...);是一个别人写的关于ftp类的函数,实际使用中发现这个ftp类在连接有问题的ftp服务器时会出现停在某处不再反应的问题,于是我希望从外面强行中断它
while(!cancel){
//do anything u want
}
}然后public void cancel(){
cancel = true;
}要废掉这个thread,调用cancel就行
其中倒数第3问,比较适合。
问题正如helpall()说的那样,程序会在run()中一个函数上停住,而不会再运行到while(!cancel)处。另外:volatile是什么啊?麻烦解释一下。
Here's a little test program to facilitate your testing. If you really can work it out, I will create a new posting to score you 200 points. I am serious.
class ThreadTest extends Thread {
public volatile boolean isCancel = false; public static void main(String[] args) {
ThreadTest t = new ThreadTest();
t.start();
wait(10);
t.isCancel = true;
System.out.println("\nMain ends.");
wait(10);
} public void run() {
while(!isCancel) {
System.out.println("Enter endless loop");
for(int i = 0;i < 300 ; i++) {
wait(1);
System.out.print(i+" ");
}
}
System.out.println("End endless loop");
} public static void wait(int second) {
try{
Thread.sleep(second * 1000);
}catch(Exception e){}
}
}
However, I've figured out how to abort or cancel a living thread.
Hope this time I didnt make any mistakes.
Any suggestion will be highly appreciated./*******************Poster starts here****************************/
public class Poster implements Runnable { private String query = null;
private volatile boolean aborting = false;
private boolean isCanceled = false; Poster() {
Thread t = new Thread(this);
t.start();
} public synchronized void sendPost(String query) {
this.query = query;
notify();
} public void run() {
running:
while (!aborting) {
String query;
synchronized (this) {
System.out.println("I'm still eligible to doSend");
while (this.query == null) {
try {
wait();
} catch (InterruptedException e) {
}
if (aborting) {
System.out.println("being aborted");
break running;
}
}
query = this.query;
this.query = null;
}
//must have been notified by sendPost
doSend(query);
}
System.out.println("I'm useless");
} /*
*surppose we need do send step by step, either step is
*time consuming
*the 1st step will costs 3 sec(without being canceled),
*and the 2nd step
*will never end untill being canceled.
*/
private void doSend(String query) {
int i = 0;
for (; !isCanceled && i < 3; i++) {
System.out.println(query + " stepI " + i);
try {
Thread.sleep(1 * 1000);
} catch (InterruptedException e) {
}
}
for (; !isCanceled; i++) {
System.out.println(query + " stepII " + i);
try {
Thread.sleep(1 * 1000);
} catch (InterruptedException e) {
}
}
} /*
*This is just for tidying up - the instance is useless
*after it has
*been called
*/
public void abort() {
aborting = true;
synchronized (this) {
notify(); // wake up our posting thread and kill it
}
} public synchronized void cancel() {
System.out.println("I'm being canceled, however, I'll be back soon");
isCanceled = true;
} public synchronized void resume() {
System.out.println("I'm back as I've told you");
isCanceled = false;
}
}
/**********************Poster ends here*****************//****************ThreadTest starts here**********/
public class ThreadTest {
private static Poster poster = new Poster(); public static void main(String[] args) {
poster.sendPost("test");
wait(2);
poster.cancel();
wait(5);
poster.resume();//although having been cancled, the thread is not "dead"
poster.sendPost("test2");
wait(5);
poster.cancel();//the point is: before aborting, u must cancel it first
poster.abort();//now the thread is useless
poster.sendPost("test3"); //this will never work
} public static void wait(int second) {
try {
Thread.sleep(second * 1000);
} catch (InterruptedException e) {
}
}
}/****************ThreadTest ends here**********/
/**********************************************/thank you for your testingBest Regards,
jigsaw
这一段程序应该没有问题,但还是没有能解决顶楼的问题啊。 你把isCanceled放到doSend中,而bbdolly没有你这么幸运啊。看来我还不能给你分,顶楼看着办吧。 :-)
Only on condition that doSend() will never end, do we have to call cancel() before abort(). Otherwise, if we can assure that doSend() is able to finish its work and give us a response, we have a choice to wait till it ends and then abort it. Thus, it's optional with us to call cancel() or not.to bbdolly:Only variables may be volatile ; declaring them so indicates
that such variables might be modified asynchronously, so the compiler takes special precautions. --copied from sybex
首先,肯定要有一个监视线程。如果,搂主连这个要求都无法满足,那就没戏了。
其次,设置一个volatile的flag。
再次,监视线程要保留该工作线程的一个reference(否则怎么监呀?)。具体的机制:
当监视线程觉得可以干掉这个工作线程的时候,造一个InterruptException,终止正在运行的工作线程,这样就可以避免以下的情况:
(以下是引用前面的话)
jigsaw(jigsaw):
问题正如helpall()说的那样,程序会在run()中一个函数上停住,而不会再运行到while(!cancel)处。这样不管是什么操作extern_func1(...)(这是搂主用来封装ftp函数的method),都会被迫终止。而去检查 stopRequested 这个flag了。然后,上面的兄弟都说了,我就不重复了。下面是我的例子:
public class KillMeSoftly extends Object implements Runnable { private volatile boolean stopRequested; private Thread runThread; public void run() { runThread = Thread.currentThread(); stopRequested = false; int count = 0; while ( !stopRequested ) { System.out.println("Running ... count=" + count); count++; try { Thread.sleep(300); } catch ( InterruptedException x ) { System.out.println("Caught a InteruptedException! It is time to check the stopRequested!");
Thread.currentThread().interrupt(); // reassert } } } public void stopRequest() { stopRequested = true; if ( runThread != null ) { runThread.interrupt(); } } public static void main(String[] args) { AlternateStop as = new AlternateStop(); Thread t = new Thread(as); t.start(); try { Thread.sleep(2000); } catch ( InterruptedException x ) { // ignore }
as.stopRequest(); } }在此郑重声明:此例子引用自
《Java Thread Programming》
Paul Hyde
Copyright © 1999 Sams Publishing
吐血推荐的好书呀!如果搂主的ftp函数牵涉到I/O操作,那么上面的方法还是不行的。你就再开一个贴子问我吧!