Difference between revisions of "CSC352 Synchronization and Java Threads"
(→It all starts at the Lowest Level: Assembly Language (again)) |
(→It all starts at the Lowest Level: Assembly Language (again)) |
||
Line 32: | Line 32: | ||
: What is a way around the problem, such that two parallel update of the variable '''sum''' will increment it by 2, every time? | : What is a way around the problem, such that two parallel update of the variable '''sum''' will increment it by 2, every time? | ||
+ | =Demo= | ||
<br /> | <br /> | ||
+ | <source lang="java"> | ||
+ | /* | ||
+ | * UnsynchronizedThreadExample.java | ||
+ | * D. Thiebaut | ||
+ | * Undocumented code that computes Pi with 2 threads, but is terribly | ||
+ | * flawed in the way it updates the global sum... | ||
+ | */ | ||
+ | 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 ) { | ||
+ | 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; | ||
+ | UnsynchronizedThreadExample U = new UnsynchronizedThreadExample(); | ||
+ | U.process( N ); | ||
+ | } | ||
+ | |||
+ | } | ||
+ | </source> | ||
+ | <br /> | ||
+ | ==Output== | ||
+ | |||
+ | '''java DT.UnsynchronizedThreadExample ''' | ||
+ | sum = 198846243 | ||
+ | Execution time: 25 ms | ||
+ | |||
+ | <br /> | ||
+ | |||
=Critical Section= | =Critical Section= | ||
<tanbox> | <tanbox> |
Revision as of 14:23, 9 September 2013
--D. Thiebaut (talk) 14:10, 9 September 2013 (EDT)
This page treats of the concepts of synchronization of parallel programs in general, applied to the Java platform in particular.
Contents
References
A good source of information for the material presented here is
The programs covered in this page can be found in this page:
- Computing Pi and Synchronization programs
It all starts at the Lowest Level: Assembly Language (again)
Let's look at a different parallel computation of Pi. Imagine that the two threads in the ParallelPi program updated a global sum variable by doing something like this?
sum += thread_sum;
where thread_sum is a local variable used by the thread function to accumulate the terms of the series. The actual program can be found here.
- Question 1
- Figure out the assembly language for the java statement above
- Question 2
- Assume that main memory is a stack of index cards. One index card is sum. Two people in the class represent two different processors. Their notebook represent their registers. Execute these instructions simultaneously and figure out if there's a way for their simultaneous operation to fail.
- Question 3
- What makes the code fail? What is the processor missing?
- Question 4
- What is a way around the problem, such that two parallel update of the variable sum will increment it by 2, every time?
Demo
/*
* UnsynchronizedThreadExample.java
* D. Thiebaut
* Undocumented code that computes Pi with 2 threads, but is terribly
* flawed in the way it updates the global sum...
*/
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 ) {
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;
UnsynchronizedThreadExample U = new UnsynchronizedThreadExample();
U.process( N );
}
}
Output
java DT.UnsynchronizedThreadExample sum = 198846243 Execution time: 25 ms
Critical Section
A series of instructions that are executed in an atomic fashion.