麻烦帮忙看看如下的程序,为什么对sum_thread2的同步完全没有起到作用呢?谢谢。 package multiThread; /*
* Still use the first way to write a thread
* Has mistake in this code
*/
public class moreMultiThreadExamples implements Runnable {
private double sum = 0;
private double [] f1;
private double [] f2;
private int vectorLength = 100000;
private double sum_thread1 = 0;
private Double sum_thread2 = 0.0;
public void run() {
synchronized( sum_thread2 ) {
for( int i = f1.length / 2 ; i < f1.length ; i ++ ) {
//System.out.println( "f1[" + i + "]=" + f1[i] + "," + "f2[" + i + "]=" +
f2[i]);
sum_thread2 += f1[i] * f2[i];
}
}
}
public void vectorMultiplicationMultiThread() {
Thread mythread = new Thread( this );
mythread.start();
for( int i = 0 ; i < f1.length / 2 ; i ++ ) {
//System.out.println( "f1[" + i + "]=" + f1[i] + "," + "f2[" + i + "]=" +
f2[i]);
sum_thread1 += f1[i] * f2[i];
}
/*此处,不等mythread结束就会接着算出sum然后打印输出,所以导致结果不对*/
synchronized( this.sum_thread2 ) {
sum = sum_thread1 + sum_thread2;
}
System.out.println( sum );
}
public void vectorMultiplicationSingleThread() {
sum = 0;
for( int i = 0 ; i < f1.length ; i ++ ) {
//System.out.println( "f1[" + i + "]=" + f1[i] + "," + "f2[" + i + "]=" +
f2[i]);
sum += f1[i] * f2[i];
}
System.out.println( sum );
}
public void init() {
f1 = new double [vectorLength];
f2 = new double [vectorLength];
java.util.Random rd = new java.util.Random(System.currentTimeMillis());
for( int i = 0 ; i < f1.length; i ++ ) {
f1[i] = rd.nextDouble();
f2[i] = rd.nextDouble();
}
}
public static void main(String [] args) {
moreMultiThreadExamples myexample = new moreMultiThreadExamples();
myexample.init();
long starttime = System.currentTimeMillis();
myexample.vectorMultiplicationSingleThread();
long endtime = System.currentTimeMillis();
System.out.println( "Single thread:" + (endtime - starttime) + "ms" );
starttime = System.currentTimeMillis();
myexample.vectorMultiplicationMultiThread();
endtime = System.currentTimeMillis();
System.out.println( "Multi thread:" + (endtime - starttime) + "ms" ); /* Thread mythread = new Thread(myexample);
mythread.start();
for( int i = 0 ; i < myexample.f1.length / 2 ; i ++ ) {
//System.out.println( "f1[" + i + "]=" + f1[i] + "," + "f2[" + i + "]=" +
f2[i]);
myexample.sum_thread1 += myexample.f1[i] * myexample.f2[i];
}
synchronized( myexample.sum_thread2 ) {
myexample.sum = myexample.sum_thread1 + myexample.sum_thread2;
}
System.out.println( myexample.sum ); */
}
}
* Still use the first way to write a thread
* Has mistake in this code
*/
public class moreMultiThreadExamples implements Runnable {
private double sum = 0;
private double [] f1;
private double [] f2;
private int vectorLength = 100000;
private double sum_thread1 = 0;
private Double sum_thread2 = 0.0;
public void run() {
synchronized( sum_thread2 ) {
for( int i = f1.length / 2 ; i < f1.length ; i ++ ) {
//System.out.println( "f1[" + i + "]=" + f1[i] + "," + "f2[" + i + "]=" +
f2[i]);
sum_thread2 += f1[i] * f2[i];
}
}
}
public void vectorMultiplicationMultiThread() {
Thread mythread = new Thread( this );
mythread.start();
for( int i = 0 ; i < f1.length / 2 ; i ++ ) {
//System.out.println( "f1[" + i + "]=" + f1[i] + "," + "f2[" + i + "]=" +
f2[i]);
sum_thread1 += f1[i] * f2[i];
}
/*此处,不等mythread结束就会接着算出sum然后打印输出,所以导致结果不对*/
synchronized( this.sum_thread2 ) {
sum = sum_thread1 + sum_thread2;
}
System.out.println( sum );
}
public void vectorMultiplicationSingleThread() {
sum = 0;
for( int i = 0 ; i < f1.length ; i ++ ) {
//System.out.println( "f1[" + i + "]=" + f1[i] + "," + "f2[" + i + "]=" +
f2[i]);
sum += f1[i] * f2[i];
}
System.out.println( sum );
}
public void init() {
f1 = new double [vectorLength];
f2 = new double [vectorLength];
java.util.Random rd = new java.util.Random(System.currentTimeMillis());
for( int i = 0 ; i < f1.length; i ++ ) {
f1[i] = rd.nextDouble();
f2[i] = rd.nextDouble();
}
}
public static void main(String [] args) {
moreMultiThreadExamples myexample = new moreMultiThreadExamples();
myexample.init();
long starttime = System.currentTimeMillis();
myexample.vectorMultiplicationSingleThread();
long endtime = System.currentTimeMillis();
System.out.println( "Single thread:" + (endtime - starttime) + "ms" );
starttime = System.currentTimeMillis();
myexample.vectorMultiplicationMultiThread();
endtime = System.currentTimeMillis();
System.out.println( "Multi thread:" + (endtime - starttime) + "ms" ); /* Thread mythread = new Thread(myexample);
mythread.start();
for( int i = 0 ; i < myexample.f1.length / 2 ; i ++ ) {
//System.out.println( "f1[" + i + "]=" + f1[i] + "," + "f2[" + i + "]=" +
f2[i]);
myexample.sum_thread1 += myexample.f1[i] * myexample.f2[i];
}
synchronized( myexample.sum_thread2 ) {
myexample.sum = myexample.sum_thread1 + myexample.sum_thread2;
}
System.out.println( myexample.sum ); */
}
}
......
mythread.start();
// 在这个地方加入下面代码
try {
mythread.join();
} catch (Exception e) {
}
//do anything,maybe have a long time.
};});
mythread.start();
//do anything,maybe have a long time.
// flag 1
try {
mythread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
//use result of mythread run
// flag 2主线程先启动了一个子线程 mythread ,并且随即让其运行。然后主线程在 flag 1 处进行一个耗时的运算。然后,他要等待 mythread 的运行结果。