Visitar URL original
tests sync · zacscoding/java_example@2eeefde · GitHub
Skip to content

Commit 2eeefde

Browse files
committed
tests sync
1 parent f3d744c commit 2eeefde

3 files changed

Lines changed: 157 additions & 0 deletions

File tree

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package sync;
2+
3+
/**
4+
*
5+
* @GitHub : https://github.com/zacscoding
6+
*/
7+
public enum SyncEvent {
8+
NEW_PEER, TIME_LIMIT;
9+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package sync;
2+
3+
import java.util.Random;
4+
import java.util.concurrent.TimeUnit;
5+
6+
import org.junit.Test;
7+
8+
/**
9+
*
10+
* @GitHub : https://github.com/zacscoding
11+
*/
12+
public class SyncTest {
13+
14+
@Test
15+
public void runTests() throws Exception {
16+
final Sync sync = new Sync();
17+
sync.start();
18+
19+
Thread t = new Thread(() -> {
20+
while (!Thread.currentThread().isInterrupted()) {
21+
try {
22+
long sleep = new Random().nextInt(10);
23+
TimeUnit.SECONDS.sleep(sleep);
24+
sync.newPeer();
25+
} catch (InterruptedException e) {
26+
return;
27+
}
28+
}
29+
});
30+
t.setDaemon(true);
31+
t.start();
32+
33+
TimeUnit.MINUTES.sleep(1L);
34+
}
35+
}

0 commit comments

Comments
 (0)