Difference between revisions of "CSC352: Java Threads and Synchronization Examples"

From dftwiki3
Jump to: navigation, search
(Created page with "--~~~~ ---- =A Badly Written (and Flawed) Multithreaded Computation of Pi= <br /> <source lang="java"> package DT; public class UnsynchronizedThreadExample { static int su...")
 
(A Synchronized Version of the Same Program)
Line 63: Line 63:
 
Note also that the execution time is quite fast: 19 ms.
 
Note also that the execution time is quite fast: 19 ms.
  
 +
<br />
 
=A Synchronized Version of the Same Program=
 
=A Synchronized Version of the Same Program=
  
Line 136: Line 137:
 
   
 
   
 
Note that the result is now correct.  However the execution time is 400 longer!
 
Note that the result is now correct.  However the execution time is 400 longer!
 +
 +
<br />
 +
=A second way of synchronizing the threaded computation of Pi=
 +
 +
<br />
 +
<source lang="java">
 +
 +
package DT;
 +
 +
public class SynchronizedThreadExample2 {
 +
 +
static int sum = 0;
 +
 +
class PiThreadBad extends Thread {
 +
private int N; // the total number of samples/iterations
 +
 +
public PiThreadBad( int Id, int N ) {
 +
super( "Thread-"+Id ); // give a name to the thread
 +
this.N = N;
 +
}
 +
 +
@Override
 +
public void run() {
 +
for ( int i=0; i<N; i++ )
 +
incrementSum();
 +
}
 +
}
 +
 +
private synchronized void incrementSum() {
 +
sum++;
 +
}
 +
 +
public void process( int N ) {
 +
long startTime = System.currentTimeMillis();
 +
 +
PiThreadBad t1 = new PiThreadBad( 0, N );
 +
PiThreadBad t2 = new PiThreadBad( 1, N );
 +
 +
//--- start two threads ---
 +
t1.start();
 +
t2.start();
 +
 +
//--- wait till they finish ---
 +
try {
 +
t1.join();
 +
t2.join();
 +
} catch (InterruptedException e) {
 +
e.printStackTrace();
 +
}
 +
 +
System.out.println( "sum = " + sum );
 +
System.out.println( "Execution time: " + (System.currentTimeMillis()-startTime) + " ms" );
 +
 +
}
 +
 +
public static void main(String[] args) {
 +
int N = 100000000;
 +
SynchronizedThreadExample2 U = new SynchronizedThreadExample2();
 +
U.process( N );
 +
}
 +
 +
}
 +
 +
</source>
 +
<br />

Revision as of 20:17, 4 September 2013

--D. Thiebaut (talk) 21:12, 4 September 2013 (EDT)


A Badly Written (and Flawed) Multithreaded Computation of Pi


package DT;

public class UnsynchronizedThreadExample {

	static int sum = 0;
	
	class PiThreadBad extends Thread {
		private int N;			// the total number of samples/iterations 

		public PiThreadBad( int Id, int N ) {
			super( "Thread-"+Id ); // give a name to the thread
			this.N 		= N;
		}
			
		@Override
		public void run() {
			for ( int i=0; i<N; i++ )
				sum ++;
		}
	}

	public void process( int N ) {
		PiThreadBad t1 = new PiThreadBad( 0, N );
		PiThreadBad t2 = new PiThreadBad( 1, N );
		
		//--- start two threads ---
		t1.start();
		t2.start();
		
		//--- wait till they finish ---
		try {
			t1.join();
			t2.join();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
		System.out.println( "sum = " + sum );
	}
	
	public static void main(String[] args) {
		int N = 100000000;
		UnsynchronizedThreadExample U = new UnsynchronizedThreadExample();
		U.process( N );
	}

}


Output

sum = 180612836
Execution time: 19 ms

Note that the sum should really be 200000000, as both threads increment sum 100000000 times. The result is certainly incorrect.

Note also that the execution time is quite fast: 19 ms.


A Synchronized Version of the Same Program

We decide to put the statement that increments the variable sum into a function, and ask Java to synchronize around the function, i.e. make sure than only one thread at a time runs through this function. In other word, the synchronized function becomes atomic for threads.


package DT;

public class SynchronizedThreadExample {

	int sum = 0;
	Integer lock=0;
	
	SynchronizedThreadExample() {
		sum = 0;
	}
	
	class PiThreadBad extends Thread {
		private int N;			// the total number of samples/iterations 

		public PiThreadBad( int Id, int N ) {
			super( "Thread-"+Id ); // give a name to the thread
			this.N 		= N;
		}
			
		@Override
		public void run() {
			for ( int i=0; i<N; i++ )
				synchronized( lock ) {
					sum++;
				}
		}
	}
	
	
	public void process( int N ) {
		long startTime = System.currentTimeMillis();

		PiThreadBad t1 = new PiThreadBad( 0, N );
		PiThreadBad t2 = new PiThreadBad( 1, N );
		
		//--- start two threads ---
		t1.start();
		t2.start();
		
		//--- wait till they finish ---
		try {
			t1.join();
			t2.join();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
		System.out.println( "sum = " + sum );
		System.out.println( "Execution time: " + (System.currentTimeMillis()-startTime) + " ms" );
	}
	
	public static void main(String[] args) {
		int N = 100000000;
		SynchronizedThreadExample U = new SynchronizedThreadExample();
		U.process( N );
	}

}


Output

sum = 200000000
Execution time: 8448 ms

Note that the result is now correct. However the execution time is 400 longer!


A second way of synchronizing the threaded computation of Pi


package DT;

public class SynchronizedThreadExample2 {

	static int sum = 0;
	
	class PiThreadBad extends Thread {
		private int N;			// the total number of samples/iterations 

		public PiThreadBad( int Id, int N ) {
			super( "Thread-"+Id ); // give a name to the thread
			this.N 		= N;
		}
			
		@Override
		public void run() {
			for ( int i=0; i<N; i++ )
				incrementSum();
		}
	}
	
	private synchronized void incrementSum() {
		sum++;
	}
	
	public void process( int N ) {
		long startTime = System.currentTimeMillis();

		PiThreadBad t1 = new PiThreadBad( 0, N );
		PiThreadBad t2 = new PiThreadBad( 1, N );
		
		//--- start two threads ---
		t1.start();
		t2.start();
		
		//--- wait till they finish ---
		try {
			t1.join();
			t2.join();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
		System.out.println( "sum = " + sum );
		System.out.println( "Execution time: " + (System.currentTimeMillis()-startTime) + " ms" );

	}
	
	public static void main(String[] args) {
		int N = 100000000;
		SynchronizedThreadExample2 U = new SynchronizedThreadExample2();
		U.process( N );
	}

}