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

Write the program in java and make sure to comment on each line Not applicable.

ID: 3854583 • Letter: W

Question

Write the program in java and make sure to comment on each line


Not applicable. 4. Associated Data Files 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 . Remove the deadlock from the code 6. Expected Output The expected output should be displayed on the eclipse console

Explanation / Answer

public class LockStyle {

public void method1(String o1,Integer o2) //method accept string and int

{

synchronized(o1)

{

System.out.println(o1); //print string

}

synchronized(o2)

{

System.out.println(o2);//print int

}

}

public void method2(String o1,Integer o2)//method accept string and int

{

synchronized(o2)

{

System.out.println(o2);//print int

}

synchronized(o1)

{

System.out.println(o1);//print string

}

}

public static void main(String[] args) {

LockStyle so=new LockStyle();//create object of classs

Thread t = new Thread(new Runnable() {//create thread

@Override

public void run() {

// Insert some method call here.

String s=new String("akshay");

Integer i=new Integer(123);

so.method1(s,i);

so.method2(s,i);

}

});

t.start();

}

}

==========================================

akshay
123
123
akshay