Skip to content

Commit 6ac6237

Browse files
author
vvedenin
committed
JAL-210 Add Quasar
1 parent cc2f005 commit 6ac6237

File tree

6 files changed

+220
-0
lines changed

6 files changed

+220
-0
lines changed

hellowords/1.1-common-frameworks/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,6 @@
2525

2626
<modules>
2727
<module>spring</module>
28+
<module>quasar</module>
2829
</modules>
2930
</project>
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>1.1-common-frameworks</artifactId>
7+
<groupId>com.github.vedenin</groupId>
8+
<version>0.01</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>quasar</artifactId>
13+
14+
<properties>
15+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
16+
</properties>
17+
18+
<dependencies>
19+
<dependency>
20+
<groupId>co.paralleluniverse</groupId>
21+
<artifactId>quasar-core</artifactId>
22+
<version>0.7.3</version>
23+
<classifier>jdk8</classifier>
24+
</dependency>
25+
26+
<dependency>
27+
<groupId>junit</groupId>
28+
<artifactId>junit</artifactId>
29+
<!-- Update version to suit your needs -->
30+
<version>4.12</version>
31+
<scope>test</scope>
32+
</dependency>
33+
</dependencies>
34+
35+
<build>
36+
<plugins>
37+
<plugin>
38+
<groupId>org.apache.maven.plugins</groupId>
39+
<artifactId>maven-compiler-plugin</artifactId>
40+
<version>3.2</version>
41+
<configuration>
42+
<source>1.8</source>
43+
<target>1.8</target>
44+
</configuration>
45+
</plugin>
46+
47+
<plugin>
48+
<artifactId>maven-dependency-plugin</artifactId>
49+
<version>2.5.1</version>
50+
<executions>
51+
<execution>
52+
<id>getClasspathFilenames</id>
53+
<goals>
54+
<goal>properties</goal>
55+
</goals>
56+
</execution>
57+
</executions>
58+
</plugin>
59+
60+
<plugin>
61+
<groupId>org.codehaus.mojo</groupId>
62+
<artifactId>exec-maven-plugin</artifactId> <!-- Run with "mvn compile maven-dependency-plugin:properties exec:exec" -->
63+
<version>1.3.2</version>
64+
<configuration>
65+
<mainClass>testgrp.QuasarHelloWorld</mainClass>
66+
<workingDirectory>target/classes</workingDirectory>
67+
<executable>java</executable>
68+
<arguments>
69+
<argument>-Dco.paralleluniverse.fibers.verifyInstrumentation=true</argument>
70+
71+
<argument>-javaagent:${co.paralleluniverse:quasar-core:jar:jdk8}</argument> <!-- Add "=b" to force instrumenting blocking calls like Thread.sleep() -->
72+
73+
<!-- Classpath -->
74+
<argument>-classpath</argument> <classpath/>
75+
76+
<!-- Main class -->
77+
<argument>testgrp.QuasarIncreasingEchoApp</argument>
78+
</arguments>
79+
</configuration>
80+
</plugin>
81+
82+
<plugin>
83+
<groupId>org.apache.maven.plugins</groupId>
84+
<artifactId>maven-surefire-plugin</artifactId>
85+
<version>2.9</version>
86+
<configuration>
87+
<argLine>-Dco.paralleluniverse.fibers.verifyInstrumentation=true</argLine>
88+
89+
<argLine>-javaagent:${co.paralleluniverse:quasar-core:jar:jdk8}</argLine>
90+
</configuration>
91+
</plugin>
92+
</plugins>
93+
</build>
94+
95+
96+
</project>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import co.paralleluniverse.fibers.Fiber;
2+
import co.paralleluniverse.fibers.SuspendExecution;
3+
4+
import java.util.concurrent.ExecutionException;
5+
6+
/**
7+
* Show simple HelloWorld using Fiber
8+
*
9+
* To run start it using "maven test"
10+
*
11+
* Created by vvedenin on 4/11/2016.
12+
*/
13+
public class FiberHelloWorld {
14+
public static Integer doTest() throws ExecutionException, InterruptedException {
15+
System.out.println("Hello world1!");
16+
Fiber<Integer> integerFiber = new Fiber<Integer>() {
17+
@Override
18+
protected Integer run() throws SuspendExecution, InterruptedException {
19+
System.out.println("Hello world2!");
20+
return 10;
21+
}
22+
}.start();
23+
System.out.println("Hello world3!");
24+
return integerFiber.get();
25+
}
26+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import co.paralleluniverse.fibers.Fiber;
2+
import co.paralleluniverse.fibers.SuspendExecution;
3+
4+
import java.util.concurrent.ExecutionException;
5+
6+
/**
7+
* Show simple HelloWorld using Fiber and sleep
8+
*
9+
* To run start it using "maven test"
10+
*
11+
* Created by vvedenin on 4/11/2016.
12+
*/
13+
public class FiberSleepHelloWorld {
14+
public static Integer doTest() throws ExecutionException, InterruptedException {
15+
System.out.println("Hello world1!");
16+
Fiber<Integer> integerFiber = new Fiber<Integer>() {
17+
@Override
18+
protected Integer run() throws SuspendExecution, InterruptedException {
19+
System.out.println("Hello world2!");
20+
Fiber.sleep(1000);
21+
System.out.println("Hello world3!");
22+
return 2;
23+
}
24+
}.start();
25+
System.out.println("Hello world4!");
26+
Integer result = integerFiber.get();
27+
System.out.println("Hello world5!");
28+
return result;
29+
}
30+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import co.paralleluniverse.fibers.Fiber;
2+
import co.paralleluniverse.fibers.SuspendExecution;
3+
import co.paralleluniverse.strands.SuspendableCallable;
4+
import co.paralleluniverse.strands.SuspendableRunnable;
5+
import co.paralleluniverse.strands.channels.Channels;
6+
import co.paralleluniverse.strands.channels.IntChannel;
7+
8+
/**
9+
* Created by vvedenin on 4/11/2016.
10+
*/
11+
public class FibersAndChanelHelloWorld {
12+
13+
public static Integer doTest() throws Exception {
14+
final IntChannel fiber1ToFiber2 = Channels.newIntChannel(0); // Synchronizing channel (buffer = 0)
15+
final IntChannel fiber2ToFiber1 = Channels.newIntChannel(0); // Synchronizing channel (buffer = 0)
16+
17+
Fiber<Integer> fiber1 = new Fiber<>("Fiber1", new SuspendableCallable<Integer>() {
18+
@Override
19+
public Integer run() throws SuspendExecution, InterruptedException {
20+
Fiber.sleep(1000);
21+
fiber1ToFiber2.send(1);
22+
Integer i1 = fiber2ToFiber1.receive();
23+
System.out.println(" Hello words " + i1);
24+
fiber1ToFiber2.send(9);
25+
Integer i2 = fiber2ToFiber1.receive();
26+
System.out.println(" Hello words " + i2);
27+
fiber1ToFiber2.send(0);
28+
return i2;
29+
}
30+
}).start();
31+
32+
Fiber<Void> fiber2 = new Fiber<Void>("Fiber2", new SuspendableRunnable() { @Override public void run() throws SuspendExecution, InterruptedException {
33+
Integer i;
34+
i = fiber1ToFiber2.receive();
35+
while(i != 0) {
36+
fiber2ToFiber1.send(i + 1);
37+
i = fiber1ToFiber2.receive();
38+
}
39+
} }).start();
40+
41+
42+
fiber1.join();
43+
fiber2.join();
44+
45+
return fiber1.get();
46+
47+
}
48+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import org.junit.Test;
2+
3+
import static org.hamcrest.CoreMatchers.is;
4+
import static org.junit.Assert.assertThat;
5+
6+
/**
7+
* Created by vvedenin on 4/11/2016.
8+
*/
9+
public class HelloWordsTest {
10+
@Test
11+
public void test() throws Exception {
12+
System.out.println("FiberHelloWorld:");
13+
assertThat(FiberHelloWorld.doTest(), is(10));
14+
System.out.println("FiberSleepHelloWorld:");
15+
assertThat(FiberSleepHelloWorld.doTest(), is(2));
16+
System.out.println("FibersAndChanelHelloWorld:");
17+
assertThat(FibersAndChanelHelloWorld.doTest(), is(10));
18+
}
19+
}

0 commit comments

Comments
 (0)