|
| 1 | +package com.duwei.multythread.interrupt; |
| 2 | + |
| 3 | +import javafx.concurrent.Task; |
| 4 | + |
| 5 | +import java.util.concurrent.BlockingQueue; |
| 6 | +import java.util.concurrent.LinkedBlockingQueue; |
| 7 | +import java.util.concurrent.TimeUnit; |
| 8 | + |
| 9 | +/** |
| 10 | + * 线程中断相关Demo |
| 11 | + * <p> |
| 12 | + * https://www.ibm.com/developerworks/cn/java/j-jtp05236.html |
| 13 | + */ |
| 14 | +public class InterruptDemo { |
| 15 | + |
| 16 | + |
| 17 | + /** |
| 18 | + * 001:将中断异常向上抛出,外部调用者处理,告诉调用者这是一个堵塞方法 |
| 19 | + */ |
| 20 | + public static class TaskQueue { |
| 21 | + private static final int MAX_TASKS = 1000; |
| 22 | + |
| 23 | + private BlockingQueue<Runnable> queue |
| 24 | + = new LinkedBlockingQueue<Runnable>(MAX_TASKS); |
| 25 | + |
| 26 | + public void putTask(Runnable r) throws InterruptedException { |
| 27 | + queue.put(r); |
| 28 | + } |
| 29 | + |
| 30 | + public Runnable getTask() throws InterruptedException { |
| 31 | + return queue.take(); |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * 002:在抛出异常之前进行清理操作,模拟匹配玩家游戏 |
| 37 | + */ |
| 38 | + public static class PlayerMatcher { |
| 39 | + private PlayerSource players; |
| 40 | + |
| 41 | + public PlayerMatcher(PlayerSource players) { |
| 42 | + this.players = players; |
| 43 | + } |
| 44 | + |
| 45 | + public void matchPlayers() throws InterruptedException { |
| 46 | + Player playerOne = null, playerTwo = null; |
| 47 | + try { |
| 48 | + while (true) { |
| 49 | + // Wait for two players to arrive and start a new game |
| 50 | + playerOne = players.waitForPlayer(); // could throw IE |
| 51 | + playerTwo = players.waitForPlayer(); // could throw IE |
| 52 | + startNewGame(playerOne, playerTwo); |
| 53 | + } |
| 54 | + } catch (InterruptedException e) { |
| 55 | + // If we got one player and were interrupted, put that player back |
| 56 | + if (playerOne != null) |
| 57 | + players.addFirst(playerOne); |
| 58 | + // Then propagate the exception |
| 59 | + throw e; |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + private void startNewGame(Player playerOne, Player playerTwo) { |
| 64 | + } |
| 65 | + |
| 66 | + private static class PlayerSource { |
| 67 | + public Player waitForPlayer() throws InterruptedException { |
| 68 | + return null; |
| 69 | + } |
| 70 | + |
| 71 | + public void addFirst(Player playerOne) { |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + private static class Player { |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * 003:不可抛出的时候要捕获再中断 |
| 81 | + */ |
| 82 | + public static class TaskRunner implements Runnable { |
| 83 | + private BlockingQueue<Task> queue; |
| 84 | + |
| 85 | + public TaskRunner(BlockingQueue<Task> queue) { |
| 86 | + this.queue = queue; |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public void run() { |
| 91 | + try { |
| 92 | + while (true) { |
| 93 | + Task task = queue.take();//这里不能抛,只能try-catch |
| 94 | + task.execute(); |
| 95 | + } |
| 96 | + } catch (InterruptedException e) { |
| 97 | + // Restore the interrupted status |
| 98 | + Thread.currentThread().interrupt(); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + private static class Task { |
| 103 | + public void execute(){} |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | +} |
0 commit comments