Skip to content

Commit b657619

Browse files
committed
19/08/2018
1 parent fe596fd commit b657619

15 files changed

+370
-17
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.in28munites.ConcurrencyLocksAtomicityAndCollections;
2+
3+
public class BiCounter {
4+
private int i = 0;
5+
private int j = 0;
6+
7+
synchronized public void incrementI() {
8+
i++;
9+
//get i
10+
//increment
11+
//set i
12+
}
13+
14+
public int getI() {
15+
return i;
16+
}
17+
18+
synchronized public void incrementJ() {
19+
j++;
20+
//get j
21+
//increment
22+
//set j
23+
}
24+
25+
public int getJ() {
26+
return j;
27+
}
28+
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.in28munites.ConcurrencyLocksAtomicityAndCollections;
2+
3+
import java.util.concurrent.atomic.AtomicInteger;
4+
5+
public class BiCounterWithAtomicInteger {
6+
private AtomicInteger i = new AtomicInteger();
7+
private AtomicInteger j = new AtomicInteger();
8+
9+
public void incrementI() {
10+
i.incrementAndGet();
11+
}
12+
13+
public int getI() {
14+
return i.get();
15+
}
16+
17+
public void incrementJ() {
18+
j.incrementAndGet();
19+
}
20+
21+
public int getJ() {
22+
return j.get();
23+
}
24+
25+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.in28munites.ConcurrencyLocksAtomicityAndCollections;
2+
3+
import java.util.concurrent.locks.Lock;
4+
import java.util.concurrent.locks.ReentrantLock;
5+
6+
public class BiCounterWithLock {
7+
private int i = 0;
8+
private int j = 0;
9+
10+
Lock lockForI = new ReentrantLock();
11+
Lock lockForJ = new ReentrantLock();
12+
13+
public void incrementI() {
14+
lockForI.lock();//Get Lock For I
15+
i++;
16+
lockForI.unlock();
17+
//Release Lock For I
18+
}
19+
20+
public int getI() {
21+
return i;
22+
}
23+
24+
public void incrementJ() {
25+
lockForJ.lock();//Get Lock For J
26+
j++;
27+
lockForJ.unlock();//Release Lock For J
28+
}
29+
30+
public int getJ() {
31+
return j;
32+
}
33+
34+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.in28munites.ConcurrencyLocksAtomicityAndCollections;
2+
3+
public class ConcurrencyRunner {
4+
5+
public static void main(String[] args) {
6+
Counter counter = new Counter();
7+
counter.increment();
8+
counter.increment();
9+
counter.increment();
10+
System.out.println(counter.getI());
11+
12+
}
13+
14+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.in28munites.ConcurrencyLocksAtomicityAndCollections;
2+
3+
import java.util.Hashtable;
4+
import java.util.Map;
5+
import java.util.concurrent.ConcurrentHashMap;
6+
import java.util.concurrent.ConcurrentMap;
7+
import java.util.concurrent.atomic.LongAdder;
8+
9+
public class ConcurrentMapRunner {
10+
11+
public static void main(String[] args) {
12+
ConcurrentMap<Character, LongAdder> occurances = new ConcurrentHashMap<>();
13+
14+
String str = "ABCD ABCD ABCD";
15+
16+
for(char character:str.toCharArray()) {
17+
occurances.computeIfAbsent(character, ch -> new LongAdder()).increment();
18+
}
19+
20+
System.out.println(occurances);
21+
22+
23+
}
24+
25+
/*
26+
Map<Character, LongAdder> occurances = new Hashtable<>();
27+
28+
String str = "ABCD ABCD ABCD";
29+
for(char character:str.toCharArray()) {
30+
LongAdder longAdder = occurances.get(character);
31+
if(longAdder == null) {
32+
longAdder = new LongAdder();
33+
}
34+
longAdder.increment();
35+
occurances.put(character, longAdder);
36+
}
37+
38+
System.out.println(occurances);
39+
40+
*/
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.in28munites.ConcurrencyLocksAtomicityAndCollections;
2+
3+
import java.util.List;
4+
import java.util.concurrent.CopyOnWriteArrayList;
5+
6+
public class CopyOnWriteArrayListRunner {
7+
8+
//add - copy
9+
//get
10+
11+
public static void main(String[] args) {
12+
List<String> list = new CopyOnWriteArrayList<>();
13+
14+
//Threads - 3
15+
list.add("Ant");
16+
list.add("Bat");
17+
list.add("Cat");
18+
19+
//Threads - 10000
20+
for(String element:list) {
21+
System.out.println(element);
22+
}
23+
24+
// TODO Auto-generated method stub
25+
26+
}
27+
28+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.in28munites.ConcurrencyLocksAtomicityAndCollections;
2+
3+
public class Counter {
4+
private int i = 0;
5+
6+
synchronized public void increment() {
7+
i++;
8+
//get i
9+
//increment
10+
//set i
11+
}
12+
13+
public int getI() {
14+
return i;
15+
}
16+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.in28munites.ExceptionHandling;
2+
3+
public class CheckedExceptionRunner {
4+
5+
public static void main(String[] args) {
6+
/* try {
7+
someOtherMethod();
8+
Thread.sleep(2000);
9+
} catch (InterruptedException e) {
10+
e.printStackTrace();
11+
}*/
12+
//someOtherMethod1();
13+
someOtherMethod2();
14+
15+
}
16+
17+
private static void someOtherMethod2() throws RuntimeException{
18+
19+
}
20+
21+
private static void someOtherMethod() throws InterruptedException{
22+
Thread.sleep(2000);
23+
}
24+
25+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.in28munites.ExceptionHandling;
2+
3+
public class ExceptionHandlingRunner {
4+
5+
public static void main(String[] args) {
6+
method1();
7+
System.out.println("Main Ended");
8+
}
9+
10+
private static void method1() {
11+
method2();
12+
System.out.println("Method1 Ended");
13+
}
14+
15+
private static void method2() {
16+
String str = null;
17+
str.length();
18+
System.out.println("Method2 Ended");
19+
}
20+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.in28munites.ExceptionHandling;
2+
3+
public class ExceptionHandlingRunner2 {
4+
5+
public static void main(String[] args) {
6+
method1();
7+
System.out.println("Main Ended");
8+
}
9+
10+
private static void method1() {
11+
method2();
12+
System.out.println("Method1 Ended");
13+
}
14+
15+
private static void method2() {
16+
try {
17+
//String str = null;
18+
//str.length();
19+
int[] i = {1,2};
20+
int number = i[3];
21+
System.out.println("Method2 Ended");
22+
} catch (NullPointerException ex) {
23+
System.out.println("Matched NullPointerException");
24+
ex.printStackTrace();
25+
} catch (ArrayIndexOutOfBoundsException ex) {
26+
System.out.println("Matched ArrayIndexOutOfBoundsException");
27+
ex.printStackTrace();
28+
} catch (Exception ex) {
29+
System.out.println("Matched Exception");
30+
ex.printStackTrace();
31+
}
32+
}
33+
}

0 commit comments

Comments
 (0)