|
| 1 | +package sync; |
| 2 | + |
| 3 | +import java.time.LocalDateTime; |
| 4 | +import java.time.format.DateTimeFormatter; |
| 5 | +import java.util.Random; |
| 6 | +import java.util.concurrent.BlockingQueue; |
| 7 | +import java.util.concurrent.LinkedBlockingQueue; |
| 8 | +import java.util.concurrent.TimeUnit; |
| 9 | + |
| 10 | +/** |
| 11 | + * |
| 12 | + * @GitHub : https://github.com/zacscoding |
| 13 | + */ |
| 14 | +public class Sync { |
| 15 | + |
| 16 | + SyncTask task; |
| 17 | + |
| 18 | + public void start() { |
| 19 | + if (task != null) { |
| 20 | + System.out.println("Already started"); |
| 21 | + return; |
| 22 | + } |
| 23 | + |
| 24 | + task = new SyncTask(); |
| 25 | + task.start(); |
| 26 | + } |
| 27 | + |
| 28 | + public void stop() { |
| 29 | + if (task == null) { |
| 30 | + System.out.println("Already stopped"); |
| 31 | + return; |
| 32 | + } |
| 33 | + |
| 34 | + task.interrupt(); |
| 35 | + task = null; |
| 36 | + } |
| 37 | + |
| 38 | + public void newPeer() { |
| 39 | + if (task != null) { |
| 40 | + task.newPeer(); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + public static class SyncTask extends Thread { |
| 45 | + |
| 46 | + private BlockingQueue<SyncEvent> eventQueue; |
| 47 | + private boolean isSyncing; |
| 48 | + |
| 49 | + public SyncTask() { |
| 50 | + super.setDaemon(true); |
| 51 | + eventQueue = new LinkedBlockingQueue<>(); |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public void run() { |
| 56 | + while (!Thread.currentThread().isInterrupted()) { |
| 57 | + try { |
| 58 | + SyncEvent event = eventQueue.poll(3L, TimeUnit.SECONDS); |
| 59 | + if (event == null) { |
| 60 | + event = SyncEvent.TIME_LIMIT; |
| 61 | + } |
| 62 | + |
| 63 | + switch (event) { |
| 64 | + case NEW_PEER: |
| 65 | + System.out.println(getPrefix() + " new peer event occur.."); |
| 66 | + synchronize(); |
| 67 | + break; |
| 68 | + case TIME_LIMIT: |
| 69 | + System.out.println(getPrefix() + " time limit occur.."); |
| 70 | + synchronize(); |
| 71 | + break; |
| 72 | + } |
| 73 | + } catch (InterruptedException e) { |
| 74 | + e.printStackTrace(System.err); |
| 75 | + break; |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + public void newPeer() { |
| 81 | + eventQueue.offer(SyncEvent.NEW_PEER); |
| 82 | + } |
| 83 | + |
| 84 | + private void synchronize() { |
| 85 | + synchronized (this) { |
| 86 | + if (isSyncing) { |
| 87 | + return; |
| 88 | + } |
| 89 | + isSyncing = true; |
| 90 | + Thread t = new Thread(() -> { |
| 91 | + try { |
| 92 | + long sleep = new Random().nextInt(10); |
| 93 | + System.out.println(getPrefix() + " start to synchronize with sleep " + sleep); |
| 94 | + TimeUnit.SECONDS.sleep(sleep); |
| 95 | + System.out.println(getPrefix() + " complete to synchronize"); |
| 96 | + |
| 97 | + synchronized (SyncTask.this) { |
| 98 | + isSyncing = false; |
| 99 | + } |
| 100 | + } catch (InterruptedException e) { |
| 101 | + e.printStackTrace(); |
| 102 | + } |
| 103 | + }); |
| 104 | + t.setDaemon(true); |
| 105 | + t.start(); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + private String getPrefix() { |
| 110 | + return "[" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("HH:mm:ss")) + "]"; |
| 111 | + } |
| 112 | + } |
| 113 | +} |
0 commit comments