comsat java_java 协程

package com.zhou.quasar.quasar;

import java.util.List;

import java.util.concurrent.CountDownLatch;

import java.util.concurrent.ExecutionException;

import java.util.concurrent.ExecutorService;

import java.util.concurrent.Executors;

import java.util.concurrent.atomic.AtomicInteger;

import java.util.concurrent.atomic.LongAdder;

import java.util.stream.Collectors;

import java.util.stream.Stream;

import co.paralleluniverse.fibers.Fiber;

import co.paralleluniverse.fibers.SuspendExecution;

import co.paralleluniverse.fibers.Suspendable;

import co.paralleluniverse.strands.Strand;

import co.paralleluniverse.strands.SuspendableRunnable;

public class Helloworld {

@Suspendable

static void m1() throws InterruptedException, SuspendExecution {

String m = "m1";

// System.out.println("m1 begin");

m = m2();

// System.out.println("m1 end");

// System.out.println(m);

}

static String m2() throws SuspendExecution, InterruptedException {

String m = m3();

Strand.sleep(1000);

return m;

}

@Suspendable

static String m3() {

List l = Stream.of(1, 2, 3).filter(i -> i % 2 == 0).collect(Collectors.toList());

return l.toString();

}

public static void main(String[] args) throws ExecutionException, InterruptedException {

int count = 10000;

testFiber(count);

testThreadpool(count);

}

static void testThreadpool(int count) throws InterruptedException {

final CountDownLatch latch = new CountDownLatch(count);

ExecutorService es = Executors.newFixedThreadPool(200);

LongAdder latency = new LongAdder();

LongAdder counter = new LongAdder();

long t = System.currentTimeMillis();

WrappyInteger sum=new WrappyInteger(0);

for (int i = 0; i

es.submit(() ->{

// Long long1=sum;

long start = System.currentTimeMillis();

try {

m1();

} catch (InterruptedException e) {

e.printStackTrace();

} catch (SuspendExecution suspendExecution) {

suspendExecution.printStackTrace();

}

start = System.currentTimeMillis() - start;

latency.add(start);

counter.add(1);

sum.i++;

latch.countDown();

});

}

latch.await();

t = System.currentTimeMillis() - t;

long l = latency.longValue() / count;

System.out.println("thread pool took: " + t + ", latency: " + l + " ms------"+counter.longValue()+"sum ---"+sum.i);

es.shutdownNow();

}

static void testFiber(int count) throws InterruptedException {

final CountDownLatch latch = new CountDownLatch(count);

LongAdder latency = new LongAdder();

LongAdder counter = new LongAdder();

WrappyInteger sum=new WrappyInteger(0);

long t = System.currentTimeMillis();

for (int i = 0; i

new Fiber("Caller", new SuspendableRunnable() {

@Override

public void run() throws SuspendExecution, InterruptedException {

long start = System.currentTimeMillis();

m1();

start = System.currentTimeMillis() - start;

counter.add(1);

latency.add(start);

sum.i++;

latch.countDown();

}

}).start();

}

latch.await();

t = System.currentTimeMillis() - t;

long l = latency.longValue() / count;

System.out.println("fiber took: " + t + ", latency: " + l + " ms,------"+counter+"--sum"+sum.i);

}

static class WrappyInteger{

public int i;

public WrappyInteger(int i) {

this.i = i;

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值