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

Edit View History Bookmarks People Window Help x G strategy pattern Java bean cl

ID: 3841943 • Letter: E

Question

Edit View History Bookmarks People Window Help x G strategy pattern Java bean cla x I Exam Mail M Gmail Google Docs an LinkedIn heBay o Soundcloud YouTube sc c canvas public class stubborn inplements Runnable t 'static Thread t1; static int x Si public void runo t1.getldCD) shoveX): currentThreadX) getrdio else pusho static synchronized void push() shoveO; static void shoveO system out printo H try Thread sleep(zaee catch (Exception e) public static void main(StringO args) ti nen Thread new Stubborn t1.starto new Thread(new StubbornO start() which are true? o the seepo invocation was removed, the chance of deadlock would decrease. o The program could deadlock The output could be 5,followed by deadlock. o The output is 543210 The output is 54321

Explanation / Answer

public class Stubborn implements Runnable {
static Thread t1;
static int x=5;
public void run(){
if(Thread.currentThread().getId() == t1.getId()) shove();
else push();
}
static synchronized void push() { shove();}
static void shove() {
synchronized(Stubborn.class){
System.out.print(x == + " ");
try { Thread.sleep(2000); } catch (Exception e) { ; }
if(x > 0) push();
}
}
public static void main(String[] args) {
t1 = new Thread(new Stubborn());
t1.start();
new Thread(new Stubborn()).start();
}
}

output:

Ans: The output could be 5,followed by deadlock.

Explanation: Deadlock is a situation where more than one threads are blocked forever, and waiting for each other. It occures when multiple threads need the same locks.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote