Results 1 to 2 of 2
  1. #1
    gandrew is offline Member
    Join Date
    Nov 2010
    Posts
    8
    Rep Power
    0

    Default Synchronized methods example, main and background thread

    This is an example from Effective Java 2nd Ed, Joshua Bloch. Properly synchronized cooperative thread termination.
    I found this program really strange concerning program flow. I put printlns into the author's example as a help. The
    main thread is going to terminate the backgroundThread after a millisecond. I would have thought that the backgroundThread
    would run forever in the while loop. I was wrong because the main thread proceeds after executing the start of the backgroundThead
    followed by the time delay and finally calling requestStop(). Meanwhile the background thread is incrementing var i and constantly
    checking the stopRequested boolean.

    Bloch writes "The actions of the synchronized methods in StopThread would be atomic even without synchronization. In other words, the synchronization on these methods is used solely for its communication effects, not for mutual exclusion."

    I think I answered my own question, but I still have to think about this.

    Java Code:
    import java.util.concurrent.TimeUnit;
    
    public class StopThread {
    
    	private static boolean stopRequested;  // initially false
    	
    	// Write method
    	private static synchronized void requestStop(){
    		System.out.println("requestStop method setting stopRequested = true");
    		stopRequested = true;
    	}
    	// Read method
    	private static synchronized boolean stopRequested(){
    		System.out.println("stopRequested method returns " + stopRequested);
    		return stopRequested;
    	}
    	
    	public static void main(String[] args) throws InterruptedException {
    		System.out.println("Main...");
    		System.out.println("Value of stopRequested is " + stopRequested);
    		
    		Thread backgroundThread = new Thread(new Runnable(){
    			public void run() {
    				int i=0;
    				while(!stopRequested()){
    					System.out.println(i++);
    				}
    			}
    		});
    		backgroundThread.start();
    		
    		System.out.println("Activate sleep");
    		//TimeUnit.SECONDS.sleep(1);
    		TimeUnit.MILLISECONDS.sleep(1);
    		System.out.println("Main thread call to requestStop method");
    		requestStop();
    		
    		System.out.println("End Main...");
    	}
    
    }
    Last edited by Tolls; 07-20-2016 at 06:05 PM. Reason: Fix tags

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Synchronized methods example, main and background thread

    The main thread runs as long as stopRequested() returns false.
    The main thread calls requestStop() which sets the stopRequested field to true.
    But the while loop only runs while stopRequested() returns false. So when
    it returns true, the loop ends.

    BTW, I bought that book at least 5+ years ago. Imho it is excellent.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Synchronized Blocks in Static Methods Confusion
    By MasterChief in forum Threads and Synchronization
    Replies: 10
    Last Post: 07-23-2014, 09:23 AM
  2. Thread synchronization and the synchronized modifier
    By A GJP in forum Threads and Synchronization
    Replies: 4
    Last Post: 08-07-2012, 08:17 PM
  3. Qns on synchronized methods
    By guest_user in forum New To Java
    Replies: 1
    Last Post: 07-18-2011, 04:26 PM
  4. Replies: 9
    Last Post: 09-20-2009, 10:14 AM
  5. Replies: 3
    Last Post: 10-06-2008, 12:27 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •