- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
Latest commit
70 lines (50 loc) · 2.31 KB
/
Copy pathMain.java
File metadata and controls
70 lines (50 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
importcom.fasterxml.jackson.annotation.JsonProperty;
importcom.fasterxml.jackson.databind.ObjectMapper;
importcom.google.gson.Gson;
importcom.sun.org.apache.xpath.internal.operations.Bool;
importjava.io.BufferedReader;
importjava.io.IOException;
importjava.io.InputStreamReader;
importjava.net.HttpURLConnection;
importjava.net.MalformedURLException;
importjava.net.ProtocolException;
importjava.net.URL;
importjava.sql.Time;
importjava.util.concurrent.ExecutorService;
importjava.util.concurrent.Executors;
importjava.util.concurrent.TimeUnit;
publicclassMain {
publicstaticvoidmain(String[] args) {
/*
* This example project does threading with API calls to improve performance. (Rather than running them synchronously)
* It artificially sleeps the 2nd API by 3 seconds to show how this example waits until all threads are complete to continue working
*
* There are many ways to handle threading this is one way of doing so
*/
threadedAPIcall1 = newthreadedAPI(false);
threadedAPIcall2 = newthreadedAPI(true);
threadedAPIcall3 = newthreadedAPI(false);
ExecutorServicees = Executors.newCachedThreadPool();
System.out.println("Threads starting");
//add each runnable class to the executor
es.execute(call1);
es.execute(call2);
es.execute(call3);
es.shutdown(); //stops executor for accepting anymore
try {
//this one line puts a stop in and waits for all threads in the executor service to finish
//Uncommenting this line will cause the app to crash because the code will try and access values before the API finishes returning them
booleanfinished = es.awaitTermination(30, TimeUnit.SECONDS);
System.out.println("Threads finished");
} catch (InterruptedExceptione) {
e.printStackTrace();
}
//just showing that data is accessible
longtime1 = call1.getResponse().getCurrentFileTime();
longtime2 = call2.getResponse().getCurrentFileTime();
longtime3 = call2.getResponse().getCurrentFileTime();
System.out.println("Call 1 data: " + time1);
System.out.println("Call 2 data: " + time2);
System.out.println("Call 3 data: " + time3);
}
}