-1

My question is seems similar to other question in stack over flow,but I am not able to understand that when have more than one method in the class I put both the methods in synchronized block and I am trying to run the both methods is different threads but here the methods are running one after the other.

Main Class

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Login.Package;

import java.util.ArrayList;
import java.util.List;
import org.hibernate.Session;

/**
 *
 * @author ranjeeth.g
 */
public class TransportDataTransper {

    public static void main(String[] args) {
        Account a = new Account();
        Thread t = new Thread(new WithDraw(a));
        t.start();
        Thread b = new Thread(new BalanceChecfk(a));
        b.start();
//        Thread t1 = new Thread(new WithDraw(a));
//        t1.start();

//        t.start();
//        t.start();
    }

}

Account Class.

public class Account {

    public double withDr() {
        synchronized (this) {
            System.out.println("with draw check method = ");
            for (int i = 0; i < 50; i++) {
                System.out.println("i = " + i);
            }
            return 0;
        }
    }

    public double balacheck() {
        synchronized (this) {
            System.out.println("Balance check method = ");
            for (int i = 0; i < 50; i++) {
                System.out.println("i = " + i);
            }
            return 0;
        }
    }
//    public synchronized double deposit(double amount) {
//        return 0;
//    }

}
user3108190
  • 27
  • 1
  • 2
  • 10
  • 2
    If you're asking if there's a difference between a `synchronized` method and the method code wrapped in `synchronized (this)`, no. – Bohemian Jul 23 '14 at 06:08
  • possible duplicate of [synchronized block vs synchronized method?](http://stackoverflow.com/questions/574240/synchronized-block-vs-synchronized-method) – Seelenvirtuose Jul 23 '14 at 06:10

3 Answers3

1

this is the same Account object a which is being used to lock

so only one thread can lock it at a time

Kalpesh Soni
  • 6,879
  • 2
  • 56
  • 59
1

To fully answer your question, you will need to include the "WithDraw" and "BalanceChecfk" classes. However, the "Account" class uses synchronized blocks based on an instance monitor, so if the WithDraw and BalanceChecfk classes use the account object being passed into them to delegate to the methods in Account, then whichever one happens to start first will complete first followed by the other - just as you observe. If you add a sleep of two seconds to the Account.withDr method before the synchronize, you will likely see BalanceChecfk being executed first followed by WithDraw.

Curiously, there are no shared resources which need protection in your Account object implementation, so technically there is no need at all for the synchronization, as currently implemented. If, however, you added

private Long penniesInMyAccount

to the Account object, then you would need to properly synchronize access to this shared resource from these methods. There are a number of ways to do this correctly and efficiently.

As a complete aside:

  1. name your classes and methods clearly;
  2. don't use abbreviations; and,
  3. use proper grammar and spelling.

Hope that helps.

Remember, "Be kind to your future self." --Hansen

Jan Nielsen
  • 10,892
  • 14
  • 65
  • 119
0

There is no difference, however, synchronized blocks have the flexibility to lock on different objects, as well as to synchronize a block of code (as their name implies) rather than an entire method.