Sie sind auf Seite 1von 4

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 = "fifo.txt";
private final int val;
private int r = 0;
private int w = 0;
public fifo(int val){
this.val = val;
}
public synchronized int get() throws FileNotFoundException{
Scanner scanner = new Scanner(new File(fileName));
r = 0;
int tempVal = 0;
while(scanner.hasNextInt())
{
r++;
tempVal = scanner.nextInt();
}
System.out.println("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);
fw.append(String.valueOf(val));
fw.append("\n");
fw.close();
w++;
System.out.println("Value: " + val + " is written in thread " + name);
}
}

Thanks to Lund University, Advanced concurrent programming in Java course, http://cs.lth.se/english/sde/phdcourses/advanced-concurrent-programming-in-java/

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) {
this.name = name;
this.FIFO = new fifo(0);
}
@Override
public void run() {
synchronized(FIFO)
{
Random rand = new Random();
try {
for (int i = 0; i < 20; i++)
{
int r = rand.nextInt(1000);
FIFO.put(r, name);
sleep(r);
}
FIFO.get();
} catch (IOException ex) {
Logger.getLogger(stmThread.class.getName()).log(Level.SEVERE,
null, ex);
} catch (InterruptedException ex) {
Logger.getLogger(stmThread.class.getName()).log(Level.SEVERE,
null, ex);
}
System.out.println("Thread " + name + " exiting.");
}
}
@Override
public void start() {
System.out.println("Starting " + name);
thread = new Thread(this, name);
thread.start();
}
}

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");
fifoThread1.start();
fifoThread2.start();
fifoThread3.start();
}

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

Das könnte Ihnen auch gefallen