Java 并发编程之任务取消 (二)

计时中断

下面举个栗子,比如我们需要在指定时间内运行一个任意的Runnable的示例。它在调用线程中运行任务,并且安排了一个取消任务,在运行指定的时间间隔后中断它,这解决了从任务中抛出未检查异常的问题。因为该异常会被timedRun的调用者捕获。

import java.util.Collection;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class Init {
        private static final ScheduledExecutorService cancelExec = Executors.newScheduledThreadPool(1);

	public void timedrun(Runnable r, long timeout, TimeUnit unit)
			throws InterruptedException {

		final Thread taskThread = Thread.currentThread();

		cancelExec.schedule(new Runnable() {
			public void run() {
				taskThread.interrupt();
				System.out.println(taskThread.isInterrupted());
			}
		}, timeout, unit);
		r.run();
		System.out.println(taskThread.isInterrupted());
	}

	public static void main(String[] args) {
		Init init = new Init();
		Runnable run = new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				int i = 0;
				for (int j = 0; j < 100000000; j++) {
					i++;
					if (i % 10000000 == 0) {
						System.out.println(i);
					}
				}
			}
		};
		try {
			init.timedrun(run, 1000, TimeUnit.MILLISECONDS);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

运行结果:

true
10000000
20000000
30000000
40000000
50000000
60000000
70000000
80000000
90000000
100000000
true

在实际运用中断时,需要注意,调用inerrupt并不意味着立即停止对目标线程正在进行的工作,而只是传递了请求中断的消息。


所以实际上中断的作用是这样,一种特殊的消息叫做中断,比如说,我说接口方法,所以我们可以在子类中实现它。那么中断也是这样,它是一类消息,我们也要在类里边实现它自身的中断策略。它们会在一个合适的时刻中断自己。这些时刻又被称为取消点。比如wait sleep join等,将严格地处理这种请求,当它们收到中断请求或者在开始执行发现某个已被设置好的中断状态,将抛出一个异常。


这是一种非常简单的方法,但却破坏了以下规则:在中断线程之前,应该了解它的中断策略,由于 TimedRun可以从任意一个线程中调用,因此它无法知道这个调用线程的中断策略。

如果任务在超时之前完成,那么中断TImedRun所在线程的取消任务将在TimedRun返回到调用者之后启动,我们不知道在这种情况下将运行什么 代码,虽然我们可以使用schedule返回的ScheduledFuture来取消这个取消任务以避免这种风险。做法虽然可行,但却非常复杂。

如果任务不响应中断,那么timedRun会在任务结束时才返回,此时可能已经超过了指定的时限或者没有。如果某个限时运行的服务没有在指定的时间内返回,那么对调用者带来负面影响 。

栗子二:

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class Init {
	private static final ScheduledExecutorService cancelExec = Executors
			.newScheduledThreadPool(1);

	public void timedrun(final Runnable r, long timeout, TimeUnit unit)
			throws InterruptedException {
		class RethrowableTask implements Runnable {
			private volatile Throwable t;

			public void run() {
				try {
					r.run();
				} catch (Throwable t) {
					this.t = t;
				}
			}

			void rethrow() {
				if (t != null) {
					throw launderThrowable(e.getCause());
				}
			}
		}
		RethrowableTask task = new RethrowableTask();
		final Thread taskThread = new Thread(task);
		taskThread.start();

		cancelExec.schedule(new Runnable() {
			public void run() {
				taskThread.interrupt();
				System.out.println(taskThread.isInterrupted());
			}
		}, timeout, unit); 
		taskThread.join(unit.toMillis(timeout));
		task.rethrow();
		System.out.println(taskThread.isInterrupted());
	}
        public static RuntimeException launderThrowable(Throwable t) {

        if (t instanceof RuntimeException)
            return (RuntimeException) t;

        else if (t instanceof Error)
            throw (Error) t;

        else
            throw new IllegalStateException("Not unchecked", t);

    }
      public static void main(String[] args) {
		Init init = new Init();
		Runnable run = new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				int i = 0;
				for (int j = 0; j < 100000000; j++) {
					i++;
					if (i % 20000000 == 0) {
						System.out.println(i);
					}
				}
			}
		};
		try {
			init.timedrun(run, 150, TimeUnit.MILLISECONDS);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

结果如下:

20000000
40000000
false
true
60000000
80000000
100000000



执行线程在这个栗子中有了自己的执行策略,即使任务不响应中断,限时运行的方法仍能返回到它的调用者,在启动任务线程之后,timeRun将执行一个join方法,在join返回后,它将检查任务中是否有异常抛出,如果有的话则会在调用timedRun的线程中再次抛出该异常,由于 Throwable将在两个线程之间共享,因此该变量被声明为volatile类型。由于依赖一个限时的Join,所以它的不足就是 无法知道执行控制是因为线程正常退出而返回还是因为join超时而返回




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值