0% found this document useful (0 votes)
50 views4 pages

Concurrent Transaction Management Tutorial

The document discusses transaction management in concurrent programming. It describes a Fifo class that is intended to be used by multiple producer and consumer threads but contains a concurrency bug. To detect the bug, multiple Thread objects are created that write and read from the Fifo class concurrently. Proper transaction management is needed to avoid issues like lost updates or inconsistent data retrieval that could result from the concurrent access without synchronization.

Uploaded by

Kin Skye
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views4 pages

Concurrent Transaction Management Tutorial

The document discusses transaction management in concurrent programming. It describes a Fifo class that is intended to be used by multiple producer and consumer threads but contains a concurrency bug. To detect the bug, multiple Thread objects are created that write and read from the Fifo class concurrently. Proper transaction management is needed to avoid issues like lost updates or inconsistent data retrieval that could result from the concurrent access without synchronization.

Uploaded by

Kin Skye
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Tutorial 8 Transaction Management

Contents
1

Objectives .................................................................................................................................................... 1

Exercise ........................................................................................................................................................ 1

Fifo class ...................................................................................................................................................... 1

Thread class ................................................................................................................................................. 2

Main method ............................................................................................................................................... 2

How to solve the issue? ............................................................................................................................... 2

Question ...................................................................................................................................................... 3

Tutorial 8 Transaction Management

1 Objectives
In this tutorial we try to run concurrent processes and at the same time Avoid interference between
transactions which means we should not have any lost update and inconsistent retrieval1

2 Exercise
Consider the class Fifo. Assume multiple producers, multiple consumers. There is (at least one) concurrencyrelated bug here. How can it be detected during testing?

3 Fifo class
public class fifo{
private final String fileName = "[Link]";
private final int val;
private int r = 0;
private int w = 0;
public fifo(int val){
[Link] = val;
}
public synchronized int get() throws FileNotFoundException{
Scanner scanner = new Scanner(new File(fileName));
r = 0;
int tempVal = 0;
while([Link]())
{
r++;
tempVal = [Link]();
}
[Link]("Last number is:" + tempVal);
return tempVal;
}
public synchronized void put(int val, String name) throws IOException{
File file=new File(fileName);
FileWriter fw=new FileWriter(file,true);
[Link]([Link](val));
[Link]("\n");
[Link]();
w++;
[Link]("Value: " + val + " is written in thread " + name);
}
}

Thanks to Lund University, Advanced concurrent programming in Java course, [Link]

DECOM Tutorial document

last update August 2014

Tutorial 8 Transaction Management

4 Thread class
public class stmThread extends Thread {
private Thread thread;
private String name;
fifo FIFO;
stmThread(String name) {
[Link] = name;
[Link] = new fifo(0);
}
@Override
public void run() {
synchronized(FIFO)
{
Random rand = new Random();
try {
for (int i = 0; i < 20; i++)
{
int r = [Link](1000);
[Link](r, name);
sleep(r);
}
[Link]();
} catch (IOException ex) {
[Link]([Link]()).log([Link],
null, ex);
} catch (InterruptedException ex) {
[Link]([Link]()).log([Link],
null, ex);
}
[Link]("Thread " + name + " exiting.");
}
}
@Override
public void start() {
[Link]("Starting " + name);
thread = new Thread(this, name);
[Link]();
}
}

5 Main method
public static void main(String[] args) {
stmThread fifoThread1 = new stmThread("Thread 1");
stmThread fifoThread2 = new stmThread("Thread 2");
stmThread fifoThread3 = new stmThread("Thread 3");
[Link]();
[Link]();
[Link]();
}

6 How to solve the issue?

DECOM Tutorial document

last update August 2014

Tutorial 8 Transaction Management

7 Question
1. In which case the Rollback transaction will take place? Please explain.
2. What is the meaning of race condition?

DECOM Tutorial document

last update August 2014

You might also like