现在 先看一个 使用Runnable接口实现求数值积分的java多线程并行程序如下(电脑是四核,并行3个线程):
package com.xing.add;
/**
*
* @author Yinxing
*
*/
public class PI_Runnable {
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
//-------------------------并行运算------------------------------
long startTime ,endTime;
double step = 1.0 / 100000000;
works works1 = new works(1, 100000000);
works works2 = new works(2, 100000000);
works works3 = new works(3, 100000000);
Thread thread1 = new Thread(works1);
Thread thread2 = new Thread(works2);
Thread thread3 = new Thread(works3);
startTime = System.currentTimeMillis();//获取并行计算的开始时的系统时间
thread1.start();
thread2.start();
thread3.start();