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

    Default Thread synchronization

    if a class has synchronized methods and nonsynchronized methods then that object of the class can be Threadsafe or not ?

  2. #2
    golan2 is offline Member
    Join Date
    Jan 2011
    Posts
    5
    Rep Power
    0

    Default

    I am not an expert in threads but I see none answers so I will give you my opinion as “something to think about”.
    IMO the object CAN be thread-safe but it depends which methods.
    Saying thread-safe means that the STATE of the object is “thread-safe”.
    Example:
    I will write a class “MyCollection” which holds values.
    The methods that add/remove items to the collection must be synchronized.
    A method for checking if an item exist in the collection or not may be non-synchronized.
    You see now MyCollection is thread-safe because in order to change it you must have the monitor.
    But if you only read from it then you might not need the monitor.
    Many can read from it simultaneously and with no harm.

  3. #3
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Thumbs down

    Quote Originally Posted by rajanis View Post
    if a class has synchronized methods and nonsynchronized methods then that object of the class can be Threadsafe or not ?
    It might or might not be thread safe. A class is threadsafe if it has been coded in such a way that different threads using the same object can not cause a deadlock, monitor exceptions, or an undefined state. For example, the following is threadsafe even thoughit has a static member, and even though it has an unsynchornized method.

    Java Code:
    private static final Object myObject = new Object();
    
    private volatile Vector<Object> objVec = new Vector<Object>();
    
    public Object getA() {
      return myObject;
    }
    
    public synchronized addObject(Object o) {
      objVec.add(o);
    }
    
    public synchronized removeObject(Object o) {
      objVec.remove(o);
    }
    Last edited by toadaly; 01-31-2011 at 11:35 PM.

  4. #4
    rajender99 is offline Member
    Join Date
    Jan 2011
    Posts
    1
    Rep Power
    0

    Default

    here is one sample example for thread sync....

    class Counter {

    private long balance = 600;
    private Object lock1 = new Object();

    Counter()
    {
    System.out.println("initial balance in A/c ==> "+600);
    }

    public /*synchronized*/ void withdraw( int amount) // synchronized method: just removed the comment /*synchronized*/ it will be synchronized method
    {

    int drawamount = amount;

    synchronized(lock1) // synchronized statement
    {
    if(balance<=500)
    {
    System.out.println("Error :: Balance is low for ...." + Thread.currentThread().getName()); //raj
    return;
    }
    else
    {
    try
    {
    System.out.println("withdrawl is under processing............ ..wait..." + Thread.currentThread().getName());
    Thread.sleep(10000);
    balance = balance - amount;
    Thread.sleep(10000);
    System.out.println("withdrawl completed............ ..sucessfully." + Thread.currentThread().getName());
    }
    catch (Exception e) { }
    }
    }
    }

    public synchronized void deposit()
    {
    System.out.println(" Deposit");
    }

    public long getbalance()
    {
    return balance;
    }

    } //counter class

    // ************************************************** *******
    public class Main_class
    {

    public static void main(String[] args)
    {

    final Counter cc1 = new Counter();

    new Thread(new Runnable()
    {
    public void run() {

    //System.out.println("thread 1 started ----------------------------" +Thread.currentThread().getName()) ;

    if(cc1.getbalance()>=500)
    {
    System.out.println("1- sufficient balalnce"); //raj
    cc1.withdraw(150);
    }
    System.out.println("Customer 1 - current balance " +cc1.getbalance());}

    }).start();


    new Thread(new Runnable()
    {
    public void run() {

    //System.out.println("thread 2 started -------------------------- " +Thread.currentThread().getName());

    if(cc1.getbalance()>=500)
    {
    System.out.println("2- sufficient balalnce"); //raj
    cc1.withdraw(200);
    }

    System.out.println("Customer 2 - current balance " +cc1.getbalance()); }

    }).start();


    }


    } // Main_class //rajender.goud@gmail.com

    /*
    synchronized methods and synchronized Statements
    --------------------------------------------------------------

    it is not possible for two invocations of synchronized methods on the same object to interleave.
    When one thread is executing a synchronized method for an object,
    all other threads that invoke synchronized methods for the same object block (suspend execution)
    until the first thread is done with the object.


    when a synchronized method exits, it automatically establishes a happens-before relationship with
    any subsequent invocation of a synchronized method for the same object.
    This guarantees that changes to the state of the object are visible to all threads


    with synchronised statement : output: current balance 450

    initial balance in A/c ==> 600
    1- sufficient balalnce
    withdrawl is under processing............ ..wait...Thread-0
    2- sufficient balalnce
    withdrawl completed............ ..sucessfully.Thread-0
    Error :: Balance is low for ....Thread-1
    Customer 1 - current balance 450
    Customer 2 - current balance 450


    Two started started simultaneosly and get the current balance as sufficient ...
    so Threas -0 enters withdrawl and locaks up....and starts withdrawing......
    Thread -1 cant enter into withdrawl because thread-1 is acessing
    transaction completes sucessfully for thread -0 and exist....
    now Thread -1 enters withdrawl and exist with Error :: Balance is low for ....Thread-1


    now comment synchronized(lock1) and see the output: balance 250 which is inconsistancy

    initial balance in A/c ==> 600
    1- sufficient balalnce
    withdrawl is under processing............ ..wait...Thread-0
    2- sufficient balalnce
    withdrawl is under processing............ ..wait...Thread-1
    withdrawl completed............ ..sucessfully.Thread-0
    withdrawl completed............ ..sucessfully.Thread-1
    Customer 1 - current balance 250
    Customer 2 - current balance 250


    */

Similar Threads

  1. Thread synchronization
    By rajanis in forum Threads and Synchronization
    Replies: 0
    Last Post: 01-07-2011, 08:38 AM
  2. Thread safe without using synchronization
    By swetu.vc in forum Threads and Synchronization
    Replies: 3
    Last Post: 01-20-2010, 09:06 AM
  3. Threads and Synchronization
    By ASADUN in forum Threads and Synchronization
    Replies: 4
    Last Post: 12-18-2009, 08:00 AM
  4. Animation Synchronization
    By dreadrocksean in forum Advanced Java
    Replies: 5
    Last Post: 08-08-2008, 03:56 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
  •