diff --git a/Deadlock.java b/Deadlock.java new file mode 100644 index 0000000..122c755 --- /dev/null +++ b/Deadlock.java @@ -0,0 +1,65 @@ + + +/*Java program to demonstrate a solution for Deadlock in Multi-threading */ + +public class Deadlock { + + public static Object Resource1 = new Object(); /* Creating 2 objects in order to acquire lock */ + public static Object Resource2 = new Object(); + + public static void main(String args[]) { + + ThreadDemo1 T1 = new ThreadDemo1(); /* Creating 2 thread objects */ + ThreadDemo2 T2 = new ThreadDemo2(); + + T1.start(); + T2.start(); + } + + private static class ThreadDemo1 extends Thread { /* Illustration of thread by extending Thread class*/ + + public void run() { + synchronized (Resource1) { + System.out.println("Thread 1: Holding Resource 1..."); /* Thread 1 acquiring lock on Resource 1 */ + + try { + Thread.sleep(10); + } catch (InterruptedException e) { + } + System.out.println( + "Thread 1: Waiting for Resource 2..."); /* + * Thread 1 in waiting state while it is trying to + * acquire Resource 2 but already locked + */ + + synchronized (Resource2) { + System.out + .println("Thread 1: Holding Resource 1 & 2..."); /* Thread 1 acquiring lock on Resource 2 */ + } + } + } + } + + private static class ThreadDemo2 extends Thread { /* Illustration of thread by extending Thread class*/ + public void run() { + synchronized (Resource1) { + System.out.println("Thread 2: Holding Resource 1..."); /* Thread 2 acquiring lock on Resource 1 */ + + try { + Thread.sleep(10); + } catch (InterruptedException e) { + } + System.out.println( + "Thread 2: Waiting for Resource 2..."); /* + * Thread 2 in waiting state while it is trying to + * acquire Resource 2 but already locked + */ + + synchronized (Resource2) { + System.out + .println("Thread 2: Holding Resource 1 & 2..."); /* Thread 2 acquiring lock on Resource 2 */ + } + } + } + } +} diff --git a/Permutations.java b/Permutations.java new file mode 100644 index 0000000..e01b5be --- /dev/null +++ b/Permutations.java @@ -0,0 +1,56 @@ + +/* Java program to obtain permutations of a given string */ + +public class Permutations { + + /* + * Function for swapping the characters at position i with character at position + * j + */ + + public static String swapString(String s, int i, int j) { + + char[] b = s.toCharArray(); /* conversion of the string char array to perform swapping */ + + char ch; + ch = b[i]; + b[i] = b[j]; + b[j] = ch; + return String.valueOf(b); + } + + /* Function for generating different permutations of the string */ + + public static void generatePermutation(String str, int start, int end) { + + /* Prints the permutations */ + + if (start == end - 1) + System.out.println(str); + else { + for (int i = start; i < end; i++) { + + /* Swapping the string by fixing a character */ + str = swapString(str, start, i); + + /* + * Recursively calling function generatePermutation() for rest of the characters + */ + generatePermutation(str, start + 1, end); + + /* Backtracking and swapping the characters again. */ + str = swapString(str, start, i); + } + } + } + + public static void main(String[] args) { + + String str = "BAD"; + int len = str.length(); /* Find the length of the given string */ + + System.out.println("All the permutations of the string are: "); + generatePermutation(str, 0, len); + } + +}