Skip to content

Commit

Permalink
design pattern program for thread
Browse files Browse the repository at this point in the history
  • Loading branch information
MrAkashKumar committed Oct 22, 2020
1 parent 5746a43 commit 4b6a4b9
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.akash.singletonforThread;

public class RunnerThreadEnviroment {

public static void main(String[] args) {
// TODO

/* Calling the thread singleton */

SingletonThreadEnviroment singleton = SingletonThreadEnviroment.getInstance();

System.out.println(singleton);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.akash.singletonforThread;

public class SingletonThreadEnviroment {

private static volatile SingletonThreadEnviroment singletonInstance = null;

private SingletonThreadEnviroment() {}

public static SingletonThreadEnviroment getInstance() {
if(singletonInstance == null) {
synchronized(SingletonThreadEnviroment.class) {
if(singletonInstance == null) {
singletonInstance = new SingletonThreadEnviroment();
}
}
}
return singletonInstance;
}
}
49 changes: 49 additions & 0 deletions Program/src/com/akash/latestProgram/NonRepeatCharFromString.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.akash.latestProgram;

import java.util.HashMap;
import java.util.Map;

public class NonRepeatCharFromString {

public static void main(String[] args) {
firstNonRepeatedChar();
nonRepeatedChar();
}

private static void nonRepeatedChar() {
// TODO

String str = "akashkumar";

HashMap<Character, Integer> map = new HashMap<>();

for(int i = 0; i<str.length(); i++) {

char ch = str.charAt(i);

if(map.containsKey(ch)) {
int count = map.get(ch);
map.put(ch, count+1);

}else {
map.put(ch, 1);
}
}

for(Map.Entry<Character, Integer> entry : map.entrySet()) {
if(entry.getValue()!= 1)
System.out.print(entry.getKey()+"");
}
}

private static void firstNonRepeatedChar() {
// TODO
String s = "java";
for(Character ch:s.toCharArray()) {
if(s.indexOf(ch) == s.lastIndexOf(ch)) {
System.out.println("First non repeat character = " + ch);
break;
}
}
}
}

0 comments on commit 4b6a4b9

Please sign in to comment.