|
| 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 | +} |
0 commit comments