0% found this document useful (0 votes)
39 views

Reverese A Number: // Program Uses Class Scanner

This document demonstrates how deadlock can occur between two threads that are trying to acquire locks for the same two resources in the opposite order. Thread 1 tries to lock resource 1 then resource 2, while thread 2 tries to lock resource 2 then resource 1. This causes each thread to wait indefinitely for the other thread to release its lock, resulting in deadlock where neither thread can continue. To avoid deadlock, all threads must always acquire locks in the same order.

Uploaded by

subhjit
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views

Reverese A Number: // Program Uses Class Scanner

This document demonstrates how deadlock can occur between two threads that are trying to acquire locks for the same two resources in the opposite order. Thread 1 tries to lock resource 1 then resource 2, while thread 2 tries to lock resource 2 then resource 1. This causes each thread to wait indefinitely for the other thread to release its lock, resulting in deadlock where neither thread can continue. To avoid deadlock, all threads must always acquire locks in the same order.

Uploaded by

subhjit
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

1.

Reverese a number
package project.test; import java.util.Scanner; // program uses class Scanner public class ReverseDigits { static Scanner console = new Scanner(System.in); public static void main( String args[]) { String number; System.out.println("Enter an integer"); number = console.nextLine(); reverse (number); // print the method reverse } // end method main public static void reverse ( String num ) { int lastDigit; // the last digit returned when reversed int reverse = 0; for(int i = num.length()-1; i >= 0; i--) { System.out.print(num.charAt(i)); } System.out.println(" is integer with its digits reversed."); int res=Integer.parseInt(num); System.out.println("Result ="+res); } } Shortest: new StringBuilder(input + "").reverse().toString()

2. Deadlock
package project.test; /** * This is a demonstration of how NOT to write multi-threaded programs. * It is a program that purposely causes deadlock between two threads that * are both trying to acquire locks for the same two resources.

* To avoid this sort of deadlock when locking multiple resources, all threads * should always acquire their locks in the same order. **/ public class Deadlock { public static void main(String[ ] args) { // These are the two resource objects we'll try to get locks for final Object resource1 = "resource1"; final Object resource2 = "resource2"; // Here's the first thread. It tries to lock resource1 then resource2 Thread t1 = new Thread( ) { public void run( ) { // Lock resource 1 synchronized(resource1) { System.out.println("Thread 1: locked resource 1"); // Pause for a bit, simulating some file I/O or // something. Basically, we just want to give the // other thread a chance to run. Threads and deadlock // are asynchronous things, but we're trying to force // deadlock to happen here... try { Thread.sleep(50); } catch (InterruptedException e) { } // Now wait 'till we can get a lock on resource 2 synchronized(resource2) { System.out.println("Thread 1: locked resource 2"); }

} }; }

// Here's the second thread. It tries to lock resource2 then resource1 Thread t2 = new Thread( ) { public void run( ) { // This thread locks resource 2 right away synchronized(resource2) { System.out.println("Thread 2: locked resource 2"); // Then it pauses, just like the first thread. try { Thread.sleep(50); } catch (InterruptedException e) { } // Then it tries to lock resource1. But wait! Thread // 1 locked resource1, and won't release it 'till it // gets a lock on resource2. This thread holds the // lock on resource2, and won't release it 'till it // gets resource1. We're at an impasse. Neither // thread can run, and the program freezes up. synchronized(resource1) { System.out.println("Thread 2: locked resource 1"); }

} }; }

// Start the two threads. If all goes as planned, deadlock will occur, // and the program will never exit.

t1.start( ); t2.start( ); } }

You might also like