1. 什么是纤程?

纤程:用户态的线程,线程中的线程,切换和调度不需要经过OS

2. 纤程和线程的区别

主要的区别是

3. 纤程的优势

  1. 占有资源很少,启动一个县城操作系统前前后后要为它分配大概1M的资源, 纤程Fiber是4K
  2. 切换比较简单
  3. 启动很多个10W+

目前(2020年3月22日)支持内置纤程的语言:Kotlin Scala Go Python(lib)… Java?(open jdk : loom)

4. 纤程的应用场景

很短的计算任务,不需要和内核打交道,并发量高!

5. 纤程的使用(利用Quaser库)

  1. <!-- https://mvnrepository.com/artifact/co.paralleluniverse/quasar-core -->
  2. <dependency>
  3. <groupId>co.paralleluniverse</groupId>
  4. <artifactId>quasar-core</artifactId>
  5. <version>0.4.0</version>
  6. </dependency>
  1. import co.paralleluniverse.fibers.Fiber;
  2. import co.paralleluniverse.fibers.SuspendExecution;
  3. import co.paralleluniverse.strands.SuspendableRunnable;
  4. import java.util.concurrent.ExecutionException;
  5. /**
  6. * 线程和纤程的对比
  7. * 启动10000个纤程和10000个线程,分别进行相同的计算量calculate(), 对比线程和纤程的性能
  8. */
  9. public class FiberAndThread {
  10. //启动的纤程/线程数量
  11. public static final int SIZE = 10000;
  12. //计算
  13. static void calculate() {
  14. int result = 0;
  15. for (int m = 0; m < 10000; m++) {
  16. for (int i = 0; i < 200; i++) {
  17. result += i;
  18. }
  19. }
  20. }
  21. //测试纤程
  22. public static void testFiber() throws ExecutionException, InterruptedException {
  23. //开始计时
  24. long start = System.currentTimeMillis();
  25. //初始化纤程数组
  26. Fiber<Void>[] fibers = new Fiber[SIZE];
  27. for (int i = 0; i < fibers.length; i++) {
  28. fibers[i] = new Fiber<Void>(new SuspendableRunnable() {
  29. public void run() throws SuspendExecution, InterruptedException {
  30. calculate();
  31. }
  32. });
  33. }
  34. //启动
  35. for (int i = 0; i < fibers.length; i++) {
  36. fibers[i].start();
  37. }
  38. //加入当前线程
  39. for (int i = 0; i < fibers.length; i++) {
  40. fibers[i].join();
  41. }
  42. //结束计时
  43. long end = System.currentTimeMillis();
  44. //计算并打印耗时
  45. System.out.println("纤程耗时"+(end - start));
  46. }
  47. //测试线程
  48. public static void testThread() throws InterruptedException {
  49. //开始计时
  50. long start = System.currentTimeMillis();
  51. Runnable r = new Runnable() {
  52. @Override
  53. public void run() {
  54. calculate();
  55. }
  56. };
  57. //初始化线程数组
  58. Thread[] threads = new Thread[SIZE];
  59. //填充
  60. for (int i = 0; i < threads.length; i++) {
  61. threads[i] = new Thread(r);
  62. }
  63. //启动
  64. for (int i = 0; i < threads.length; i++) {
  65. threads[i].start();
  66. }
  67. //加入当前线程
  68. for (int i = 0; i < threads.length; i++) {
  69. threads[i].join();
  70. }
  71. //结束计时
  72. long end = System.currentTimeMillis();
  73. //计算并打印耗时
  74. System.out.println("线程耗时"+(end - start));
  75. }
  76. public static void main(String[] args) throws InterruptedException, ExecutionException {
  77. //测试线程
  78. for (int i = 0 ; i < 10; i++){
  79. testThread();
  80. }
  81. //测试纤程
  82. for (int i = 0 ; i < 10; i++){
  83. testFiber();
  84. }
  85. }
  86. }

测试结果:

线程耗时577 线程耗时606

线程耗时605

线程耗时537

线程耗时497

线程耗时507

线程耗时494

线程耗时513

线程耗时501

线程耗时505

纤程耗时143

纤程耗时8

纤程耗时8

纤程耗时6

纤程耗时5

纤程耗时7

纤程耗时5

纤程耗时8

纤程耗时5

纤程耗时4