版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u013491262/article/details/81237656
整理工作中常用的多线程场景
1、异步操作
ExecutorService 的corePoolSize核心线程数可以理解为【同一时刻】最多处理的个数,规则:
- 1.线程数量未达到corePoolSize,则新建一个线程(核心线程)执行任务
- 2.线程数量达到了corePools,则将任务移入队列等待
import org.apache.commons.lang3.time.DateFormatUtils;
import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class Main {
public static void main(String[] args) throws Exception {
ExecutorService executor = new ThreadPoolExecutor(3, 5, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>());
out("begin executor");
for (int i = 1; i <= 10; i++) {
final int a = i;
executor.submit(new Runnable() {
@Override
public void run() {
gao(a);
}
});
}
out("end executor");
}
static void gao(int i) {
out("begin i=" + i);
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
}
out("end i=" + i);
}
static void out(String s) {
System.out.println(String.format("[%s]:%s", DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"), s));
}
}
核心线程数设置为3
[2018-07-27 15:05:34]:begin executor
[2018-07-27 15:05:34]:begin i=2
[2018-07-27 15:05:34]:begin i=3
[2018-07-27 15:05:34]:begin i=1
[2018-07-27 15:05:34]:end executor
[2018-07-27 15:05:36]:end i=2
[2018-07-27 15:05:36]:end i=1
[2018-07-27 15:05:36]:end i=3
[2018-07-27 15:05:36]:begin i=4
[2018-07-27 15:05:36]:begin i=5
[2018-07-27 15:05:36]:begin i=6
[2018-07-27 15:05:38]:end i=5
[2018-07-27 15:05:38]:end i=4
[2018-07-27 15:05:38]:end i=6
[2018-07-27 15:05:38]:begin i=7
[2018-07-27 15:05:38]:begin i=9
[2018-07-27 15:05:38]:begin i=8
[2018-07-27 15:05:40]:end i=9
[2018-07-27 15:05:40]:end i=7
[2018-07-27 15:05:40]:begin i=10
[2018-07-27 15:05:40]:end i=8
[2018-07-27 15:05:42]:end i=10
核心线程数设置为1
[2018-07-27 15:10:32]:begin executor
[2018-07-27 15:10:32]:end executor
[2018-07-27 15:10:32]:begin i=1
[2018-07-27 15:10:34]:end i=1
[2018-07-27 15:10:34]:begin i=2
[2018-07-27 15:10:36]:end i=2
[2018-07-27 15:10:36]:begin i=3
[2018-07-27 15:10:38]:end i=3
[2018-07-27 15:10:38]:begin i=4
[2018-07-27 15:10:40]:end i=4
[2018-07-27 15:10:40]:begin i=5
[2018-07-27 15:10:42]:end i=5
[2018-07-27 15:10:42]:begin i=6
[2018-07-27 15:10:44]:end i=6
[2018-07-27 15:10:44]:begin i=7
[2018-07-27 15:10:46]:end i=7
[2018-07-27 15:10:46]:begin i=8
[2018-07-27 15:10:48]:end i=8
[2018-07-27 15:10:48]:begin i=9
[2018-07-27 15:10:50]:end i=9
[2018-07-27 15:10:50]:begin i=10
[2018-07-27 15:10:52]:end i=10
2、CountDownLatch同步工具
场景A,超过最多等待时间,但是没有执行完成的线程继续执行
import org.apache.commons.lang3.time.DateFormatUtils;
import java.util.Date;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class Main {
public static void main(String[] args) throws Exception {
ExecutorService executor = new ThreadPoolExecutor(1, 5, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>());
final CountDownLatch latch = new CountDownLatch(10);
out("begin executor");
for (int i = 1; i <= 10; i++) {
final int a = i;
executor.submit(new Runnable() {
@Override
public void run() {
gao(a);
latch.countDown();
}
});
}
out("await:" + latch.await(2000, TimeUnit.MILLISECONDS));
out("end executor");
}
static void gao(int i) {
out("begin i=" + i);
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
}
out("end i=" + i);
}
static void out(String s) {
System.out.println(String.format("[%s]:%s", DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"), s));
}
}
[2018-08-01 10:15:10]:begin executor
[2018-08-01 10:15:10]:begin i=1
[2018-08-01 10:15:12]:await:false
[2018-08-01 10:15:12]:end i=1
[2018-08-01 10:15:12]:end executor
[2018-08-01 10:15:12]:begin i=2
[2018-08-01 10:15:14]:end i=2
[2018-08-01 10:15:14]:begin i=3
[2018-08-01 10:15:16]:end i=3
[2018-08-01 10:15:16]:begin i=4
[2018-08-01 10:15:18]:end i=4
[2018-08-01 10:15:18]:begin i=5
[2018-08-01 10:15:20]:end i=5
[2018-08-01 10:15:20]:begin i=6
[2018-08-01 10:15:22]:end i=6
[2018-08-01 10:15:22]:begin i=7
[2018-08-01 10:15:24]:end i=7
[2018-08-01 10:15:24]:begin i=8
[2018-08-01 10:15:26]:end i=8
[2018-08-01 10:15:26]:begin i=9
[2018-08-01 10:15:28]:end i=9
[2018-08-01 10:15:28]:begin i=10
[2018-08-01 10:15:30]:end i=10
设置latch.await(2000, TimeUnit.MINUTES)
[2018-08-01 10:18:03]:begin executor
[2018-08-01 10:18:03]:begin i=1
[2018-08-01 10:18:05]:end i=1
[2018-08-01 10:18:05]:begin i=2
[2018-08-01 10:18:07]:end i=2
[2018-08-01 10:18:07]:begin i=3
[2018-08-01 10:18:09]:end i=3
[2018-08-01 10:18:09]:begin i=4
[2018-08-01 10:18:11]:end i=4
[2018-08-01 10:18:11]:begin i=5
[2018-08-01 10:18:13]:end i=5
[2018-08-01 10:18:13]:begin i=6
[2018-08-01 10:18:15]:end i=6
[2018-08-01 10:18:15]:begin i=7
[2018-08-01 10:18:17]:end i=7
[2018-08-01 10:18:17]:begin i=8
[2018-08-01 10:18:19]:end i=8
[2018-08-01 10:18:19]:begin i=9
[2018-08-01 10:18:21]:end i=9
[2018-08-01 10:18:21]:begin i=10
[2018-08-01 10:18:23]:end i=10
[2018-08-01 10:18:23]:await:true
[2018-08-01 10:18:23]:end executor