package com.team.gaoguangjin.thread.style;
import lombok.extern.slf4j.Slf4j;
/**
* @ClassName:NotUseThread.java
* @Description:业务场景:上班时间
* @author gaoguangjin
* @Date 2015-3-19 下午5:40:05
*/
@Slf4j
public class NotUseThread {
public static void main(String[] args) throws InterruptedException {
// 早上上班路上50分钟时间
gotoWorkNormal();
}
private static void gotoWorkNormal() throws InterruptedException {
long beginTime = System.currentTimeMillis();
// 1、坐车30
takeTheSubWay();
// 2、吃饭10
eatBreakFast();
// 3、看新闻10
readNews();
long endTime = System.currentTimeMillis();
log.info("总耗费时间:" + (endTime - beginTime) / 100 + "分钟");
}
private static void readNews() throws InterruptedException {
Thread.sleep(1000);
log.info("看新闻花费10分钟");
}
private static void eatBreakFast() throws InterruptedException {
Thread.sleep(1000);
log.info("吃饭花费10分钟");
}
private static void takeTheSubWay() throws InterruptedException {
Thread.sleep(3000);
log.info("坐车花费30分钟");
}
}
2015-03-19 17:53:28,566 INFO [main] (NotUseThread.java:51) - 坐车花费30分钟
2015-03-19 17:53:29,569 INFO [main] (NotUseThread.java:46) - 吃饭花费10分钟
2015-03-19 17:53:30,569 INFO [main] (NotUseThread.java:40) - 看新闻花费10分钟
2015-03-19 17:53:30,569 INFO [main] (NotUseThread.java:34) - 总耗费时间:50分钟
整个上班用了50分钟哎,是不是挺长的。
如果我们用线程来跑 看看会咋样!
package com.team.gaoguangjin.thread.style;
import lombok.extern.slf4j.Slf4j;
/**
* @ClassName:UseThread.java
* @Description: 业务场景。多线程上班
* @author gaoguangjin
* @Date 2015-3-19 下午5:59:04
*/
@Slf4j
public class UseThread extends Thread {
static long beginTime = 0;
static long endTime = 0;
public static void main(String[] args) throws InterruptedException {
beginTime = System.currentTimeMillis();
// 早上上班路上50分钟时间
new UseThread().start();
gotoWorkNormal();
log.info("利用坐车时间吃饭和看新闻");
}
public void run() {
try {
// 1、坐车30
takeTheSubWay();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private static void gotoWorkNormal() throws InterruptedException {
// 2、吃饭10
eatBreakFast();
// 3、看新闻10
readNews();
}
private static void readNews() throws InterruptedException {
Thread.sleep(1000);
log.info("看新闻花费10分钟");
}
private static void eatBreakFast() throws InterruptedException {
Thread.sleep(1000);
log.info("吃饭花费10分钟");
}
private static void takeTheSubWay() throws InterruptedException {
Thread.sleep(3000);
log.info("坐车花费30分钟");
endTime = System.currentTimeMillis();
log.info("总耗费时间:" + (endTime - beginTime) / 100 + "分钟");
}
}