Commit 978a2515 authored by lihao's avatar lihao

提交多线程任务执行返回值

parent 05f0f0ca
......@@ -2,6 +2,7 @@ package com.mushiny.task;
import java.util.Collection;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.BiFunction;
/**
......@@ -46,17 +47,16 @@ public class TaskExecutor<T, U, R> {
/**
* 执行函数
*/
public void execute() {
public boolean execute() {
TaskDefinition<T, U, R> t = this.task;
Collection<T> data = t.getData();
countDownLatch = new CountDownLatch(data.size());
AtomicInteger failCount = new AtomicInteger();
data.forEach(d -> pool.execute(() -> {
try {
Object apply = t.getHandler().apply(d, t.getContext());
System.out.println("bi func result: " + apply);
} catch (Exception ex) {
failCount.addAndGet(1);
} finally {
countDownLatch.countDown();
}
......@@ -68,6 +68,7 @@ public class TaskExecutor<T, U, R> {
} finally {
this.destroy();
}
return failCount.get() <= 0;
}
/**
......
package com.mushiny.task;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.List;
......@@ -11,6 +14,7 @@ import java.util.concurrent.TimeUnit;
public class TaskTests {
private int SLEEP_TIME = 100;
private List<User> list;
......@@ -24,102 +28,76 @@ public class TaskTests {
@Test
public void multiTaskLoopNoArgsTest() {
long startTime = System.currentTimeMillis();
TaskExecutor<User, User, String> executor = TaskPoolFactory.createExecutor();
TaskDefinition<User, User, String> taskDefinition = new TaskDefinition<>(list, (data, context) -> {
System.out.println("enter bi func, current Thread:" + Thread.currentThread());
System.out.println(data);
data.setName(data.getName() + "handle");
System.out.println(context);
System.out.println("exit bi func");
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "success";
}, new User(30, "leehyoo"));
executor.init((data, context) -> {
System.out.println("enter bi func, current Thread:" + Thread.currentThread());
System.out.println(data);
data.setName(data.getName() + "handle");
System.out.println(context);
System.out.println("exit bi func");
try {
Thread.sleep(200);
Thread.sleep(SLEEP_TIME);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "success";
}, list, new User(30, "leehyoo"));
executor.execute();
System.out.println("finally over. cost: " + (System.currentTimeMillis() - startTime));
boolean result = executor.execute();
Assert.assertTrue(result);
}
@Test
public void normalLoopTest() {
long startTime = System.currentTimeMillis();
User context = new User(30, "leehyoo");
list.forEach(data -> {
System.out.println("enter bi func, current Thread:" + Thread.currentThread());
System.out.println(data);
data.setName(data.getName() + "handle");
System.out.println(context);
System.out.println("exit bi func");
try {
Thread.sleep(500);
Thread.sleep(SLEEP_TIME);
} catch (InterruptedException e) {
e.printStackTrace();
}
});
System.out.println("finally over. cost: " + (System.currentTimeMillis() - startTime));
}
@Test
public void multiTaskLoopWithArgsTest() {
long startTime = System.currentTimeMillis();
TaskExecutor<User, User, String> executor = TaskPoolFactory.createExecutor(new ThreadPoolExecutor(4, 4,
TaskExecutor<User, User, String> executor = TaskPoolFactory.createExecutor(new ThreadPoolExecutor(5, 6,
0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>()));
TaskDefinition<User, User, String> taskDefinition = new TaskDefinition<>(list, (data, context) -> {
System.out.println("enter bi func, current Thread:" + Thread.currentThread());
System.out.println(data);
executor.init((data, context) -> {
data.setName(data.getName() + "handle");
System.out.println(context);
System.out.println("exit bi func");
try {
Thread.sleep(200);
Thread.sleep(SLEEP_TIME);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "success";
}, new User(30, "leehyoo"));
}, list, new User(30, "leehyoo"));
boolean result = executor.execute();
Assert.assertTrue(result);
}
@Test
public void failCaseTest() {
TaskExecutor<User, User, String> executor = TaskPoolFactory.createExecutor();
executor.init((data, context) -> {
System.out.println("enter bi func, current Thread:" + Thread.currentThread());
System.out.println(data);
data.setName(data.getName() + "handle");
System.out.println(context);
System.out.println("exit bi func");
try {
Thread.sleep(200);
Thread.sleep(SLEEP_TIME);
} catch (InterruptedException e) {
e.printStackTrace();
}
context.getAge();
return "success";
}, list, new User(30, "leehyoo"));
executor.execute();
System.out.println("finally over. cost: " + (System.currentTimeMillis() - startTime));
}, list, null);
boolean result = executor.execute();
Assert.assertFalse(result);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment