Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

ESC 262 Object oriented Design & Programming II Lab 09 a Java Threads INTRODUCTI

ID: 3819434 • Letter: E

Question

ESC 262 Object oriented Design & Programming II Lab 09 a Java Threads INTRODUCTION The purpose of this laboratory work is to gain the first experience in threads in Java and threads synchronization in Java. For this lab you will use Bluel in Windows environment. Initial variants of the programs are given in a zip file. Download the .zip file and unzip it in jou working directory. Cpen relevant projects for the experiments Starting with a very simple Java program, a student will study its operation in the following modes: (a) a program, consisting of a main and two additional threads that access a shared object (two variants using inheritance and implementing runable interface) and threads using oin method simulate traditionalprogamming a program to synchronize three threads a main, or primary thread and two child threads in case the shared object is definedby the use user implemented objects can be changed to be ready for mutithreaded applications vs. library classes can not be changed) using synchronized methods (c) a program to synchronize three threads a main, or primary thread and two child threads in case the shared object is defined by the user using synchronized regions allows to synchronize on this part of the code, that access the shared resource) (d) a program to synchronize three threads a main, or primary thread and two child threads in case the stared object is defined by others (library using synchronized regions

Explanation / Answer

1. two ways to create threads

a) by extending Thread class

b) by implementing Runnable interface

2. the code in experiment a is not synchronised

as the images shows the threads are created using Thread class and Runnable interface.

to make the function synshronized in java we use synchronized keyword or blocks and none of them is used

3). Advantages for making the code synchronized using experiment b

a) no two or more thread can execute the function at the same time

b) there will be no data inconsistency and same data value will be accessed by multiple threads at the same time

Disadvantages for making the code synchronized using experiment b

a) it makes the execution of the program slow and waititng time for the thread execution will increase

4.

Advantages for making the code synchronized using experiment c

a) the code will make the critical section of the code synchronized so that the multiple thread cannot execute the critical section of the code at the same time

b) the code will be less slow as compared to the way described in experiment b i.e. making the whole method synchronized

disadvantages for making the code synchronized using experiment c

a) waiting time of the thread will be there but will be less as compared to the way descrbed in experiment b

5) experimen 5 also showa that the most critical section i.e. the part of the code where we are updating the value of the object that is being shared by the thread as synchronized rather than making the whole code syncronized so thw waiting time will be less and there will be no data inconsistency.

6) if we want the child thread to complete their execution before main thread ends then we will use join method

suppose there are two child thread t1 and t2

so call t1.join(); t2.join() inside the main()

since all the code inside the main() is executed by the main thread so the main thread will exute the line and will wait till the child thread completes their execution

7) yes two or more thread can access the same routine

8) if two or more thread read the same data then their cannot be any preoblem but the issue arise when two or more thread update the same variable in that case their will be data inconsistencies .

to overcome such proble use synchronized keyword or synchronized blocks