Topic: 小妹问个线程问题,各位ggjj帮忙

  Print this page

1.小妹问个线程问题,各位ggjj帮忙 Copy to clipboard
Posted by: xinzi
Posted on: 2005-05-19 11:28

我有一个Job接口对象,Job执行时是创建一个实现Runnable接口的对象X,X会调用Job接口的execute()方法。但是Job这个方法可能长时间执行不完(由于某些异常)我想规定一个最大时间,如果超过这个时间则Job的execute()方法返回,并执行完X的run()方法,这样一个异常Job不会占用一个Thread过久。这个怎么实现? 小妹我查了很多资料,但无奈脑袋不开窍,没找到好的办法,请ggjj帮忙啊!

2.Re:小妹问个线程问题,各位ggjj帮忙 [Re: xinzi] Copy to clipboard
Posted by: jigsaw
Posted on: 2005-05-19 14:27

check javadoc of wait(long) and interrupt() of java.lang.Thread

3.Re:小妹问个线程问题,各位ggjj帮忙 [Re: jigsaw] Copy to clipboard
Posted by: xinzi
Posted on: 2005-05-19 16:31

Thanks for your advise Smile
jigsaw wrote:
check javadoc of wait(long) and interrupt() of java.lang.Thread

My code sketch is here, could you give me more suggestion pls. thanks a lot Smile


public interface Job {
public boolean execute();
}
class JobRunner implements Runnable {
Job job;
public JobRunner(Job job) {
this.job = job;
}

public void run() {
job.execute(); //注意:job的execute()方法执行时间是不可定的,
//可能很长,甚至没有返回,
//如何在JobRunner级别来控制job.execute()方法运行时间。
//请各位ggjj帮忙看看.
}
}


4.Re:小妹问个线程问题,各位ggjj帮忙 [Re: xinzi] Copy to clipboard
Posted by: danzel
Posted on: 2005-05-19 16:42

如果只是异常的话是不是可以考虑try{}catch???

5.Re:小妹问个线程问题,各位ggjj帮忙 [Re: xinzi] Copy to clipboard
Posted by: think
Posted on: 2005-05-19 16:55

可以考虑后台运行一个定时器,timeout后终止正在运行的线程。
不过应该有许多的资源清除工作需要做。

public void run() {
final long timeout = XXX;

final Thread currThread = Thread.currentThread();
final Timer timer = new Timer();

TimerTask task = new TimerTask() {
public void run() {
if(currThread.isAlive())
currThread.interrupt();

timer.cancel();
}
};
timer.schedule(task, timeout, 1);

job.execute();
}

6.Re:小妹问个线程问题,各位ggjj帮忙 [Re: xinzi] Copy to clipboard
Posted by: qizi
Posted on: 2005-05-19 16:59

Job是一个interface
必须通过例如

public Manager implements Job{
public Manager(){
//初始化
}
public boolean execute(){
//你需要执行的代码;
}
}

class JobRunner implements Runnable {
private Job job;
private Thread thread;
public JobRunner(Job job) {
this.job = job;
thread = new Thread(this);//自动启动线程
thread.start();
}

public void run() {
job.execute();
}
public static void main(String [] args){
Job onejob = new Manager();
JobRuner = new JobRunner(onejob);
}
}

7.Re:小妹问个线程问题,各位ggjj帮忙 [Re: xinzi] Copy to clipboard
Posted by: jigsaw
Posted on: 2005-05-20 10:29


final long duration = 3000L; // wait for 3 sec
final Thread work = new Thread(new JobRunner(new Manager()));

Thread timer = new Thread(new Runnable() {
public void run() {
try {
wait(duration);
} catch (InterrupttedException e) {
// ignore?
}
work.interrupt();
// make sure the Manager class does have fail-save on InterupttedException

}
});

work.start();
timer.start();

Note that Manager will NOT be interrupted if it's being blocked in IO opration.

8.Re:小妹问个线程问题,各位ggjj帮忙 [Re: xinzi] Copy to clipboard
Posted by: henryevol
Posted on: 2005-06-04 17:50

这个问题
jdk5.0
去 java.util.concurrent.*
里看看


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923