Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Lab 10: Student/School Threads Synchronization, Lab Reports of Computer Science

A lab exercise focused on working with java threads, specifically creating a producer and consumer thread for generating and consuming student objects. The exercise is repeated in two situations: non-synchronized and synchronized. The results are displayed on a jtextarea attached to a jinternalframe.

Typology: Lab Reports

Pre 2010

Uploaded on 08/18/2009

koofers-user-znu
koofers-user-znu 🇺🇸

10 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
LAB # 10
PRE LAB
In this lab we are going to work with Threads. The Lab consists of generating a producer thread
and one or more consumer threads. The code is again an extension of your previous labs code
thus expanding on the GUI. Your producer thread is a thread that produces a student object
with a particular id (including the other attributes as in previous labs). The object is stored in a
shared memory location and then it can be consumed by a School thread.
The lab will be repeated for two different situations. In one situation the threads are non
synchronized and in the second situation the threads are synchronized. In both case the data
should be displayed on a JText Area attached to a JInternalFrame.
In the Pre-Lab do the following:
1. Append the JMenuBar in your GUI to include a new JMenu titled “Student/School
Threads”. Attach two JMenuItems : 1. Non-Synchronized 2. Synchronized
2. Answer the following question:
What is the meaning of synchronization?
1
pf3

Partial preview of the text

Download Lab 10: Student/School Threads Synchronization and more Lab Reports Computer Science in PDF only on Docsity!

LAB # 10

PRE LAB

In this lab we are going to work with Threads. The Lab consists of generating a producer thread and one or more consumer threads. The code is again an extension of your previous labs code thus expanding on the GUI. Your producer thread is a thread that produces a student object with a particular id (including the other attributes as in previous labs). The object is stored in a shared memory location and then it can be consumed by a School thread. The lab will be repeated for two different situations. In one situation the threads are non synchronized and in the second situation the threads are synchronized. In both case the data should be displayed on a JText Area attached to a JInternalFrame. In the Pre-Lab do the following:

  1. Append the JMenuBar in your GUI to include a new JMenu titled “Student/School Threads”. Attach two JMenuItems : 1. Non-Synchronized 2. Synchronized
  2. Answer the following question: What is the meaning of synchronization?

IN-LAB

Create the following event handlers for the menu items created in the Pre-Lab section:

  1. When the “Non-Synchronized” menu item is clicked a JInternal Frame is created on JDesktopPane. The JInternalFrame has a JTextArea and JButon titled “Start”. When the start button is pressed a Student thread is created that creates ten Student objects with a sequenced ids 1-10. Every time a Student object is created it is stored in a shared class by the consumer School Thread. When a new Student object is generated it overrides the previous object in the shared class (one one object at a time can exist). The attributes of the generated object are printed out on the text area as follows : “Producer Thread: I have produced a Student with First Name ……., Last Name…. .., ID=…..” one line at a time for each object produced. At the same time that a producer thread is spawn a School consumer thread is also generated. The job of the consumer thread is to read off the shared class the Student object that the producer thread produced. As soon as the consumer thread acquired the data it displays the data on the same text area on a line below the producer thread (notice that it is possible that the consumer thread tried to read the shared object first before the producer produced anything in which case it should read null values). The data read by the consumer thread is displayed as follows: “Consumer Thread: I have consumed a Student with First Name ….., Last Name….., ID=…… “ one line at a time for each object read. THE TWO THREADS MUST USE NON_SYNCHRONIZED METHODS in this case. The results displayed could be as follows due to the fact that the two threads use non synchronized method son the shared object class: Producer Thread: I have produced a Student with First Name John1, Last Name Doe1, ID= Producer Thread: I have produced a Student with First Name John2, Last Name Doe2, ID= Consumer Thread: I have consumed a Student with First Name John2, Last Name Doe2, ID= Producer Thread: I have produced a Student with First Name John3, Last Name Doe3, ID= Consumer Thread: I have consumed a Student with First Name John3, Last Name Doe3, ID= Consumer Thread: I have consumed a Student with First Name John3, Last Name Doe3, ID=