Results 1 to 4 of 4
- 05-25-2011, 04:17 AM #1
Member
- Join Date
- May 2011
- Posts
- 2
- Rep Power
- 0
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 themethodJava Code:increment()
Thank you for your help and Im so sorry if I've said something wrongJava 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); } }
- 05-25-2011, 07:13 AM #2
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!
- 05-25-2011, 07:18 AM #3
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 07:23 AM. Reason: hurr
Get in the habit of using standard Java naming conventions!
- 05-25-2011, 02:49 PM #4
Member
- Join Date
- May 2011
- Posts
- 2
- Rep Power
- 0
Similar Threads
-
calling yield() method in synchronized block
By Ash-infinity in forum New To JavaReplies: 2Last Post: 12-04-2012, 05:35 PM -
Strings made by scanner don't work with startsWith() method
By samza in forum New To JavaReplies: 13Last Post: 05-16-2010, 12:09 AM -
what made paintComponent() method to be called twice??
By Y. Progammer in forum New To JavaReplies: 5Last Post: 02-21-2010, 10:19 PM -
[SOLVED] Non-synchronized instance method of an Object
By piyu.sha in forum Threads and SynchronizationReplies: 2Last Post: 10-06-2008, 06:35 AM -
Differences between constructor and method
By Java Tip in forum Java TipReplies: 0Last Post: 12-19-2007, 08:53 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks