mirror of
https://github.com/Eaglercraft-TeaVM-Fork/eagler-teavm.git
synced 2024-12-22 00:04:10 -08:00
Improve Async demo
This commit is contained in:
parent
3a78db74c1
commit
4f508954d0
|
@ -64,7 +64,7 @@
|
|||
<targetDirectory>${project.build.directory}/generated/js/teavm</targetDirectory>
|
||||
<mainClass>org.teavm.samples.async.AsyncProgram</mainClass>
|
||||
<runtime>SEPARATE</runtime>
|
||||
<minifying>false</minifying>
|
||||
<minifying>true</minifying>
|
||||
<debugInformationGenerated>true</debugInformationGenerated>
|
||||
<sourceMapsGenerated>true</sourceMapsGenerated>
|
||||
<sourceFilesCopied>true</sourceFilesCopied>
|
||||
|
|
File diff suppressed because one or more lines are too long
|
@ -20,8 +20,177 @@
|
|||
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
|
||||
<script type="text/javascript" charset="utf-8" src="teavm/runtime.js"></script>
|
||||
<script type="text/javascript" charset="utf-8" src="teavm/classes.js"></script>
|
||||
<script type="text/javascript" charset="utf-8" src="teavm/stdout.js"></script>
|
||||
<script type="text/javascript" charset="utf-8" src="highlight.pack.js"></script>
|
||||
<link rel="stylesheet" type="text/css" href="style.css"></link>
|
||||
<link rel="stylesheet" type="text/css" href="syntax.css"></link>
|
||||
<script type="text/javascript">
|
||||
function runAll() {
|
||||
hljs.highlightBlock(document.getElementById("source-code"));
|
||||
main(["foo", "bar"]);
|
||||
}
|
||||
</script>
|
||||
</head>
|
||||
<body onload="main()">
|
||||
<p>Please, open developer's console to view program's output</p>
|
||||
<body onload="runAll()">
|
||||
<div id="description">This application shows how TeaVM can handle multiple threads and synchronization primitives
|
||||
(see <a href="https://github.com/konsoletyper/teavm/tree/master/teavm-samples/teavm-samples-async">source code on GitHub</a>).</div>
|
||||
|
||||
<div id="blocks">
|
||||
<div class="block" id="stdout-wrapper">
|
||||
<div class="block-title">stdout</div>
|
||||
<div class="block-content" id="stdout"></div>
|
||||
</div>
|
||||
|
||||
<div class="block" id="source-wrapper">
|
||||
<div class="block-title">Source code</div>
|
||||
<pre class="block-content" id="source-code">
|
||||
public final class AsyncProgram {
|
||||
private static long start = System.currentTimeMillis();
|
||||
|
||||
private AsyncProgram() {
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws InterruptedException {
|
||||
report(Arrays.toString(args));
|
||||
findPrimes();
|
||||
withoutAsync();
|
||||
report("");
|
||||
withAsync();
|
||||
|
||||
report("");
|
||||
final Object lock = new Object();
|
||||
Thread t = new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
doRun(lock);
|
||||
} catch (InterruptedException ex) {
|
||||
report("Exception caught: " + ex.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
}, "Test Thread");
|
||||
t.start();
|
||||
|
||||
Thread t2 = new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
doRun(lock);
|
||||
} catch (InterruptedException ex) {
|
||||
report("Exception caught: " + ex.getMessage());
|
||||
}
|
||||
}
|
||||
}, "Test Thread 2");
|
||||
t2.start();
|
||||
|
||||
report("Should be main");
|
||||
report("Now trying wait...");
|
||||
|
||||
synchronized (lock) {
|
||||
report("Lock acquired");
|
||||
lock.wait(20000);
|
||||
}
|
||||
report("Finished main thread");
|
||||
}
|
||||
|
||||
private static void findPrimes() {
|
||||
report("Finding primes");
|
||||
boolean[] prime = new boolean[1000];
|
||||
prime[2] = true;
|
||||
prime[3] = true;
|
||||
nextPrime: for (int i = 5; i < prime.length; i += 2) {
|
||||
int maxPrime = (int)Math.sqrt(i);
|
||||
for (int j = 3; j <= maxPrime; j += 2) {
|
||||
Thread.yield();
|
||||
if (prime[j] && i % j == 0) {
|
||||
continue nextPrime;
|
||||
}
|
||||
}
|
||||
prime[i] = true;
|
||||
}
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (int i = 0; i < 1000; ++i) {
|
||||
if (prime[i]) {
|
||||
sb.append(i).append(' ');
|
||||
}
|
||||
}
|
||||
report(sb.toString());
|
||||
}
|
||||
|
||||
private static void report(String message) {
|
||||
long current = System.currentTimeMillis() - start;
|
||||
System.out.println("[" + Thread.currentThread().getName() + "]/" + current + ": " + message);
|
||||
}
|
||||
|
||||
private static void doRun(Object lock) throws InterruptedException {
|
||||
report("Executing timer task");
|
||||
Thread.sleep(2000);
|
||||
report("Calling lock.notify()");
|
||||
synchronized (lock) {
|
||||
lock.notify();
|
||||
}
|
||||
report("Finished calling lock.notify()");
|
||||
report("Waiting 5 seconds");
|
||||
Thread.sleep(5000);
|
||||
report("Finished another 5 second sleep");
|
||||
|
||||
synchronized (lock) {
|
||||
report("Sleep inside locked section");
|
||||
Thread.sleep(2000);
|
||||
report("Finished locked section");
|
||||
}
|
||||
}
|
||||
|
||||
private static void withoutAsync() {
|
||||
report("Start sync");
|
||||
for (int i = 0; i < 20; ++i) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (int j = 0; j <= i; ++j) {
|
||||
sb.append(j);
|
||||
sb.append(' ');
|
||||
}
|
||||
report(sb.toString());
|
||||
}
|
||||
report("Complete sync");
|
||||
}
|
||||
|
||||
private static void withAsync() throws InterruptedException {
|
||||
report("Start async");
|
||||
for (int i = 0; i < 20; ++i) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (int j = 0; j <= i; ++j) {
|
||||
sb.append(j);
|
||||
sb.append(' ');
|
||||
}
|
||||
report(sb.toString());
|
||||
if (i % 3 == 0) {
|
||||
report("Suspend for a second");
|
||||
Thread.sleep(1000);
|
||||
}
|
||||
}
|
||||
report("2nd Thread.sleep in same method");
|
||||
Thread.sleep(1000);
|
||||
|
||||
report("Throwing exception");
|
||||
try {
|
||||
throwException();
|
||||
} catch (IllegalStateException e) {
|
||||
report("Exception caught");
|
||||
}
|
||||
report("Complete async");
|
||||
}
|
||||
|
||||
private static void throwException() {
|
||||
Thread.yield();
|
||||
report("Thread.yield called");
|
||||
throw new IllegalStateException();
|
||||
}
|
||||
}
|
||||
</pre>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</body>
|
||||
</html>
|
62
teavm-samples/teavm-samples-async/src/main/webapp/style.css
Normal file
62
teavm-samples/teavm-samples-async/src/main/webapp/style.css
Normal file
|
@ -0,0 +1,62 @@
|
|||
html, body {
|
||||
position: absolute;
|
||||
left: 0;
|
||||
top: 0;
|
||||
right: 0;
|
||||
bottom: 0;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
}
|
||||
|
||||
.block {
|
||||
position: absolute;
|
||||
left: 0px;
|
||||
right: 0px;
|
||||
}
|
||||
.block-title {
|
||||
position: absolute;
|
||||
top: 10px;
|
||||
height: 20px;
|
||||
left: 10px;
|
||||
right: 10px;
|
||||
font-weight: bold;
|
||||
}
|
||||
.block-content {
|
||||
position: absolute;
|
||||
top: 30px;
|
||||
bottom: 5px;
|
||||
left: 10px;
|
||||
right: 10px;
|
||||
background-color: rgb(240,240,240);
|
||||
border-width: 1px;
|
||||
border-style: solid;
|
||||
border-color: rgb(210,210,210);
|
||||
overflow: auto;
|
||||
padding: 3px;
|
||||
margin: 0;
|
||||
border-radius: 3px;
|
||||
}
|
||||
|
||||
#description {
|
||||
position: absolute;
|
||||
top: 10px;
|
||||
height: 40px;
|
||||
left: 10px;
|
||||
right: 10px;
|
||||
}
|
||||
#blocks {
|
||||
position: absolute;
|
||||
top: 50px;
|
||||
bottom: 0;
|
||||
left: 0;
|
||||
right: 0;
|
||||
}
|
||||
#stdout-wrapper {
|
||||
top: 0;
|
||||
height: 50%;
|
||||
}
|
||||
#source-wrapper {
|
||||
top: 50%;
|
||||
bottom: 0;
|
||||
}
|
||||
|
12
teavm-samples/teavm-samples-async/src/main/webapp/syntax.css
Normal file
12
teavm-samples/teavm-samples-async/src/main/webapp/syntax.css
Normal file
|
@ -0,0 +1,12 @@
|
|||
.hljs-keyword {
|
||||
font-weight: bold;
|
||||
}
|
||||
.hljs-class .hljs-title {
|
||||
color: #9BB01D;
|
||||
}
|
||||
.hljs-annotation {
|
||||
color: #FFA500;
|
||||
}
|
||||
.hljs-string, .hljs-number {
|
||||
color: red;
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
function $rt_putStdout(ch) {
|
||||
if (ch == 0xA) {
|
||||
var lineElem = document.createElement("div");
|
||||
var stdoutElem = document.getElementById("stdout");
|
||||
lineElem.appendChild(document.createTextNode($rt_stdoutBuffer));
|
||||
stdoutElem.appendChild(lineElem);
|
||||
$rt_stdoutBuffer = "";
|
||||
stdoutElem.scrollTop = stdoutElem.scrollHeight;
|
||||
} else {
|
||||
$rt_stdoutBuffer += String.fromCharCode(ch);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user