Refactor volatile (#2444)
This commit is contained in:
		
							parent
							
								
									3b5d9585ed
								
							
						
					
					
						commit
						6db067d2c9
					
				| @ -4,7 +4,7 @@ package com.baeldung.concurrent.volatilekeyword; | |||||||
| public class SharedObject { | public class SharedObject { | ||||||
|     private volatile int count=0; |     private volatile int count=0; | ||||||
| 
 | 
 | ||||||
|     public void increamentCount(){ |     void increamentCount(){ | ||||||
|         count++; |         count++; | ||||||
|     } |     } | ||||||
|     public int  getCount(){ |     public int  getCount(){ | ||||||
|  | |||||||
| @ -8,9 +8,9 @@ import static junit.framework.Assert.assertEquals; | |||||||
| 
 | 
 | ||||||
| public class SharedObjectManualTest { | public class SharedObjectManualTest { | ||||||
| 
 | 
 | ||||||
|     SharedObject sharedObject; |     private SharedObject sharedObject; | ||||||
|     int valueReadByThread2; |     private int valueReadByThread2; | ||||||
|     int valueReadByThread3; |     private int valueReadByThread3; | ||||||
| 
 | 
 | ||||||
|     @Before |     @Before | ||||||
|     public void setUp() { |     public void setUp() { | ||||||
| @ -19,36 +19,28 @@ public class SharedObjectManualTest { | |||||||
| 
 | 
 | ||||||
|     @Test |     @Test | ||||||
|     public void whenOneThreadWrites_thenVolatileReadsFromMainMemory() throws InterruptedException { |     public void whenOneThreadWrites_thenVolatileReadsFromMainMemory() throws InterruptedException { | ||||||
|         Thread writer = new Thread(){ |         Thread writer = new Thread(() -> sharedObject.increamentCount()); | ||||||
|             public void run(){ |  | ||||||
|                 sharedObject.increamentCount(); |  | ||||||
|             } |  | ||||||
|         }; |  | ||||||
|         writer.start(); |         writer.start(); | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|         Thread readerOne = new Thread(){ |         Thread readerOne = new Thread(() -> { | ||||||
|             public void run(){ |  | ||||||
|             try { |             try { | ||||||
|                 Thread.sleep(1000); |                 Thread.sleep(1000); | ||||||
|             } catch (InterruptedException e) { |             } catch (InterruptedException e) { | ||||||
|                 e.printStackTrace(); |                 e.printStackTrace(); | ||||||
|             } |             } | ||||||
|             valueReadByThread2 = sharedObject.getCount(); |             valueReadByThread2 = sharedObject.getCount(); | ||||||
|             } |         }); | ||||||
|         }; |  | ||||||
|         readerOne.start(); |         readerOne.start(); | ||||||
| 
 | 
 | ||||||
|         Thread readerTwo = new Thread(){ |         Thread readerTwo = new Thread(() -> { | ||||||
|             public void run(){ |  | ||||||
|             try { |             try { | ||||||
|                 Thread.sleep(1000); |                 Thread.sleep(1000); | ||||||
|             } catch (InterruptedException e) { |             } catch (InterruptedException e) { | ||||||
|                 e.printStackTrace(); |                 e.printStackTrace(); | ||||||
|             } |             } | ||||||
|             valueReadByThread3 = sharedObject.getCount(); |             valueReadByThread3 = sharedObject.getCount(); | ||||||
|             } |         }); | ||||||
|         }; |  | ||||||
|         readerTwo.start(); |         readerTwo.start(); | ||||||
| 
 | 
 | ||||||
|         assertEquals(1, valueReadByThread2); |         assertEquals(1, valueReadByThread2); | ||||||
| @ -58,42 +50,22 @@ public class SharedObjectManualTest { | |||||||
| 
 | 
 | ||||||
|     @Test |     @Test | ||||||
|     public void whenTwoThreadWrites_thenVolatileReadsFromMainMemory() throws InterruptedException { |     public void whenTwoThreadWrites_thenVolatileReadsFromMainMemory() throws InterruptedException { | ||||||
|         Thread writerOne = new Thread(){ |         Thread writerOne = new Thread(() -> sharedObject.increamentCount()); | ||||||
|             public void run(){ |  | ||||||
|                 sharedObject.increamentCount(); |  | ||||||
|             } |  | ||||||
|         }; |  | ||||||
|         writerOne.start(); |         writerOne.start(); | ||||||
|         Thread.sleep(100); |         Thread.sleep(100); | ||||||
| 
 | 
 | ||||||
|         Thread writerTwo = new Thread(){ |         Thread writerTwo = new Thread(() -> sharedObject.increamentCount()); | ||||||
|             public void run(){ |  | ||||||
|                 sharedObject.increamentCount(); |  | ||||||
|             } |  | ||||||
|         }; |  | ||||||
|         writerTwo.start(); |         writerTwo.start(); | ||||||
|         Thread.sleep(100); |         Thread.sleep(100); | ||||||
| 
 | 
 | ||||||
|         Thread readerOne = new Thread(){ |         Thread readerOne = new Thread(() -> valueReadByThread2 = sharedObject.getCount()); | ||||||
|             public void run(){ |  | ||||||
|                 valueReadByThread2= sharedObject.getCount(); |  | ||||||
|             } |  | ||||||
|         }; |  | ||||||
|         readerOne.start(); |         readerOne.start(); | ||||||
| 
 | 
 | ||||||
|         Thread readerTwo = new Thread(){ |         Thread readerTwo = new Thread(() -> valueReadByThread3 = sharedObject.getCount()); | ||||||
|             public void run(){ |  | ||||||
|                 valueReadByThread3=sharedObject.getCount(); |  | ||||||
|             } |  | ||||||
|         }; |  | ||||||
|         readerTwo.start(); |         readerTwo.start(); | ||||||
| 
 | 
 | ||||||
|         assertEquals(2, valueReadByThread2); |         assertEquals(2, valueReadByThread2); | ||||||
|         assertEquals(2, valueReadByThread3); |         assertEquals(2, valueReadByThread3); | ||||||
| 
 | 
 | ||||||
|     } |     } | ||||||
|     @After |  | ||||||
|     public void cleanup(){ |  | ||||||
|         sharedObject = null; |  | ||||||
|     } |  | ||||||
| } | } | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user