Results 1 to 4 of 4
  1. #1
    dav
    dav is offline Member
    Join Date
    May 2011
    Posts
    2
    Rep Power
    0

    Default The differences made by Synchronized method

    Hi everybody,
    I'm just a newbie in Java Threads and Synchronization. I made a program to reveal the differences whether I used the synchronized method or not.
    However I did not see any differences whether I used the synchronized method or not.
    I wonder perhaps I misunderstand something about synchronization. Would you mind explaining for me ?
    Here, I put the synchronized keyword in the
    Java Code:
    increment()
    method
    Java Code:
    class Data {
        private int x = 0;
        
        Data() {
        }
    
        public void inc() {
    	x ++;	
        }
    
        public int get() {
    	return x;
        }
    }
    
    class MyThread extends Thread {	
        private Data data;
    
        MyThread(Data data) {
    	this.data = data;
    	start();
        }
    
        public synchronized void increment() {
    	String threadName = Thread.currentThread().getName();
    	int old = data.get();
    	data.inc();
    	System.out.println(old + " " + threadName + ": " + data.get());
        }
    
        public void run() {
    	for (int i = 0; i < 10; i ++) {
    	    increment();
    	}
    	try {
    	    sleep(1000);
    	}
    	catch (InterruptedException e) {
    	}
        }
    } 
    
    public class MultiThreads {
    
        public static void main(String args[]) {
    	Data data = new Data();
    	MyThread t1 = new MyThread(data);
    	MyThread t2 = new MyThread(data);
        }
    }
    Thank you for your help and Im so sorry if I've said something wrong

  2. #2
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default

    A thread entering a synchronized method obtains a lock on the object that method is a member of. As you've written it, each thread is obtaining a lock on itself, i.e., MyThread.this. Since you have two separate instances of MyThread, synchronizing there does absolutely nothing.

    What you'd want to do here is make Data.inc() and Data.get() both synchronized. Both MyThread instances share the same Data instance. Making inc() synchronized ensures that only one thread at a time may enter the inc() method on that particular Data object. This is important because the ++ operator is not atomic. The importance of making get() synchronized is less obvious. It ensures that get() returns the true updated value from the Data object, and not some old value that was cached by the current thread.
    Get in the habit of using standard Java naming conventions!

  3. #3
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default

    If you want to demonstrate incorrect behavior when threads are not properly synchronized, here are a couple things you could try:

    - Have two threads repeatedly add two to a variable, with no synchronization. See if the variable ever changes from even to odd. Edit: actually, that won't work. Nevermind. Better idea: have one thread increment by 1, and one thread increment by 100. If the read/add/write sequences get interleaved, you might see the value go down by 99 at some point. It would happen like this:

    Thread 1: read value n
    Thread 2: read value n
    Thread 2: write value n + 100
    Thread 2: print n + 100
    Thread 1: write value n + 1
    Thread 1: print n + 1

    - Have one thread increment a variable while another thread repeatedly reads and prints it, with no synchronization. You MIGHT see the second thread keep printing the same value. It depends on your JVM, and it's more likely on multi-CPU/multi-core machines.
    Last edited by kjkrum; 05-25-2011 at 08:23 AM. Reason: hurr
    Get in the habit of using standard Java naming conventions!

  4. #4
    dav
    dav is offline Member
    Join Date
    May 2011
    Posts
    2
    Rep Power
    0

Similar Threads

  1. calling yield() method in synchronized block
    By Ash-infinity in forum New To Java
    Replies: 2
    Last Post: 12-04-2012, 06:35 PM
  2. Replies: 13
    Last Post: 05-16-2010, 01:09 AM
  3. what made paintComponent() method to be called twice??
    By Y. Progammer in forum New To Java
    Replies: 5
    Last Post: 02-21-2010, 11:19 PM
  4. [SOLVED] Non-synchronized instance method of an Object
    By piyu.sha in forum Threads and Synchronization
    Replies: 2
    Last Post: 10-06-2008, 07:35 AM
  5. Differences between constructor and method
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-19-2007, 09:53 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
  •