Skip to content

Commit 45e0a8e

Browse files
synchronization demo
1 parent fd4d889 commit 45e0a8e

File tree

2 files changed

+173
-0
lines changed

2 files changed

+173
-0
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/**
2+
*
3+
*/
4+
package sporadic.thread.synchronization.withSynchronization;
5+
6+
/**
7+
* This produces the SAME result every time you run this program, in contrast to
8+
* the one without synchronization. Copied from this link:
9+
* http://www.tutorialspoint.com/java/java_thread_synchronization.htm, refer to
10+
* this link for further info.
11+
*
12+
* When we start two or more threads within a program, there may be a situation
13+
* when multiple threads try to access the same resource and finally they can
14+
* produce unforeseen result due to concurrency issue. For example if multiple
15+
* threads try to write within a same file then they may corrupt the data
16+
* because one of the threads can overrite data or while one thread is opening
17+
* the same file at the same time another thread might be closing the same file.
18+
*
19+
* So there is a need to synchronize the action of multiple threads and make
20+
* sure that only one thread can access the resource at a given point in time.
21+
* This is implemented using a concept called monitors. Each object in Java is
22+
* associated with a monitor, which a thread can lock or unlock. Only one thread
23+
* at a time may hold a lock on a monitor.
24+
*
25+
* Java programming language provides a very handy way of creating threads and
26+
* synchronizing their task by using synchronized blocks. You keep shared
27+
* resources within this block. Following is the general form of the
28+
* synchronized statement:
29+
*
30+
* synchronized(objectidentifier) {
31+
* // Access shared variables and other shared resources
32+
* }
33+
*
34+
* Here, the objectidentifier is a reference to an object whose lock associates
35+
* with the monitor that the synchronized statement represents. Now we are going
36+
* to see two examples where we will print a counter using two different
37+
* threads. When threads are not synchronized, they print counter value which is
38+
* not in sequence, but when we print counter by putting inside synchronized()
39+
* block, then it prints counter very much in sequence for both the threads.
40+
*/
41+
public class TestThread {
42+
public static void main(String args[]) {
43+
44+
PrintDemo PD = new PrintDemo();
45+
46+
ThreadDemo T1 = new ThreadDemo("Thread - 1 ", PD);
47+
ThreadDemo T2 = new ThreadDemo("Thread - 2 ", PD);
48+
49+
T1.start();
50+
T2.start();
51+
52+
// wait for threads to end
53+
try {
54+
T1.join();
55+
T2.join();
56+
} catch (Exception e) {
57+
System.out.println("Interrupted");
58+
}
59+
}
60+
}
61+
62+
class PrintDemo {
63+
public void printCount() {
64+
try {
65+
for (int i = 5; i > 0; i--) {
66+
System.out.println("Counter --- " + i);
67+
}
68+
} catch (Exception e) {
69+
System.out.println("Thread interrupted.");
70+
}
71+
}
72+
73+
}
74+
75+
class ThreadDemo extends Thread {
76+
private Thread t;
77+
private String threadName;
78+
PrintDemo PD;
79+
80+
ThreadDemo(String name, PrintDemo pd) {
81+
threadName = name;
82+
PD = pd;
83+
}
84+
85+
public void run() {
86+
synchronized (PD) {//Here's all the difference between the two examples! It uses this synchronized keyword to identify the resources that need to be synchronized!
87+
PD.printCount();
88+
}
89+
System.out.println("Thread " + threadName + " exiting.");
90+
}
91+
92+
public void start() {
93+
System.out.println("Starting " + threadName);
94+
if (t == null) {
95+
t = new Thread(this, threadName);
96+
t.start();
97+
}
98+
}
99+
100+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/**
2+
*
3+
*/
4+
package sporadic.thread.synchronization.withoutSynchronization;
5+
6+
/**
7+
* @author jiahuan
8+
* This produces DIFFERENT result every time you run this program, in contrast to the one with synchronization.
9+
* Here is a simple example which may or may not print counter value in sequence and every time we run it, it produces different result based on CPU availability to a thread.
10+
* Copied from this link: http://www.tutorialspoint.com/java/java_thread_synchronization.htm, refer to this link for further info.
11+
*
12+
*/
13+
public class TestThread {
14+
public static void main(String args[]) {
15+
16+
PrintDemo PD = new PrintDemo();
17+
18+
ThreadDemo T1 = new ThreadDemo( "Thread - 1 ", PD );
19+
ThreadDemo T2 = new ThreadDemo( "Thread - 2 ", PD );
20+
21+
T1.start();
22+
T2.start();
23+
24+
// wait for threads to end
25+
try {
26+
T1.join();
27+
T2.join();
28+
} catch( Exception e) {
29+
System.out.println("Interrupted");
30+
}
31+
}
32+
33+
}
34+
35+
36+
class PrintDemo {
37+
public void printCount(){
38+
try {
39+
for(int i = 5; i > 0; i--) {
40+
System.out.println("Counter --- " + i );
41+
}
42+
} catch (Exception e) {
43+
System.out.println("Thread interrupted.");
44+
}
45+
}
46+
47+
}
48+
49+
class ThreadDemo extends Thread {
50+
private Thread t;
51+
private String threadName;
52+
PrintDemo PD;
53+
54+
ThreadDemo( String name, PrintDemo pd){
55+
threadName = name;
56+
PD = pd;
57+
}
58+
public void run() {
59+
PD.printCount();
60+
System.out.println("Thread " + threadName + " exiting.");
61+
}
62+
63+
public void start ()
64+
{
65+
System.out.println("Starting " + threadName );
66+
if (t == null)
67+
{
68+
t = new Thread (this, threadName);
69+
t.start ();
70+
}
71+
}
72+
73+
}

0 commit comments

Comments
 (0)