JAVA的协程实现

1. 前言

         在Java中使用协程,一般会用到kilim( https://github.com/kilim/kilim )这个框架。但是看了看其用法,比较难懂。跟之前python所理解的协程的使用有很大的区别。所以就上github看看有没有别的协程框架可以用。然后发现了这个框架( https://github.com/offbynull/coroutines),也就是下面实现协程所用的框架。

 

2. 介绍

如何创建协程

1. 实现Coroutine接口,重写run方法,在run方法中可以在任意位置调用coutinuation.suspend()方法暂停语句的执行并保存上下文。

2. 协程的运行通过CoroutineRunner对象来实现的。创建一个CoroutineRunner对象,这个对象需要传入一个刚才实现的Coroutine接口类进行构造。

3. 调用coroutineRunner.execute()方法,将会调用Coroutine的run方法。然后run方法里面的语句一直运行,直到run方法中调用了coroutine.suspend()方法或者已经将run方法的语句执行完,执行暂停于此。若再次执行execute方法,将继续从刚才暂停的地方继续执行。比较有意思的地方是,如果run方法已经执行完,再次执行execute方法也是可以的,此时将重新执行run方法。那怎么知道任务是否已经执行完毕呢?在上述的例子中没有表现出来,其实调用coroutineRunner.execute()之后会有一个返回值,false代表整个任务已经执行完毕了,而不是停留在中间。由此就能判断任务是否执行完毕。

 

只需三步,就能创建一个协程了,十分简单。另外,作者提到,coroutine除了有suspend方法之外,还有getContext和setContext方法,具体用法尚未研究。但是知道有suspend就足够了。

但是很快就能发现问题,如果有多个协程,他们之间应该怎样调度,怎样进行上下文切换呢?可见,这是一个十分简单的框架,只是提供了最最基本的功能,作者也没提及这方面的问题。不过也留给了我们创作的空间。

 

3. 实现

3.1 实现Coroutine接口,自定义Task

[java] view plain copy
  1. final class MyCoroutine1 implements Coroutine {  
  2.     @Override  
  3.     public void run(Continuation c) {  
  4.         System.out.println("[TASK 1] In task 1, doing something...");  
  5.         System.out.println("[TASK 1] Switch to other task.");  
  6.         c.suspend();  
  7.         System.out.println("[TASK 1] In task 1, doing something...");  
  8.         System.out.println("[TASK 1] Switch to other task.");  
  9.         c.suspend();  
  10.         System.out.println("[TASK 1] In task 1, doing something...");  
  11.         System.out.println("[TASK 1] Finish task 1 !");  
  12.     }  
  13. }  
  14.   
  15. final class MyCoroutine2 implements Coroutine {  
  16.     @Override  
  17.     public void run(Continuation c) {  
  18.         System.out.println("[TASK 2] In task 2, doing something...");  
  19.         System.out.println("[TASK 2] Switch to other task.");  
  20.         c.suspend();  
  21.         System.out.println("[TASK 2] In task 2, doing something...");  
  22.         System.out.println("[TASK 2] Finish task 2 !");  
  23.     }  
  24.   
  25. }  
  26.   
  27. final class MyCoroutine3 implements Coroutine {  
  28.     @Override  
  29.     public void run(Continuation c) {  
  30.         System.out.println("[TASK 3] In task 3, doing something...");  
  31.         System.out.println("[TASK 3] Finish task 3 !");  
  32.     }  
  33. }  

 

3.2 设计调度器

我的设计是维护一个就绪队列,每次取队头的CoroutineRunner(Task)调用一次execute,如果Task尚未完成,就把它放回队尾,这样就可以使每一个任务得到轮流的执行。

自定义一个MyCoroutineRunner类,join方法用于将Task放进队列,execute方法用于执行队列中的任务。

 

[java] view plain copy
  1. class MyCoroutineRunner {   
  2.     Queue<CoroutineRunner> coroutineQueue = new LinkedList<CoroutineRunner>();  
  3.     public void join(Coroutine c) {   
  4.         // 往就绪队列塞Task  
  5.         coroutineQueue.add(new CoroutineRunner(c));  
  6.     }  
  7.     public void execute() {   
  8.         while (!coroutineQueue.isEmpty()) {  
  9.             System.out.printf("[MAIN] Current number of tasks: %d\n", coroutineQueue.size());  
  10.             CoroutineRunner coroutineRunner = coroutineQueue.remove();  
  11.             boolean notFinish = coroutineRunner.execute(); // 执行协程  
  12.             if (notFinish) {   
  13.                 // 若协程没有完成,继续丢到队尾  
  14.                 coroutineQueue.add(coroutineRunner);  
  15.             }  
  16.         }  
  17.     }  
  18. }  

 

3.3 测试代码

主函数代码如下,创建了3个任务,每个任务都有不同的特点(挂起的次数和时机不一样)。

 

[java] view plain copy
  1. package com.dct.jay;  
  2.   
  3. import java.util.LinkedList;  
  4. import java.util.Queue;  
  5.   
  6.   
  7.   
  8. import com.offbynull.coroutines.user.Continuation;  
  9. import com.offbynull.coroutines.user.Coroutine;  
  10. import com.offbynull.coroutines.user.CoroutineRunner;  
  11.   
  12. public class TestRunner {  
  13.     public static void main(String args[]) {  
  14.         MyCoroutineRunner myCoroutineRunner = new MyCoroutineRunner();  
  15.         myCoroutineRunner.join(new MyCoroutine1());  
  16.         myCoroutineRunner.join(new MyCoroutine2());  
  17.         myCoroutineRunner.join(new MyCoroutine3());  
  18.         System.out.println("=== Start ===");  
  19.         myCoroutineRunner.execute();  
  20.         System.out.println("=== All tasks finished ! ===");  
  21.     }  
  22. }  

3.4 构建项目

         使用ant构建项目。

需要注意的是,在编译完成之后,运行之前,需要做一次Instrument(插桩、侵入)。

插桩就是在代码中插入一段我们自定义的代码。记得在戴尔IT课程上做的APM系统也有用到插桩技术,为了监控服务器的性能,需要对服务器的字节码插入代码。在这里,也需要进行插桩。难道导入一个包,创建几个对象就能实现协程?当然没那么容易。通过使用这种“黑魔法”才能让语句的执行流程暂停。

[html] view plain copy
  1. <?xml version="1.0" encoding="UTF-8" standalone="no"?>  
  2. <!-- WARNING: Eclipse auto-generated file.  
  3.               Any modifications will be overwritten.  
  4.               To include a user specific buildfile here, simply create one in the same  
  5.               directory with the processing instruction <?eclipse.ant.import?>  
  6.               as the first entry and export the buildfile again. --><project basedir="." default="build" name="JavaCoroutine">  
  7.     <property environment="env"/>  
  8.     <property name="debuglevel" value="source,lines,vars"/>  
  9.     <property name="target" value="7"/>  
  10.     <property name="source" value="7"/>  
  11.     <path id="JavaCoroutine.classpath">  
  12.         <pathelement location="./bin"/>  
  13.         <pathelement location="./jar/user-1.1.1.jar"/>  
  14.   
  15.     </path>  
  16.     <taskdef name="InstrumentTask" classname="com.offbynull.coroutines.antplugin.InstrumentTask">  
  17.         <classpath>  
  18.             <pathelement location="./jar/ant-plugin-1.1.1-shaded.jar"/>  
  19.         </classpath>  
  20.     </taskdef>  
  21.     <target name="init">  
  22.         <mkdir dir="bin"/>  
  23.         <copy includeemptydirs="false" todir="bin">  
  24.             <fileset dir="src">  
  25.                 <exclude name="**/*.launch"/>  
  26.                 <exclude name="**/*.java"/>  
  27.             </fileset>  
  28.         </copy>  
  29.     </target>  
  30.     <target name="-post-compile">  
  31.         <!-- The classpath attribute is a semicolon delimited list of the classpath required by your code. -->  
  32.         <InstrumentTask classpath="./bin" sourceDirectory="bin" targetDirectory="bin"/>  
  33.     </target>  
  34.     <target name="clean">  
  35.         <delete dir="bin"/>  
  36.     </target>  
  37.     <target depends="clean" name="cleanall"/>  
  38.     <target depends="build-subprojects,build-project,-post-compile" name="build"/>  
  39.     <target name="build-subprojects"/>  
  40.     <target depends="init" name="build-project">  
  41.         <echo message="${ant.project.name}: ${ant.file}"/>  
  42.         <javac debug="true" debuglevel="${debuglevel}" destdir="bin" includeantruntime="false" source="${source}" target="${target}">  
  43.             <src path="src"/>  
  44.             <compilerarg line="-encoding UTF-8 " />    
  45.             <classpath refid="JavaCoroutine.classpath"/>  
  46.         </javac>  
  47.     </target>  
  48.     <target name="TestRunner">  
  49.         <java classname="com.dct.jay.TestRunner" failonerror="true" fork="yes">  
  50.             <classpath refid="JavaCoroutine.classpath"/>  
  51.         </java>  
  52.     </target>  
  53. </project>  


 

3.5 运行

运行结果如下,发现输出与每个预设的task行为是一致的。

 JAVA的协程实现


4. 总结

虽然能够实现协程上下文自由切换,但是仿佛并没有什么实质上的用途,算是一个半成品。像python这种线程是硬伤的语言才需要用到协程这种折衷的策略。协程对于Java来说毕竟还是有点非主流啊!