串行程序如何并行化,串行和并行的区别
侧边栏壁纸
  • 累计撰写 192,421 篇文章
  • 累计收到 3 条评论

串行程序如何并行化,串行和并行的区别

admin
2022-12-16 / 0 评论 / 0 阅读 / 正在检测是否收录...

1、串行程序

串行程序是基于嵌入式Linux串行通信GUI终端设计及实现。传统意义上的写法,我们得到的往往会是串行执行的程序形态,程序的总的执行时间是method1的执行时间TIme1加上method2的执行时间TIme2,这样总的执行时间TIme=TIme1+time2。我们得到的是串行的程序形态。

import com.yang.domain.BaseResult;

import org.junit.Test;

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();// 耗时操作1,时间 time1

BaseResult baseResult2 = method2();// 耗时操作2,时间 time2

long end = System.currentTimeMillis();

//总耗时 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;

}

}

执行结果:

[plain]

BaseResult{code=1, msg=‘method1’}

baseResult2 is BaseResult{code=1, msg=‘method2’}

time cost is 2000

2、串行程序的多线程过渡

而这种代码是不是可优化的地方呢?加快程序的执行效率,降低程序的执行时间。在这里method1和method2是相互不关联的,即method1的执行和method2的执行位置可以调整,而不影响程序的执行结果,我们可不可以为建立线程1执行method1然后建立线程2来执行method2呢,因为method1和method2都需要得到结果,因此我们需要使用Callable接口,然后使用Future.get()得到执行的结果,但实际上Future.get()在程序返回结果之前是阻塞的,即,线程1在执行method1方式时,程序因为调用了Future.get()会等待在这里直到method1返回结果result1,然后线程2才能执行method2,同样,Future.get()也会一直等待直到method2的结果result2返回,这样,我们开启了线程1,开启了线程2并没有得到并发执行method1,method2的效果,反而会因为程序开启线程而多占用了程序的执行时间,这样程序的执行时间time=time1+time2+time(线程开启时间)。于是我们得到了串行程序的过渡态。

[java] 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 {

// 两个线程的线程池

ExecutorService executorService = Executors.newFixedThreadPool(2);

long start = System.currentTimeMillis();

// 开启线程执行

Future《BaseResult》 future1 = executorService.submit(new Task(this, “method1”, null));

// 阻塞,直到程序返回。耗时time1

BaseResult baseResult1 = future1.get();

// 开启线程执行

Future《BaseResult》 future2 = executorService.submit(new Task(this, “method1”, null));

// 阻塞,直到程序返回。耗时time2

BaseResult baseResult2 = future2.get();

long end = System.currentTimeMillis();

// 总耗时 time = time1 + time2 + time(线程执行耗时)

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);

}

}

}

执行结果:

[plain]

BaseResult{code=1, msg=‘method1’}

baseResult2 is BaseResult{code=1, msg=‘method1’}

time cost is 2001

0

评论 (0)

取消