Question

I'm confused about Synchronized Methods. Is like that threads,or synchronized methods have special properties then different from threads? I read that code segment and I think synchronized mean thread. Can someone explain the difference between thread with synchronized Methods? Of course,if they have difference.

synchronized method = monitor:
    class Account {
        private double balance;
        public Account (double initialDeposit) {
            balance = initialDeposit;
        }
        public synchronized double getBalance() {
            return balance;
        }
        public synchronized void deposit(double amount) {
            balance += amount;
        }
        public synchronized void withdraw(double amount) {
            balance -= amount;
        }
        }
Was it helpful?

Solution 2

synchronized method means a method, to enter which thread has to obtain monitor lock for this object, where method belongs to, for instance methods, and .class object for static methods.

synchronized block is actually the same except for the point that you specify the object which monitor needs to be acquired explicitly and you can specify block boundaries as you wish;

Thread (very basically) is a sequence of instructions that can be executed in parallel with other threads;

So the answer to your question is no, synchronized method and thread are different, although often going together concepts.

OTHER TIPS

No. Synchronization is a technique to tackle thread interference and memory consistency errorstwo issues related to communication between various threads.

In the code you provided only one thread a time can use getBalance(), withdraw() and deposit() methods.

Highly suggest you read: http://docs.oracle.com/javase/tutorial/essential/concurrency/index.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top