Outvalue value read value state idlemode notifyall

This preview shows 2 out of 6 pages.

//We've been interrupted: no more messages. } out_value = value; //read value state = IDLE_MODE; notifyAll(); //wake up all threads waiting for state to change return (out_value); } /* write operation synchronized - mutual exclusive */ public synchronized void op_write(int i_value) { /* op state action ------------------------------- WRITE IDLE -> process WRITE READ -> block WRITE WRITE -> block */ while (state != IDLE_MODE){ try { wait(); }catch (InterruptedException e){ } } /* stimulate write process time */ state = WRITE_MODE; // added by zim. try { Thread.sleep(WRITE_TIME); }catch (InterruptedException e){ //We've been interrupted: no more messages. }
Image of page 2

Subscribe to view the full document.

value = i_value; //write value state = IDLE_MODE; notifyAll(); //wake up all threads waiting for state to change } /* get the primary key value */ public int get_KeyId(){ return (key_id); } }
Image of page 3
Program 2: readTrans public class readTrans extends Thread{ private int tran_id; //transaction id private dataRec my_data; //which data to read private int value; //the new value of my_data /* constructor */ public readTrans(int i_tranId, dataRec data){ my_data = data; tran_id = i_tranId; } /* perform read operation of my_data */ public void run(){ value = my_data.op_read(); System.out.println("trans [" + tran_id + "] | key_id = " + my_data.get_KeyId() + " reads : " + value); } }
Image of page 4

Subscribe to view the full document.

Program 3: writeTrans public class writeTrans extends Thread{ private int tran_id; //transaction id private dataRec my_data; //which data to read private int value; //the new value of my_data /* constructor */ public writeTrans(int i_tranId, dataRec data, int i_value){ my_data = data; tran_id = i_tranId; value = i_value; } /* perform read operation of my_data */ public void run(){ my_data.op_write(value); System.out.println("trans [" + tran_id + "] | key_id = " + my_data.get_KeyId() + " writes : " + value); } }
Image of page 5
Program 4: ReadWriteTest /* Test the class readTrans and writeTrans */ public class ReadWriteTest { public static void main(String[] args) { dataRec data = new dataRec(1, 0); //create a single dataRec for (int i = 0; i < 10; i++){ //generate read transaction id = 0, 1, .., 9 readTrans r1 = new readTrans(i, data); //generate write transaction id = 100, 101, ... ,109 writeTrans w1 = new writeTrans(i+100, data, 1000 + 10 * i); r1.start(); w1.start(); } } }
Image of page 6
You've reached the end of this preview.
  • Spring '10
  • KYLam
  • Central processing unit, Computer program, dataRec

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern