From 4b6a4b9ac7e8d475199b8512dc6e7bcc5cce9938 Mon Sep 17 00:00:00 2001 From: MrAkashKumar Date: Thu, 22 Oct 2020 12:57:49 +0530 Subject: [PATCH] design pattern program for thread --- .../RunnerThreadEnviroment.java | 15 ++++++ .../SingletonThreadEnviroment.java | 19 +++++++ .../NonRepeatCharFromString.java | 49 +++++++++++++++++++ 3 files changed, 83 insertions(+) create mode 100644 DesignPattern/src/com/akash/singletonforThread/RunnerThreadEnviroment.java create mode 100644 DesignPattern/src/com/akash/singletonforThread/SingletonThreadEnviroment.java create mode 100644 Program/src/com/akash/latestProgram/NonRepeatCharFromString.java diff --git a/DesignPattern/src/com/akash/singletonforThread/RunnerThreadEnviroment.java b/DesignPattern/src/com/akash/singletonforThread/RunnerThreadEnviroment.java new file mode 100644 index 0000000..d8bd068 --- /dev/null +++ b/DesignPattern/src/com/akash/singletonforThread/RunnerThreadEnviroment.java @@ -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); + } + +} diff --git a/DesignPattern/src/com/akash/singletonforThread/SingletonThreadEnviroment.java b/DesignPattern/src/com/akash/singletonforThread/SingletonThreadEnviroment.java new file mode 100644 index 0000000..51ee29f --- /dev/null +++ b/DesignPattern/src/com/akash/singletonforThread/SingletonThreadEnviroment.java @@ -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; + } +} diff --git a/Program/src/com/akash/latestProgram/NonRepeatCharFromString.java b/Program/src/com/akash/latestProgram/NonRepeatCharFromString.java new file mode 100644 index 0000000..ec1433a --- /dev/null +++ b/Program/src/com/akash/latestProgram/NonRepeatCharFromString.java @@ -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 map = new HashMap<>(); + + for(int i = 0; i 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; + } + } + } +}