package com.huang.test.concurrent;
import java.security.PrivilegedAction;
import java.security.PrivilegedExceptionAction;
import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
/**
* ExecutorService 基础测试类
* note: exec 不使用时需要shutdown。
*/
public class ExecutorServiceTest {
private String getThreadName()
{
return Thread.currentThread().getName();
}
private void log(String str)
{
System.out.println(str);
}
private void sleep(int seconds)
{
try {
Thread.sleep(seconds * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void testCallable()
{
Runnable run = () -> {
System.out.println("fuck you");
System.out.println("thread 2 " + Thread.currentThread().getName());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("thread 2 end...");
};
ExecutorService exec = Executors.newSingleThreadExecutor();
Callable<Object> callable = Executors.callable(run);
exec.submit(callable);
exec.shutdown();
try {
boolean isSuccess = exec.awaitTermination(3000, TimeUnit.MILLISECONDS);
System.out.println("fuck end..." + isSuccess);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void testCallableWithResult()
{
Runnable run = () -> {
System.out.println("rrrr start...");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("rrrr end...");
};
Callable<String> call = Executors.callable(run, "fuckyou");
ExecutorService exec = Executors.newSingleThreadExecutor();
Future<String> future = exec.submit(call);
try {
String str = future.get();
System.out.println("result " + str);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
System.out.println("testCallableT end...");
exec.shutdown();
}
public void testRunnableWithResult()
{
Runnable run = () -> {
System.out.println("test run start...");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("test run end...");
};
ExecutorService exec = Executors.newSingleThreadExecutor();
Future<String> future = exec.submit(run, "huang xiao hui");
exec.shutdown();
try {
String str = future.get();
System.out.println(str);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
log("testRunnableWithResult end ...");
}
public void testPrivi()
{
Callable<Object> call = Executors.callable(new PrivilegedAction<String>() {
@Override
public String run() {
log("test start...");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
log("test end...");
return "fffffffff";
}
});
ExecutorService exec = Executors.newSingleThreadExecutor();
Future<Object> future = exec.submit(call);
exec.shutdown();
try {
Object res = future.get();
log("res:" + res);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
public void testPriviException()
{
ExecutorService exec = Executors.newSingleThreadExecutor();
Callable<Object> call = Executors.callable(new PrivilegedExceptionAction<String>() {
@Override
public String run() throws Exception {
log("test start...");
Thread.sleep(3000);
log("test end...");
return "both shit!";
}
});
Future<Object> future = exec.submit(call);
exec.shutdown();
try {
Object res = future.get();
log(res.toString());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
log("test func end");
}
public static int sec = 1;
public void testThreadFactory()
{
Runnable run = () -> {
try {
Thread.sleep(sec++ * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
log("fuckkkk " + sec);
};
ThreadFactory factory = Executors.defaultThreadFactory();
for(int i = 0;i < 5;i++)
{
Thread th = factory.newThread(run);
th.start();
try {
th.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
log("end....");
}
public void testNewCacheThreadPool()
{
Runnable run1 = () -> {
log("start...." + Thread.currentThread().getId() + " - " + Thread.currentThread().getName());
sleep(1);
log("end....");
};
ExecutorService pool = Executors.newCachedThreadPool();
for(int i = 0;i < 10;i++)
{
pool.execute(new Thread(run1));
sleep(2);
}
pool.shutdown();
}
class MyThreadFactory extends Thread
{
}
public void testCachePoolWithFactory()
{
ThreadFactory tf = new ThreadFactory()
{
@Override
public Thread newThread(Runnable r) {
log("factory run...");
Thread th = new Thread(r);
return th;
}
};
Runnable run = () -> {
log("test start...");
sleep(2);
log("test end...");
};
ExecutorService exec = Executors.newCachedThreadPool(tf);
exec.execute(run);
}
public void testDaemonThread()
{
Runnable run = new Runnable()
{
@Override
public void run() {
log("test start...");
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
// sleep(2);
log("test end...");
}
};
ThreadFactory factory = Executors.defaultThreadFactory();
Thread th = factory.newThread(run);
th.setDaemon(false);
th.start();
}
public void testNewFixThreadPool()
{
Runnable run = new Runnable() {
@Override
public void run() {
String name = Thread.currentThread().getName();
log(name + " start...");
sleep(2);
log(name + " end...");
}
};
ExecutorService exec = Executors.newFixedThreadPool(3);
for(int i = 0;i < 10;i++)
{
exec.execute(run);
}
exec.shutdown();
}
public void testNewFixPoolInvokeAll()
{
Callable<String> run1 = ()->{
String threadName = getThreadName();
log("run1 start ... " + threadName);
sleep(2);
log("run1 end ... " + threadName);
return " run1 ";
};
Callable<String> run2 = ()->{
String tname = getThreadName();
log("run2 start ... " + tname);
sleep(3);
log("run2 end ... " + tname);
return " run2 ";
};
ArrayList<Callable<String>> list = new ArrayList<>();
list.add(run1);
list.add(run2);
ExecutorService exec = Executors.newFixedThreadPool(3);
try {
// List<Future<String>> futures = exec.invokeAll(list);
List<Future<String>> futures = exec.invokeAll(list, 1000, TimeUnit.MILLISECONDS);
StringBuffer sb = new StringBuffer();
for(Future<String> f:futures)
{
String s = f.get();
sb.append(s);
}
log("result : " + sb.toString());
} catch (InterruptedException e) {
e.printStackTrace();
}catch (ExecutionException e) {
e.printStackTrace();
}
catch (CancellationException e)//invokeAll 如果超时,触发取消异常
{
e.printStackTrace();
}
exec.shutdown();
}
public void testPoolInvokeAny(){
Callable<String> c1 = ()->{
log("c1............");
sleep(4);
return "c1";
};
Callable<String> c2 = ()->{
log("c2............");
sleep(2);
return "c2";
};
ExecutorService exec = Executors.newFixedThreadPool(3);
ArrayList<Callable<String>> list = new ArrayList<>();
list.add(c1);
list.add(c2);
try {
String futures = exec.invokeAny(list);
log("result is " + futures);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
exec.shutdown();
}
public void testPoolInvokeAnyWithTimeout(){
Callable<String> c1 = ()->{
log("c1............");
sleep(4);
return "c1";
};
Callable<String> c2 = ()->{
log("c2............");
sleep(2);
return "c2";
};
ExecutorService exec = Executors.newFixedThreadPool(3);
ArrayList<Callable<String>> list = new ArrayList<>();
list.add(c1);
list.add(c2);
try {
String futures = exec.invokeAny(list, 3, TimeUnit.SECONDS);
log("result is " + futures);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
} catch (TimeoutException e)
{
e.printStackTrace();
}
exec.shutdown();
}
public void testNewFixThreadPoolWithFactory()
{
Runnable run = new Runnable() {
@Override
public void run() {
String name = Thread.currentThread().getName();
log("test start..." + name);
sleep(2);
log("test end..." + name);
}
};
ThreadFactory factory = new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
return new Thread(r);
}
};
ExecutorService exec = Executors.newFixedThreadPool(3, factory);
for(int i = 0;i < 5;i++)
{
exec.execute(run);
}
exec.shutdown();
}
void testNewSchedulePoolDelay()
{
Runnable run = new Runnable() {
@Override
public void run() {
log("test schedule start...");
sleep(2);
log("test schedule end...");
}
};
ScheduledExecutorService exec = Executors.newScheduledThreadPool(3);
exec.schedule(run, 3000, TimeUnit.MILLISECONDS);
exec.shutdown();
}
void testNewSchedulePoolCallable()
{
Callable<String> call = new Callable<String>() {
@Override
public String call() {
String threadName = Thread.currentThread().getName();
log("test schedule start..." + threadName);
sleep(2);
log("test schedule end..." + threadName);
return "just for fuck";
}
};
ScheduledExecutorService exec = Executors.newScheduledThreadPool(3);
for(int i = 0;i < 4;i++)
{
ScheduledFuture future = exec.schedule(call, 3000, TimeUnit.MILLISECONDS);
}
// try {
// String result = (String) future.get();
// log(result);
// } catch (InterruptedException e) {
// e.printStackTrace();
// } catch (ExecutionException e) {
// e.printStackTrace();
// }
exec.shutdown();
log("test function end");
}
void testNewSchedulePoolRecycleRun()
{
AtomicInteger inc = new AtomicInteger(0);
ScheduledExecutorService exec = Executors.newScheduledThreadPool(3);
Runnable run = ()->{
log("test run...");
if(inc.incrementAndGet() == 5)
{
exec.shutdown();
}
};
exec.scheduleAtFixedRate(run, 1000, 2000, TimeUnit.MILLISECONDS);
}
void testNewSchedulePoolRecycleMultiRun()
{
Runnable run = new Runnable() {
@Override
public void run() {
String name = Thread.currentThread().getName();
log("test start ... " + name);
sleep(2);
log("test end ... " + name);
}
};
ScheduledExecutorService execs = Executors.newScheduledThreadPool(3);
for(int i = 0;i < 5;i++)
{
execs.scheduleAtFixedRate(run, 1000, 2000, TimeUnit.MILLISECONDS);
}
}
void testSinglePool()
{
Runnable run = new Runnable() {
@Override
public void run() {
String threadName = Thread.currentThread().getName();
log("test run start...." + threadName);
sleep(2);
log("test run end...." + threadName);
}
};
ExecutorService exec = Executors.newSingleThreadExecutor();
for(int i = 0;i < 5;i++)
{
exec.execute(run);
}
exec.shutdown();
}
void testSinglePoolFactory()
{
ThreadFactory factory = new ThreadFactory() {
private ThreadFactory defaultFactory = Executors.defaultThreadFactory();
@Override
public Thread newThread(Runnable r) {
Thread th = defaultFactory.newThread(r);
log("aaaaaaaaaaaaaaa");
return th;
}
};
Runnable run = ()->{
String threadName = Thread.currentThread().getName();
log("run start..." + threadName);
sleep(1);
log("run end..." + threadName);
};
ExecutorService exec = Executors.newSingleThreadExecutor(factory);
for(int i = 0;i < 2;i++)
{
exec.execute(run);
}
exec.shutdown();
}
void testSinglePoolSchedule()
{
ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor();
for(int i = 0;i < 5;i++)
{
exec.schedule(new Runnable() {
@Override
public void run() {
String tname = getThreadName();
log("test start..." + tname);
sleep(2);
log("test end..." + tname);
}
}, 1000, TimeUnit.MILLISECONDS);
}
exec.shutdown();
}
void testSinglePoolScheduleWithFactory()
{
ThreadFactory factory = new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
Thread th = new Thread(r);
log("factory run..." + th.getName());
return th;
}
};
ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor(factory);
for(int i = 0;i < 5;i++)
{
exec.schedule(new Runnable() {
@Override
public void run() {
String tname = getThreadName();
log("test start..." + tname);
sleep(2);
log("test end..." + tname);
}
}, 1000, TimeUnit.MILLISECONDS);
}
exec.shutdown();
}
public static void main(String[] args)
{
ExecutorServiceTest et = new ExecutorServiceTest();
et.testSinglePoolScheduleWithFactory();
System.out.println("thread main " + Thread.currentThread().getName());
}
}
ExecutorService 测试
猜你喜欢
转载自blog.csdn.net/huangxiaohui123/article/details/82979378
今日推荐
周排行