Improve async code example. Don't generate JSO aliases for static

methods
This commit is contained in:
Alexey Andreev 2015-10-20 23:05:55 +03:00
parent 9e5309a986
commit 891ec3e6fd
3 changed files with 39 additions and 52 deletions

View File

@ -29,6 +29,7 @@ import org.teavm.model.AnnotationValue;
import org.teavm.model.CallLocation; import org.teavm.model.CallLocation;
import org.teavm.model.ClassReader; import org.teavm.model.ClassReader;
import org.teavm.model.ClassReaderSource; import org.teavm.model.ClassReaderSource;
import org.teavm.model.ElementModifier;
import org.teavm.model.FieldReader; import org.teavm.model.FieldReader;
import org.teavm.model.FieldReference; import org.teavm.model.FieldReference;
import org.teavm.model.MethodDescriptor; import org.teavm.model.MethodDescriptor;
@ -145,6 +146,9 @@ class JSDependencyListener extends AbstractDependencyListener {
if (addInterface(exposedCls, iface)) { if (addInterface(exposedCls, iface)) {
added = true; added = true;
for (MethodReader method : iface.getMethods()) { for (MethodReader method : iface.getMethods()) {
if (method.hasModifier(ElementModifier.STATIC)) {
continue;
}
if (!exposedCls.inheritedMethods.containsKey(method.getDescriptor())) { if (!exposedCls.inheritedMethods.containsKey(method.getDescriptor())) {
String name = method.getName(); String name = method.getName();
AnnotationReader methodAnnot = method.getAnnotations().get(JSMethod.class.getName()); AnnotationReader methodAnnot = method.getAnnotations().get(JSMethod.class.getName());

View File

@ -36,30 +36,21 @@ public final class AsyncProgram {
report(""); report("");
final Object lock = new Object(); final Object lock = new Object();
Thread t = new Thread(new Runnable() { new Thread(() -> {
@Override
public void run() {
try { try {
doRun(lock); doRun(lock);
} catch (InterruptedException ex) { } catch (InterruptedException ex) {
report("Exception caught: " + ex.getMessage()); report("Exception caught: " + ex.getMessage());
} }
} }, "Test Thread").start();
}, "Test Thread"); new Thread(() -> {
t.start();
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
try { try {
doRun(lock); doRun(lock);
} catch (InterruptedException ex) { } catch (InterruptedException ex) {
report("Exception caught: " + ex.getMessage()); report("Exception caught: " + ex.getMessage());
} }
} }, "Test Thread 2").start();
}, "Test Thread 2");
t2.start();
report("Should be main"); report("Should be main");
report("Now trying wait..."); report("Now trying wait...");

View File

@ -33,7 +33,7 @@
</head> </head>
<body onload="runAll()"> <body onload="runAll()">
<div id="description">This application shows how TeaVM can handle multiple threads and synchronization primitives <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> (see <a href="https://github.com/konsoletyper/teavm/tree/master/samples/async">source code on GitHub</a>).</div>
<div id="blocks"> <div id="blocks">
<div class="block" id="stdout-wrapper"> <div class="block" id="stdout-wrapper">
@ -59,30 +59,21 @@ public final class AsyncProgram {
report(""); report("");
final Object lock = new Object(); final Object lock = new Object();
Thread t = new Thread(new Runnable() { new Thread(() -> {
@Override
public void run() {
try { try {
doRun(lock); doRun(lock);
} catch (InterruptedException ex) { } catch (InterruptedException ex) {
report("Exception caught: " + ex.getMessage()); report("Exception caught: " + ex.getMessage());
} }
} }, "Test Thread").start();
}, "Test Thread"); new Thread(() -> {
t.start();
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
try { try {
doRun(lock); doRun(lock);
} catch (InterruptedException ex) { } catch (InterruptedException ex) {
report("Exception caught: " + ex.getMessage()); report("Exception caught: " + ex.getMessage());
} }
} }, "Test Thread 2").start();
}, "Test Thread 2");
t2.start();
report("Should be main"); report("Should be main");
report("Now trying wait..."); report("Now trying wait...");
@ -99,18 +90,18 @@ public final class AsyncProgram {
boolean[] prime = new boolean[1000]; boolean[] prime = new boolean[1000];
prime[2] = true; prime[2] = true;
prime[3] = true; prime[3] = true;
nextPrime: for (int i = 5; i &lt; prime.length; i += 2) { nextPrime: for (int i = 5; i < prime.length; i += 2) {
int maxPrime = (int) Math.sqrt(i); int maxPrime = (int) Math.sqrt(i);
for (int j = 3; j &lt;= maxPrime; j += 2) { for (int j = 3; j <= maxPrime; j += 2) {
Thread.yield(); Thread.yield();
if (prime[j] &amp;&amp; i % j == 0) { if (prime[j] && i % j == 0) {
continue nextPrime; continue nextPrime;
} }
} }
prime[i] = true; prime[i] = true;
} }
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
for (int i = 0; i &lt; 1000; ++i) { for (int i = 0; i < 1000; ++i) {
if (prime[i]) { if (prime[i]) {
sb.append(i).append(' '); sb.append(i).append(' ');
} }
@ -144,9 +135,9 @@ public final class AsyncProgram {
private static void withoutAsync() { private static void withoutAsync() {
report("Start sync"); report("Start sync");
for (int i = 0; i &lt; 20; ++i) { for (int i = 0; i < 20; ++i) {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
for (int j = 0; j &lt;= i; ++j) { for (int j = 0; j <= i; ++j) {
sb.append(j); sb.append(j);
sb.append(' '); sb.append(' ');
} }
@ -157,9 +148,9 @@ public final class AsyncProgram {
private static void withAsync() throws InterruptedException { private static void withAsync() throws InterruptedException {
report("Start async"); report("Start async");
for (int i = 0; i &lt; 20; ++i) { for (int i = 0; i < 20; ++i) {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
for (int j = 0; j &lt;= i; ++j) { for (int j = 0; j <= i; ++j) {
sb.append(j); sb.append(j);
sb.append(' '); sb.append(' ');
} }
@ -187,6 +178,7 @@ public final class AsyncProgram {
throw new IllegalStateException(); throw new IllegalStateException();
} }
} }
</pre> </pre>
</div> </div>