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

Code in java do proper comments on each line of code 5. Problem Statement Write

ID: 3869110 • Letter: C

Question

Code in java
do proper comments on each line of code


5. Problem Statement Write a Program in Java to create two methods in a class. .In first method make two synchronized block to lock String class and Integer class. In the second method lock the above two classes in the reverse order. Inside main method creates a thread and call the above methods. This will create a deadlock. i ae a thread and cll the Remove the deadlock from the code 6. Expected Output The expected output should be displayed on the eclipse console. Files to be submitted for verification 1. Eclipse Console Screenshot with the relevant output. 2. Eclipse files in below format

Explanation / Answer

import java.io.*; class Line { // if multiple threads(trains) will try to // access this unsynchronized method, // they all will get it. So there is chance // that Object's state will be corrupted. public void getLine() { for (int i = 0; i < 3; i++) { System.out.println(i); try { Thread.sleep(400); } catch (Exception e) { System.out.println(e); } } } } class Train extends Thread { // reference to Line's Object. Line line; Train(Line line) { this.line = line; } @Override public void run() { line.getLine(); } } class GFG { public static void main(String[] args) { // Object of Line class that is shared // among the threads. Line obj = new Line(); // creating the threads that are // sharing the same Object. Train train1 = new Train(obj); Train train2 = new Train(obj); // threads start their execution. train1.start(); train2.start(); } }