Tharun Tej
unread,Oct 31, 2011, 7:41:28 PM10/31/11Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to Deuce-STM, thar...@gmail.com
Hi,
I have been running one of the program using Deuce.
The program is as follows.
/
*****************************************************************************************************************************************************************************************************************/
import java.io.IOException;
import java.util.logging.FileHandler;
import java.util.logging.Logger;
import org.deuce.Atomic;
class Account
{
int checking, savings;
public Account(int i ,int j)
{
checking = i; savings = j;
}
@Atomic
void transfer(int n)
{
System.out.println("Checkings before Transfer:"+checking);
checking += n;
System.out.println("Checkings After Transfer:"+checking);
System.out.println("Counter before first call
is :"+Global.opCounter);
Global.inc();
System.out.println("Counter after first call
is :"+Global.opCounter);
System.out.println("Savings before :"+savings);
savings -= n;
System.out.println("Savings after : "+savings);
System.out.println("Counter before Second call
is :"+Global.opCounter);
Global.inc();
System.out.println("Counter after Second call
is :"+Global.opCounter);
}
}
class Global
{
static int opCounter = 0;
@Atomic
static void inc()
{
opCounter++;
}
}
public class Test
{
public static void main(String args[]) throws SecurityException,
IOException
{
final Account x = new Account(4,7);
Thread T1 = new Thread()
{
public void run()
{
x.transfer(2);
}
};
Thread T2 = new Thread()
{
public void run()
{
Global.inc();
System.out.println("Thread 2 incremented Counter::"+
Global.opCounter);
}
};
T1.setName(" Transaction");
T1.start();
T2.setName(" Opcounter");
T2.start();
System.out.println("Finally:"+Global.opCounter);
}
}
/
****************************************************************************************************************************************************************************************************************/
Now when I run the program it should ensure that it runs atomically,
means when Thread 1 is performing some operation it should not be
disturbed by any of the other operations.
For example in the Thread 1 we are transferring the money from savings
account to current account and incrementing the global opcounter
simultaneously in the transfer method.
There is other thread called T2 which call the incrementing opcounter
method.
The Question is : when the Thread 1 is performing operations until if
finishes the operations T2 should not enter. Means the flow of the
execution should be T1, T2 or T2, T1 but not T1,T2,T1.
Here are the various outputs (Schedules )that I got.
First is the correct output schedule.
C:\Eclipse3.5 Workspace\AtomicSetSerializabilit_Deuce\src>java -
javaagent:deuceAgent-1.3.0.jar Test
Finally:0
Thread 2 incremented Counter::1
Checkings before Transfer:4
Checkings After Transfer:6
Counter before first call is :1
Counter after first call is :2
Savings before :7
Savings after : 5
Counter before Second call is :2
Counter after Second call is :3
C:\Eclipse3.5 Workspace\AtomicSetSerializabilit_Deuce\src>
/
************************************************************************************************************************************************************************/
CONFLICT Schedule::
C:\Eclipse3.5 Workspace\AtomicSetSerializabilit_Deuce\src>java -
javaagent:deuceAgent-1.3.0.jar Test
Finally:0
Checkings before Transfer:4
Checkings After Transfer:6
Counter before first call is :1
Thread 2 incremented Counter::1
-----------------------------------------------------> Why is this
coming here? How are you resolving this.
Counter after first call is :2
Savings before :7
Savings after : 5
Counter before Second call is :2
Counter after Second call is :3
/
************************************************************************************************************************************************************************************/
So in the above conflict schedule T2 is coming in the middle of T1. so
how are you handling this . This should not be the case.
If you find any difficulties in understanding the problem please let
me know.
Thank you.