本文代码会在后期整理上传到github中
1. 没有使用多线程情况
- 代码:
package com.tinygao.thread;
import com.google.common.base.Stopwatch;
import com.tinygao.thread.executor.JobExecutionFactory;
import com.tinygao.thread.job.DBJob;
import com.tinygao.thread.job.FileJob;
import com.tinygao.thread.job.Job;
import com.tinygao.thread.job.JobType;
import lombok.extern.slf4j.Slf4j;
import java.net.URI;
import java.util.concurrent.TimeUnit;
@Slf4j
public class ExecutionSingleClient {
public static void main(String[] args) throws Exception {
final Stopwatch stopwatch = Stopwatch.createStarted();
runDBJob();
runFileJob();
log.info("total elapse time {}s", stopwatch.elapsed(TimeUnit.SECONDS));
stopwatch.stop();
}
private static void runDBJob() throws Exception {
//创建一个任务
Job dbJob = new DBJob("select * from tbl_user");
//创建一个数据库执行者
Execution dbExecution = new Execution(
dbJob,
JobExecutionFactory.createInstance(JobType.DB_JOB));
dbExecution.call();
}
private static void runFileJob() throws Exception {
//创建一个任务
Job fileJob = new FileJob(new URI("file://d:"));
//创建一个文件执行者
Execution fileExecution = new Execution(
fileJob,
JobExecutionFactory.createInstance(JobType.FILE_JOB));
fileExecution.call();
}
}
- 输出:
2017-01-14 21:34:40.129 [main] INFO com.tinygao.thread.job.Job - DB_JOB job status : STARTING, elapse time : 0s
2017-01-14 21:34:40.129 [main] INFO com.tinygao.thread.job.Job - DB_JOB job status : RUNNING, elapse time : 0s
2017-01-14 21:34:41.183 [main] INFO com.tinygao.thread.job.Job - DB_JOB job status : RUNNING, elapse time : 1s
2017-01-14 21:34:42.184 [main] INFO com.tinygao.thread.job.Job - DB_JOB job status : RUNNING, elapse time : 2s
2017-01-14 21:34:43.185 [main] INFO com.tinygao.thread.job.Job - DB_JOB job status : RUNNING, elapse time : 3s
2017-01-14 21:34:44.187 [main] INFO com.tinygao.thread.job.Job - DB_JOB job status : RUNNING, elapse time : 4s
2017-01-14 21:34:45.189 [main] INFO com.tinygao.thread.job.Job - DB_JOB job status : RUNNING, elapse time : 5s
2017-01-14 21:34:46.191 [main] INFO com.tinygao.thread.job.Job - DB_JOB job status : RUNNING, elapse time : 6s
2017-01-14 21:34:47.193 [main] INFO com.tinygao.thread.job.Job - DB_JOB job status : RUNNING, elapse time : 7s
2017-01-14 21:34:48.194 [main] INFO com.tinygao.thread.job.Job - DB_JOB job status : RUNNING, elapse time : 8s
2017-01-14 21:34:49.196 [main] INFO com.tinygao.thread.job.Job - DB_JOB job status : RUNNING, elapse time : 9s
2017-01-14 21:34:50.198 [main] INFO com.tinygao.thread.job.Job - DB_JOB job status : RUNNING, elapse time : 10s
2017-01-14 21:34:50.198 [main] INFO com.tinygao.thread.job.Job - DB_JOB job status : FINISHED, elapse time : 10s
2017-01-14 21:34:50.198 [main] INFO com.tinygao.thread.job.Job - FILE_JOB job status : STARTING, elapse time : 0s
2017-01-14 21:34:50.198 [main] INFO com.tinygao.thread.job.Job - FILE_JOB job status : RUNNING, elapse time : 0s
2017-01-14 21:34:51.200 [main] INFO com.tinygao.thread.job.Job - FILE_JOB job status : RUNNING, elapse time : 0s
2017-01-14 21:34:52.202 [main] INFO com.tinygao.thread.job.Job - FILE_JOB job status : RUNNING, elapse time : 1s
2017-01-14 21:34:53.204 [main] INFO com.tinygao.thread.job.Job - FILE_JOB job status : RUNNING, elapse time : 3s
2017-01-14 21:34:54.207 [main] INFO com.tinygao.thread.job.Job - FILE_JOB job status : RUNNING, elapse time : 4s
2017-01-14 21:34:55.208 [main] INFO com.tinygao.thread.job.Job - FILE_JOB job status : RUNNING, elapse time : 5s
2017-01-14 21:34:55.208 [main] INFO com.tinygao.thread.job.Job - FILE_JOB job status : FINISHED, elapse time : 5s
2017-01-14 21:34:55.208 [main] INFO com.tinygao.thread.ExecutionSingleClient - total elapse time 15s
2. 使用多线程情况
package com.tinygao.thread;
import com.google.common.base.Stopwatch;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import com.tinygao.thread.Execution;
import com.tinygao.thread.executor.JobExecutionFactory;
import com.tinygao.thread.job.DBJob;
import com.tinygao.thread.job.FileJob;
import com.tinygao.thread.job.Job;
import com.tinygao.thread.job.JobType;
import lombok.extern.slf4j.Slf4j;
import java.net.URI;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import static java.util.concurrent.Executors.newFixedThreadPool;
/**
* Created by gsd on 2017/1/14.
*/
@Slf4j
public class ExecutionMultiClient {
public static ThreadFactory daemonThreadsNamed(String nameFormat){
return new ThreadFactoryBuilder().setNameFormat(nameFormat).setDaemon(false).build();
}
public static void main(String[] args) throws Exception {
final Stopwatch stopwatch = Stopwatch.createStarted();
ExecutorService threads = Executors.newFixedThreadPool(
2,
daemonThreadsNamed("job-threads-%d"));
threads.submit(ExecutionMultiClient::runDBJob);
threads.submit(ExecutionMultiClient::runFileJob);
threads.shutdown();
threads.awaitTermination(1, TimeUnit.DAYS);
log.info("total elapse time {}s", stopwatch.elapsed(TimeUnit.SECONDS));
stopwatch.stop();
}
private static void runDBJob() {
//创建一个任务
Job dbJob = new DBJob("select * from tbl_user");
//创建一个数据库执行者
Execution dbExecution = new Execution(
dbJob,
JobExecutionFactory.createInstance(JobType.DB_JOB));
try {
dbExecution.call();
} catch (Exception e) {
e.printStackTrace();
}
}
private static void runFileJob() {
//创建一个任务
try {
Job fileJob = new FileJob(new URI("file://d:"));
//创建一个文件执行者
Execution fileExecution = new Execution(
fileJob,
JobExecutionFactory.createInstance(JobType.FILE_JOB));
fileExecution.call();
} catch (Exception e) {
e.printStackTrace();
}
}
}
- 输出:
2017-01-14 21:38:13.384 [job-threads-1] INFO com.tinygao.thread.job.Job - FILE_JOB job status : STARTING, elapse time : 0s
2017-01-14 21:38:13.384 [job-threads-0] INFO com.tinygao.thread.job.Job - DB_JOB job status : STARTING, elapse time : 0s
2017-01-14 21:38:13.391 [job-threads-1] INFO com.tinygao.thread.job.Job - FILE_JOB job status : RUNNING, elapse time : 0s
2017-01-14 21:38:13.391 [job-threads-0] INFO com.tinygao.thread.job.Job - DB_JOB job status : RUNNING, elapse time : 0s
2017-01-14 21:38:14.404 [job-threads-1] INFO com.tinygao.thread.job.Job - FILE_JOB job status : RUNNING, elapse time : 1s
2017-01-14 21:38:14.405 [job-threads-0] INFO com.tinygao.thread.job.Job - DB_JOB job status : RUNNING, elapse time : 1s
2017-01-14 21:38:15.426 [job-threads-0] INFO com.tinygao.thread.job.Job - DB_JOB job status : RUNNING, elapse time : 2s
2017-01-14 21:38:15.426 [job-threads-1] INFO com.tinygao.thread.job.Job - FILE_JOB job status : RUNNING, elapse time : 2s
2017-01-14 21:38:16.427 [job-threads-1] INFO com.tinygao.thread.job.Job - FILE_JOB job status : RUNNING, elapse time : 3s
2017-01-14 21:38:16.427 [job-threads-0] INFO com.tinygao.thread.job.Job - DB_JOB job status : RUNNING, elapse time : 3s
2017-01-14 21:38:17.428 [job-threads-0] INFO com.tinygao.thread.job.Job - DB_JOB job status : RUNNING, elapse time : 4s
2017-01-14 21:38:17.428 [job-threads-1] INFO com.tinygao.thread.job.Job - FILE_JOB job status : RUNNING, elapse time : 4s
2017-01-14 21:38:18.429 [job-threads-1] INFO com.tinygao.thread.job.Job - FILE_JOB job status : RUNNING, elapse time : 5s
2017-01-14 21:38:18.429 [job-threads-0] INFO com.tinygao.thread.job.Job - DB_JOB job status : RUNNING, elapse time : 5s
2017-01-14 21:38:18.429 [job-threads-1] INFO com.tinygao.thread.job.Job - FILE_JOB job status : FINISHED, elapse time : 5s
2017-01-14 21:38:19.430 [job-threads-0] INFO com.tinygao.thread.job.Job - DB_JOB job status : RUNNING, elapse time : 6s
2017-01-14 21:38:20.430 [job-threads-0] INFO com.tinygao.thread.job.Job - DB_JOB job status : RUNNING, elapse time : 7s
2017-01-14 21:38:21.432 [job-threads-0] INFO com.tinygao.thread.job.Job - DB_JOB job status : RUNNING, elapse time : 8s
2017-01-14 21:38:22.434 [job-threads-0] INFO com.tinygao.thread.job.Job - DB_JOB job status : RUNNING, elapse time : 9s
2017-01-14 21:38:23.440 [job-threads-0] INFO com.tinygao.thread.job.Job - DB_JOB job status : RUNNING, elapse time : 10s
2017-01-14 21:38:23.440 [job-threads-0] INFO com.tinygao.thread.job.Job - DB_JOB job status : FINISHED, elapse time : 10s
2017-01-14 21:38:23.440 [main] INFO com.tinygao.thread.ExecutionMultiClient - total elapse time 10s
3. 分析
- 当cpu时间片轮到某一个线程时候,他就会执行它(如果可执行)
- 同一个应用在多线程和单线程情况下,多线程获取的cpu时间可能较多
- 从上面的例子可以看出多线程在较短的时间内完成两个任务。吞吐量高。
4. 总结
from 《现代操作系统》p53
- 许多应用中有多种活动,某些活动随着时间推移会被阻塞。通过将应用程序分解成可并行运行的多个线程来优化。
- 线程比进程更轻量级,比进程更快创建和撤销。在许多系统中,创建线程较创建进程要快10~100倍。
- 若多线程都是CPU密集型,性能上无法得以提升。如果存在大量计算和大量的I/O处理,拥有多线程允许这些活动彼此重叠进行,从而加快应用程序的执行速度。
5. 遗留问题
- 需要讲解阻塞(举例:cpu指令阻塞、应用层面的IO阻塞)