protected synchronized int doBarrier(boolean timed, long msecs)
throws InterruptedException, TimeoutException, BrokenBarrierException {
int index = --count_; if (broken_) {
throw new BrokenBarrierException(index);
}
else if (Thread.interrupted()) {
broken_ = true;
notifyAll();
throw new InterruptedException();
}
else if (index == 0) { // tripped
count_ = parties_;
++resets_;
notifyAll();
try {
if (barrierCommand_ != null)
barrierCommand_.run();
return 0;
}
catch (RuntimeException ex) {
broken_ = true;
return 0;
}
}
else if (timed && msecs <= 0) {
broken_ = true;
notifyAll();
throw new TimeoutException(msecs);
}
else { // wait until next reset
int r = resets_;
long startTime = (timed)? System.currentTimeMillis() : 0;
long waitTime = msecs;
for (;;) {
try {
wait(waitTime);
}
catch (InterruptedException ex) {
// Only claim that broken if interrupted before reset
if (resets_ == r) {
broken_ = true;
notifyAll();
throw ex;
}
else {
Thread.currentThread().interrupt(); // propagate
}
} if (broken_)
throw new BrokenBarrierException(index); else if (r != resets_)
return index; else if (timed) {
waitTime = msecs - (System.currentTimeMillis() - startTime);
if (waitTime <= 0) {
broken_ = true;
notifyAll();
throw new TimeoutException(msecs);
}
}
}
}
}
throws InterruptedException, TimeoutException, BrokenBarrierException {
int index = --count_; if (broken_) {
throw new BrokenBarrierException(index);
}
else if (Thread.interrupted()) {
broken_ = true;
notifyAll();
throw new InterruptedException();
}
else if (index == 0) { // tripped
count_ = parties_;
++resets_;
notifyAll();
try {
if (barrierCommand_ != null)
barrierCommand_.run();
return 0;
}
catch (RuntimeException ex) {
broken_ = true;
return 0;
}
}
else if (timed && msecs <= 0) {
broken_ = true;
notifyAll();
throw new TimeoutException(msecs);
}
else { // wait until next reset
int r = resets_;
long startTime = (timed)? System.currentTimeMillis() : 0;
long waitTime = msecs;
for (;;) {
try {
wait(waitTime);
}
catch (InterruptedException ex) {
// Only claim that broken if interrupted before reset
if (resets_ == r) {
broken_ = true;
notifyAll();
throw ex;
}
else {
Thread.currentThread().interrupt(); // propagate
}
} if (broken_)
throw new BrokenBarrierException(index); else if (r != resets_)
return index; else if (timed) {
waitTime = msecs - (System.currentTimeMillis() - startTime);
if (waitTime <= 0) {
broken_ = true;
notifyAll();
throw new TimeoutException(msecs);
}
}
}
}
}
treeroot(天才--天天被人踩,人才--人人都想踩) :
--------------------------------------------------
带有不确实因素的操作有的就要加上时间限制,因为你不可能无限地尝试下去,比如数据库连接。zealVampire(蚊子+鹤舞白沙) ,你举出的例子看的不大明白。
doBarrier()它是通过循环的wait()来消耗时间的,而我要的功能是做一个实际的操作,比如读取文件内容,是直线操作,不能通过循环操作来检查时间。
for (;;) {
try {
wait(waitTime);
}
后面有想法的朋友请继续,说个思路也行,不过补充一点,在一个方法中或单一类中也许不能实现这个功能,大家思维不要被束缚了。
Socket连接也可以设置操时呀
除了阻塞的地方要用,其他地方应该没什么用吧
所以我编了一个HardCopy,当读不出来的时候就填入上一次读取的内容
但是等待系统抛出IOException的时间太长了,而且出现多次读不出来的时候就更慢了,所以我想对每次的读取操作加一个timeout限制。本来这个程序是没多大意义的,但是我想了许多方法,如用观察者模式,用Timer等,好象都很难实现,用多线程的方法也想过了,没想通,觉得这个事情蛮有意思的,所以想拿出来大家讨论一下。
public YourClass extends Thread{
private int i=0;
public YourClass(){this.start();}
public void run(){
try{
a();
}catch(Exception any){
//to do your action.
}
} public void a(int timeout) {
try{
Thread.sleep(1000)
}catch(Exception e){}
i++;
if(15==i) throws TimeOutException;
// ...
}
}
但是一旦进入IO操作,线程就阻塞了,在IO操作返回或者抛出异常以前,根本无法执行到if判断语句,也就根本无法抛出TimeOutException用多线程的时候,达到某一时间,虽然IO仍然阻塞,但是主线程可以不用等了,直接进行下一步操作。
如果waiting的时间过长就抛出异常
class MyTask extends TimerTask {
public void run() {
throw new TimeOutException();//时间到就抛出异常,结束程序
}
}
public void a(int timeout) throws TimeOutException(){
Timer t=new Timer();
t.schedule(new MyTask(),timout);
....//do something;
t.cancel();//执行完操作但没有超时,就取消Timer
}
用一个计时器,找找JDK里有没有这样的类,如果没有这种类就单开一个计时的线程。
class MyTimer implements Runnable{
private long startTime = 0;
private long endTime = 0;
//bStart = true,表示超时,false表示继续等待
private boolean bStart = false;
private long waitTime = 5*1000;public MyTimer(){}
public boolean getBStart(){
return this.bStart;
}
public void setBStart(){
while(bStart == true){
endTime = System.currentTimeMillis();
if(endTime-startTime>=waitTime)
this.bStart = false;
else
this.bStart = true;
}
}
/* (非 Javadoc)
* @see java.lang.Runnable#run()
*/public void run() {
this.setBStart();
Thread.interrupted();
}
}
用线程管理timeout啊!
在你想用timeout的方法内这样使用啊
MyTimer timer = new MyTimer();
Thread thread = new Thread(timer);
thread.setDaemon(true);
thread.start();
主线程查询标志位,如果超时标志位仍未被改变,就抛出异常主线程:
import java.util.*;
public class MyListener extends Thread implements Observer{
//保存标志位
private boolean RunFlag;
private int timeout;
public MyListener(int timeout){
RunFlag=false;
this.timeout=timeout;
}
public void update(Observable o,Object obj){
RunFlag=true;
}
public void run(){
try{
sleep(timeout);
if(!RunFlag){
throw new Exception("任务超时!");
}
else{
System.out.print("OK,任务已经结束!");
}
}catch(Exception e){
e.printStackTrace();
}
}
}任务线程
import java.util.*;
public class MyTask extends Observable implements Runnable{
public MyTask() {
super();
}
public void run(){
//这里要执行的容易超时的任务
for(int i=0;i<5000;i++){
System.out.println("I am working");
}
setChanged();
}
}
运行方法:
public class console {
public static void main(String[] args) {
MyListener listener=new MyListener(1);
MyTask task=new MyTask();
task.addObserver(listener);
try{
listener.start();
task.run();
task.notifyObservers();
}catch(Exception e){
e.printStackTrace();
}
}
}
你所说得阻塞是说发生了异常还是方法中得执行因为某个操作慢而阻塞其它语句得执行呢?如果是发生了异常,那捕捉异常从而程序就停止执行了,那就没有超时这一说了!如果是因为方法中某个操作执行很慢,从而阻塞了其它语句得执行,那没问题,因为Timer是一个单独得线程和其它语句独立得,所以超时时间一到就会执行TimerTask。个人觉得我得方法还是可以得!
大家可以继续讨论
public void run() {
throw new TimeOutException();//时间到就抛出异常,结束程序
}因为run()方法是不能抛出异常的