1、串行程序
串行程序是基于嵌入式Linux串行通信GUI終端設(shè)計(jì)及實(shí)現(xiàn)。傳統(tǒng)意義上的寫(xiě)法,我們得到的往往會(huì)是串行執(zhí)行的程序形態(tài),程序的總的執(zhí)行時(shí)間是method1的執(zhí)行時(shí)間time1加上method2的執(zhí)行時(shí)間time2,這樣總的執(zhí)行時(shí)間time=time1+time2。我們得到的是串行的程序形態(tài)。
import com.yang.domain.BaseResult;
import java.util.concurrent.TimeUnit;
/**
* @Description:
* @Author: yangzl2008
* @Date: 2016/1/9 19:06
*/
public class Serial {
@Test
public void test() {
long start = System.currentTimeMillis();
BaseResult baseResult1 = method1();// 耗時(shí)操作1,時(shí)間 time1
BaseResult baseResult2 = method2();// 耗時(shí)操作2,時(shí)間 time2
long end = System.currentTimeMillis();
//總耗時(shí) time = time1 + time2
System.out.println(“baseResult1 is ” + baseResult1 + “\nbaseResult2 is ” + baseResult2 + “\ntime cost is ” + (end - start));
}
private BaseResult method1() {
BaseResult baseResult = new BaseResult();
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
baseResult.setCode(1);
baseResult.setMsg(“method1”);
return baseResult;
}
private BaseResult method2() {
BaseResult baseResult = new BaseResult();
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
baseResult.setCode(1);
baseResult.setMsg(“method2”);
return baseResult;
}
}
執(zhí)行結(jié)果:
?。踦lain]
BaseResult{code=1, msg=‘method1’}
baseResult2 is BaseResult{code=1, msg=‘method2’}
time cost is 2000
2、串行程序的多線程過(guò)渡
而這種代碼是不是可優(yōu)化的地方呢?加快程序的執(zhí)行效率,降低程序的執(zhí)行時(shí)間。在這里method1和method2是相互不關(guān)聯(lián)的,即method1的執(zhí)行和method2的執(zhí)行位置可以調(diào)整,而不影響程序的執(zhí)行結(jié)果,我們可不可以為建立線程1執(zhí)行method1然后建立線程2來(lái)執(zhí)行method2呢,因?yàn)閙ethod1和method2都需要得到結(jié)果,因此我們需要使用Callable接口,然后使用Future.get()得到執(zhí)行的結(jié)果,但實(shí)際上Future.get()在程序返回結(jié)果之前是阻塞的,即,線程1在執(zhí)行method1方式時(shí),程序因?yàn)檎{(diào)用了Future.get()會(huì)等待在這里直到method1返回結(jié)果result1,然后線程2才能執(zhí)行method2,同樣,F(xiàn)uture.get()也會(huì)一直等待直到method2的結(jié)果result2返回,這樣,我們開(kāi)啟了線程1,開(kāi)啟了線程2并沒(méi)有得到并發(fā)執(zhí)行method1,method2的效果,反而會(huì)因?yàn)槌绦蜷_(kāi)啟線程而多占用了程序的執(zhí)行時(shí)間,這樣程序的執(zhí)行時(shí)間time=time1+time2+time(線程開(kāi)啟時(shí)間)。于是我們得到了串行程序的過(guò)渡態(tài)。
?。踛ava] view plain copyimport com.yang.domain.BaseResult;
import org.junit.Test;
import java.lang.reflect.Method;
import java.util.concurrent.*;
/**
* @Description:
* @Author: yangzl2008
* @Date: 2016/1/9 19:13
*/
public class SerialCallable {
@Test
public void test01() throws Exception {
// 兩個(gè)線程的線程池
ExecutorService executorService = Executors.newFixedThreadPool(2);
long start = System.currentTimeMillis();
// 開(kāi)啟線程執(zhí)行
Future《BaseResult》 future1 = executorService.submit(new Task(this, “method1”, null));
// 阻塞,直到程序返回。耗時(shí)time1
BaseResult baseResult1 = future1.get();
// 開(kāi)啟線程執(zhí)行
Future《BaseResult》 future2 = executorService.submit(new Task(this, “method1”, null));
// 阻塞,直到程序返回。耗時(shí)time2
BaseResult baseResult2 = future2.get();
long end = System.currentTimeMillis();
// 總耗時(shí) time = time1 + time2 + time(線程執(zhí)行耗時(shí))
System.out.println(“baseResult1 is ” + baseResult1 + “\nbaseResult2 is ” + baseResult2 + “\ntime cost is ” + (end - start));
}
public BaseResult method1() {
BaseResult baseResult = new BaseResult();
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
baseResult.setCode(1);
baseResult.setMsg(“method1”);
return baseResult;
}
public BaseResult method2() {
BaseResult baseResult = new BaseResult();
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
baseResult.setCode(1);
baseResult.setMsg(“method2”);
return baseResult;
}
class Task《T》 implements Callable《T》 {
private Object object;
private Object[] args;
private String methodName;
public Task(Object object, String methodName, Object[] args) {
this.object = object;
this.args = args;
this.methodName = methodName;
}
@Override
public T call() throws Exception {
Method method = object.getClass().getMethod(methodName);
return (T) method.invoke(object, args);
}
}
}
執(zhí)行結(jié)果:
?。踦lain]
BaseResult{code=1, msg=‘method1’}
baseResult2 is BaseResult{code=1, msg=‘method1’}
time cost is 2001
評(píng)論
查看更多