Results 1 to 4 of 4
Thread: Thread synchronization
- 01-07-2011, 07:40 AM #1
Member
- Join Date
- Jan 2011
- Posts
- 2
- Rep Power
- 0
- 01-31-2011, 08:35 PM #2
Member
- Join Date
- Jan 2011
- Posts
- 5
- Rep Power
- 0
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.
- 01-31-2011, 10:32 PM #3
Senior Member
- Join Date
- Jan 2009
- Posts
- 671
- Rep Power
- 5
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 10:35 PM.
- 02-18-2011, 12:30 PM #4
Member
- Join Date
- Jan 2011
- Posts
- 1
- Rep Power
- 0
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
-
Thread synchronization
By rajanis in forum Threads and SynchronizationReplies: 0Last Post: 01-07-2011, 07:38 AM -
Thread safe without using synchronization
By swetu.vc in forum Threads and SynchronizationReplies: 3Last Post: 01-20-2010, 08:06 AM -
Threads and Synchronization
By ASADUN in forum Threads and SynchronizationReplies: 4Last Post: 12-18-2009, 07:00 AM -
Animation Synchronization
By dreadrocksean in forum Advanced JavaReplies: 5Last Post: 08-08-2008, 02:56 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks