Skip to content

Commit 6d6f21a

Browse files
committed
finalize milestone5 edit and readme
1 parent 08cf243 commit 6d6f21a

File tree

2 files changed

+21
-96
lines changed

2 files changed

+21
-96
lines changed

README-M5.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Milestone 5 – Async support for **JSONObject**
2+
3+
## What is Added
4+
5+
| Item | Description |
6+
|------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
7+
| `Future<JSONObject> XMLUtils.toJSONObject(Reader reader, Consumer<JSONObject> after, Consumer<Exception> error)` | **Non-blocking** conversion. Parses the XML read from `reader` into a `JSONObject` on a background thread. When parsing finishes it invokes `after.accept(result)`; when failure it calls `error.accept(ex)` and throws the exception into the returned `Future`. |
8+
| `AsyncRunner` | Tiny task aggregator. Call `add(Future<JSONObject> task)` to collect jobs, then wait for them all (e.g. `forEach(Future::get)`). |
9+
| `ExecutorService` | The default thread pool (size = available CPU cores). If you prefer a custom pool you can swap it out before calling the API (e.g. add `XMLUtils.setExecutor(...)`). |
10+
11+
Input: XML of different sizes (where they will be parsed concurrently)
12+
13+
---
14+
15+
## Run the test class
16+
```bash
17+
mvn -Dtest=org.json.junit.milestone5.tests.JSONObjectAsyncTest test

src/main/java/org/json/XML.java

Lines changed: 4 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -2240,76 +2240,10 @@ private static final String indent(int indent) {
22402240
return sb.toString();
22412241
}
22422242

2243-
// public static class asyncRunner {
2244-
// private List<Future<JSONObject>> tasks;
2245-
// private boolean running;
2246-
//
2247-
// public asyncRunner() {
2248-
// this.tasks = new ArrayList<>();
2249-
// running = false;
2250-
// }
2251-
//
2252-
// public void add(Future<JSONObject> task) {
2253-
// this.tasks.add(task);
2254-
// }
2255-
//
2256-
//
2257-
// public void run() {
2258-
// running = true;
2259-
// while(running) {
2260-
// List<Future<JSONObject>> nextTasks = new ArrayList<>();
2261-
// for(Future<JSONObject> task: tasks) {
2262-
// if(!task.isDone()) {
2263-
// nextTasks.add(task);
2264-
// }
2265-
// }
2266-
// running = !nextTasks.isEmpty();
2267-
// tasks = nextTasks;
2268-
// }
2269-
// }
2270-
// }
2271-
//
2272-
// public static Future<JSONObject> toJSONObject(Reader reader, Consumer<JSONObject> after, Consumer<Exception> error){
2273-
//
2274-
// ExecutorService executor = Executors.newSingleThreadExecutor();
2275-
// futureTask task = new futureTask(reader, after, error);
2276-
// Future<JSONObject> future = executor.submit(task);
2277-
//
2278-
// return future;
2279-
// }
2280-
//
2281-
// private static class futureTask implements Callable<JSONObject> {
2282-
//
2283-
// Reader reader;
2284-
// Consumer<JSONObject> after;
2285-
// Consumer<Exception> error;
2286-
// public futureTask(Reader reader, Consumer<JSONObject> after, Consumer<Exception> error) {
2287-
// this.reader = reader;
2288-
// this.after = after;
2289-
// this.error = error;
2290-
// }
2291-
//
2292-
// @Override
2293-
// public JSONObject call() throws Exception {
2294-
// JSONObject jo = new JSONObject();
2295-
// try {
2296-
// XMLTokener x = new XMLTokener(reader);
2297-
// while (x.more()) {
2298-
// x.skipPast("<");
2299-
// if (x.more()) {
2300-
// parse(x, jo, null, XMLParserConfiguration.ORIGINAL, 0);
2301-
// }
2302-
// }
2303-
// after.accept(jo);
2304-
// } catch (Exception e) {
2305-
// error.accept(e);
2306-
// }
2307-
// return jo;
2308-
// }
2309-
// }
2310-
private static final ExecutorService executor = Executors.newFixedThreadPool(
2311-
Runtime.getRuntime().availableProcessors()
2312-
);
2243+
// milestone 5
2244+
private static final ExecutorService executor = Executors.newFixedThreadPool(
2245+
Runtime.getRuntime().availableProcessors()
2246+
);
23132247

23142248
public static Future<JSONObject> toJSONObject(
23152249
Reader reader,
@@ -2321,10 +2255,6 @@ public static Future<JSONObject> toJSONObject(
23212255
return task;
23222256
}
23232257

2324-
public static void shutdownExecutor() {
2325-
executor.shutdown();
2326-
}
2327-
23282258
private static class FutureTaskCallable implements Callable<JSONObject> {
23292259
private final Reader reader;
23302260
private final Consumer<JSONObject> after;
@@ -2370,27 +2300,5 @@ public void add(Future<JSONObject> task) {
23702300
this.tasks.add(task);
23712301
}
23722302

2373-
public void run() {
2374-
boolean running = true;
2375-
while (running) {
2376-
List<Future<JSONObject>> nextTasks = new ArrayList<>();
2377-
for (Future<JSONObject> task : tasks) {
2378-
if (!task.isDone()) {
2379-
nextTasks.add(task);
2380-
}
2381-
}
2382-
if (nextTasks.isEmpty()) {
2383-
running = false;
2384-
} else {
2385-
tasks = nextTasks;
2386-
try {
2387-
Thread.sleep(50);
2388-
} catch (InterruptedException ie) {
2389-
Thread.currentThread().interrupt();
2390-
running = false;
2391-
}
2392-
}
2393-
}
2394-
}
23952303
}
23962304
}

0 commit comments

Comments
 (0)