[JAVA-13959] Alligned code with article (#12982)
Co-authored-by: panagiotiskakos <panagiotis.kakos@libra-is.com>
This commit is contained in:
		
							parent
							
								
									198bf2181b
								
							
						
					
					
						commit
						ca8e33e339
					
				| @ -1,4 +1,4 @@ | |||||||
| package com.baeldung.thread.join; | package com.baeldung.concurrent.threadjoin; | ||||||
| 
 | 
 | ||||||
| import org.junit.Ignore; | import org.junit.Ignore; | ||||||
| import org.junit.Test; | import org.junit.Test; | ||||||
| @ -21,49 +21,47 @@ public class ThreadJoinUnitTest { | |||||||
| 
 | 
 | ||||||
|         SampleThread(int processingCount) { |         SampleThread(int processingCount) { | ||||||
|             this.processingCount = processingCount; |             this.processingCount = processingCount; | ||||||
|             LOGGER.debug("Thread " + this.getName() + " created"); |             LOGGER.info("Thread " + this.getName() + " created"); | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         @Override |         @Override | ||||||
|         public void run() { |         public void run() { | ||||||
|             LOGGER.debug("Thread " + this.getName() + " started"); |             LOGGER.info("Thread " + this.getName() + " started"); | ||||||
|             while (processingCount > 0) { |             while (processingCount > 0) { | ||||||
|                 try { |                 try { | ||||||
|                     Thread.sleep(1000); // Simulate some work being done by thread |                     Thread.sleep(1000); // Simulate some work being done by thread | ||||||
|                 } catch (InterruptedException e) { |                 } catch (InterruptedException e) { | ||||||
|                     LOGGER.debug("Thread " + this.getName() + " interrupted."); |                     LOGGER.info("Thread " + this.getName() + " interrupted."); | ||||||
|                 } |                 } | ||||||
|                 processingCount--; |                 processingCount--; | ||||||
|                 LOGGER.debug("Inside Thread " + this.getName() + ", processingCount = " + processingCount); |                 LOGGER.info("Inside Thread " + this.getName() + ", processingCount = " + processingCount); | ||||||
|             } |             } | ||||||
|             LOGGER.debug("Thread " + this.getName() + " exiting"); |             LOGGER.info("Thread " + this.getName() + " exiting"); | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     @Test |     @Test | ||||||
|     public void givenNewThread_whenJoinCalled_returnsImmediately() throws InterruptedException { |     public void givenNewThread_whenJoinCalled_returnsImmediately() throws InterruptedException { | ||||||
|         Thread t1 = new SampleThread(0); |         Thread t1 = new SampleThread(0); | ||||||
|         LOGGER.debug("Invoking join."); |         LOGGER.info("Invoking join"); | ||||||
|         t1.join(); |         t1.join(); | ||||||
|         LOGGER.debug("Returned from join"); |         LOGGER.info("Returned from join"); | ||||||
|         LOGGER.debug("Thread state is" + t1.getState()); |         LOGGER.info("Thread state is" + t1.getState()); | ||||||
|         assertFalse(t1.isAlive()); |         assertFalse(t1.isAlive()); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     @Test |     @Test | ||||||
|     public void givenStartedThread_whenJoinCalled_waitsTillCompletion() |     public void givenStartedThread_whenJoinCalled_waitsTillCompletion() throws InterruptedException { | ||||||
|       throws InterruptedException { |  | ||||||
|         Thread t2 = new SampleThread(1); |         Thread t2 = new SampleThread(1); | ||||||
|         t2.start(); |         t2.start(); | ||||||
|         LOGGER.debug("Invoking join."); |         LOGGER.info("Invoking join"); | ||||||
|         t2.join(); |         t2.join(); | ||||||
|         LOGGER.debug("Returned from join"); |         LOGGER.info("Returned from join"); | ||||||
|         assertFalse(t2.isAlive()); |         assertFalse(t2.isAlive()); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     @Test |     @Test | ||||||
|     public void givenStartedThread_whenTimedJoinCalled_waitsUntilTimedout() |     public void givenStartedThread_whenTimedJoinCalled_waitsUntilTimedout() throws InterruptedException { | ||||||
|       throws InterruptedException { |  | ||||||
|         Thread t3 = new SampleThread(10); |         Thread t3 = new SampleThread(10); | ||||||
|         t3.start(); |         t3.start(); | ||||||
|         t3.join(1000); |         t3.join(1000); | ||||||
| @ -72,19 +70,17 @@ public class ThreadJoinUnitTest { | |||||||
| 
 | 
 | ||||||
|     @Test |     @Test | ||||||
|     @Ignore |     @Ignore | ||||||
|     public void givenThreadTerminated_checkForEffect_notGuaranteed() |     public void givenThreadTerminated_checkForEffect_notGuaranteed() throws InterruptedException { | ||||||
|       throws InterruptedException { |  | ||||||
|         SampleThread t4 = new SampleThread(10); |         SampleThread t4 = new SampleThread(10); | ||||||
|         t4.start(); |         t4.start(); | ||||||
|       //not guaranteed to stop even if t4 finishes. |         //not guaranteed to stop even if t4 finishes. | ||||||
|         do { |         do { | ||||||
| 
 | 
 | ||||||
|         } while (t4.processingCount > 0); |         } while (t4.processingCount > 0); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     @Test |     @Test | ||||||
|     public void givenJoinWithTerminatedThread_checkForEffect_guaranteed() |     public void givenJoinWithTerminatedThread_checkForEffect_guaranteed() throws InterruptedException { | ||||||
|       throws InterruptedException { |  | ||||||
|         SampleThread t4 = new SampleThread(10); |         SampleThread t4 = new SampleThread(10); | ||||||
|         t4.start(); |         t4.start(); | ||||||
|         do { |         do { | ||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user