E577 Add files via upload · veilair/java-development@502e01f · GitHub
[go: up one dir, main page]

Skip to content

Commit 502e01f

Browse files
authored
Add files via upload
1 parent 426db32 commit 502e01f

21 files changed

+696
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package methods;
2+
3+
/**
4+
* In the deep copy, we create a clone which is independent of original object and making changes in the cloned object should not affect original object.
5+
* So for deep copy we need to ensure all the member class also implement the Cloneable interface and override the clone() method of the object class.
6+
*/
7+
import java.util.ArrayList;
8+
9+
class DeepCopy1 {
10+
int x, y;
11+
}
12+
13+
class DeepCopy2 implements Cloneable {
14+
int a, b;
15+
DeepCopy1 c = new DeepCopy1();
16+
public Object clone() throws CloneNotSupportedException {
17+
DeepCopy2 t = (DeepCopy2)super.clone();
18+
t.c = new DeepCopy1();
19+
20+
return t;
21+
}
22+
}
23+
24+
public class DeepCopy {
25+
26+
public static void main(String[] args) throws CloneNotSupportedException {
27+
DeepCopy2 t1 = new DeepCopy2();
28+
t1.a = 10;
29+
t1.b = 20;
30+
t1.c.x = 30;
31+
t1.c.y = 40;
32+
33+
DeepCopy2 t3 = (DeepCopy2)t1.clone();
34+
t3.a = 100;
35+
t3.c.x = 300;
36+
37+
System.out.println(t1.a + " " + t1.b + " " + t1.c.x + " " +t1.c.y);
38+
System.out.println(t3.a + " " + t3.b + " " + t3.c.x + " " + t3.c.y);
39+
40+
}
41+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package methods;
2+
3+
public class FillInStackTraceMethod {
4+
5+
public static void main(String[] args) throws Throwable {
6+
FillInStackTraceMethod obj = new FillInStackTraceMethod();
7+
try {
8+
obj.showResult();
9+
} catch (Exception e) {
10+
System.out.println("Exception: "+e);
11+
e.printStackTrace();
12+
13+
}
14+
15+
}
16+
17+
public void showResult() throws Throwable {
18+
19+
}
20+
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package methods;
2+
3+
class Test {
4+
int x;
5+
Test(int i) { x = i; }
6+
Test() { x = 0; }
7+
}
8+
public class PassByValue {
9+
10+
11+
public static void main(String[] args) {
12+
Test t = new Test(10);
13+
14+
change(t);
15+
System.out.println("New Value :" + t.x);
16+
}
17+
18+
public static void change(Test t) {
19+
t.x = 10;
20+
}
21+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package methods;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
6+
public class ReturnListObjectClass {
7+
8+
public static List<Object> getDetails() {
9+
String name = "Pradeep";
10+
int age = 22;
11+
char gender = 'M';
12+
13+
return Arrays.asList(name, age, gender);
14+
}
15+
public static void main(String[] args) {
16+
List<Object> person = getDetails();
17+
System.out.println(person);
18+
}
19+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package methods;
2+
3+
/**
4+
* Whenever we use default implementation of clone method we get shallow copy of
5+
* object means it creates new instance and copies all the field of object to that
6+
* new instance and returns it as object type, we need to explicitly cast it back
7+
* to our original object. This is shallow copy of the object.
8+
*
9+
*/
10+
11+
import java.util.ArrayList;
12+
13+
class ShallowCopy1 {
14+
int x, y;
15+
}
16+
17+
class ShallowCopy2 implements Cloneable {
18+
int a, b;
19+
ShallowCopy1 c = new ShallowCopy1();
20+
public Object clone() throws CloneNotSupportedException {
21+
return super.clone();
22+
}
23+
}
24+
public class ShallowCopy {
25+
26+
public static void main(String[] args) throws CloneNotSupportedException {
27+
ShallowCopy2 t1 = new ShallowCopy2();
28+
t1.a = 10;
29+
t1.b = 20;
30+
t1.c.x = 30;
31+
t1.c.y = 40;
32+
33+
ShallowCopy2 t2 = (ShallowCopy2)t1.clone();
34+
35+
t2.a = 100;
36+
t2.c.x = 300;
37+
38+
System.out.println(t1.a + " " + t1.b + " " + t1.c.x + " " + t1.c.y);
39+
System.out.println(t2.a + " " + t2.b + " " + t2.c.x + " " +t2.c.y);
40+
}
41+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package multithreading;
2+
3+
import java.util.Random;
4+
import java.util.concurrent.Callable;
5+
import java.util.concurrent.FutureTask;
6+
7+
public class CallableExample implements Callable {
8+
9+
public Object call() throws Exception {
10+
Random generator = new Random();
11+
Integer randomNumber = generator.nextInt(5);
12+
Thread.sleep(randomNumber * 100);
13+
14+
return randomNumber;
15+
}
16+
17+
public static void main(String args[]) throws Exception {
18+
FutureTask[] randomNumberTasks = new FutureTask[5];
19+
20+
for(int i = 0; i < 5; i++) {
21+
Callable callable = new CallableExample();
22+
randomNumberTasks[i] = new FutureTask(callable);
23+
Thread t = new Thread(randomNumberTasks[i]);
24+
t.start();
25+
}
26+
for(int i = 0; i < 5; i++){
27+
System.out.println("FutureTask: "+randomNumberTasks[i].get());
28+
}
29+
}
30+
}
31+
32+
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package multithreading;
2+
3+
import java.util.concurrent.CountDownLatch;
4+
5+
public class CountDownLatchExample {
6+
7+
public static void main(String[] args) throws InterruptedException {
8+
9+
CountDownLatch latch = new CountDownLatch(4);
10+
11+
Worker first = new Worker(1000, latch, "WORKER-1");
12+
Worker second = new Worker(2000, latch, "WORKER-2");
13+
Worker third = new Worker(3000, latch, "WORKER-3");
14+
Worker fourth = new Worker(4000, latch, "WORKER-4");
15+
16+
first.start();
17+
second.start();
18+
third.start();
19+
fourth.start();
20+
21+
latch.wait(); // The main task waits for four threads
22+
System.out.println(Thread.currentThread().getName() + " has finised");
23+
}
24+
}
25+
26+
class Worker extends Thread {
27+
private int delay;
28+
private CountDownLatch latch;
29+
30+
public Worker(int delay, CountDownLatch latch, String name) {
31+
super(name);
32+
this.delay = delay;
33+
this.latch = latch;
34+
}
35+
@Override
36+
public void run() {
37+
try {
38+
Thread.sleep(delay);
39+
latch.countDown();
40+
System.out.println(Thread.currentThread().getName() + " finished");
41+
} catch (Exception e) {
42+
e.printStackTrace();
43+
}
44+
}
45+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package multithreading;
2+
3+
public class DaemonThread extends Thread {
4+
5+
public DaemonThread(String name) {
6+
super(name);
7+
}
8+
9+
public void run() {
10+
if(Thread.currentThread().isDaemon()) {
11+
System.out.println(getName() + " is Daemon thread");
12+
} else {
13+
System.out.println(getName() + " is User Thread" );
14+
}
15+
}
16+
public static void main(String[] args) {
17+
DaemonThread t1 = new DaemonThread("t1");
18+
DaemonThread t2 = new DaemonThread("t2");
19+
DaemonThread t3 = new DaemonThread("t3");
20+
21+
t1.setDaemon(true);
22+
t1.start();
23+
t2.start();
24+
t3.setDaemon(true);
25+
t3.start();
26+
}
27+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package multithreading;
2+
3+
import java.util.Scanner;
4+
import java.util.*;
5+
6+
public class InterThreadCommunication {
7+
8+
public static void main(String[] args) throws InterruptedException {
9+
10+
final PC pc = new PC();
11+
12+
Thread t1 = new Thread(new Runnable() {
13+
@Override
14+
public void run() {
15+
try {
16+
pc.produce();
17+
} catch (InterruptedException e) {
18+
e.printStackTrace();
19+
}
20+
}
21+
22+
});
23+
24+
Thread t2 = new Thread(new Runnable() {
25+
@Override
26+
public void run() {
27+
try {
28+
pc.consume();
29+
} catch (InterruptedException e) {
30+
e.printStackTrace();
31+
}
32+
}
33+
});
34+
35+
// Start both threads
36+
t1.start();
37+
t2.start();
38+
39+
// t1 finishes before t2
40+
t1.join();
41+
t2.join();
42+
}
43+
44+
public static class PC {
45+
public void produce() throws InterruptedException {
46+
// Synchronized block ensure only one thread running at a time
47+
synchronized(this) {
48+
System.out.println("Producer thread running");
49+
50+
// release the lock on shared resource
51+
wait();
52+
System.out.println("Resumed");
53+
}
54+
}
55+
56+
public void consume() throws InterruptedException {
57+
Thread.sleep(1000);
58+
Scanner s = new Scanner(System.in);
59+
60+
synchronized(this) {
61+
System.out.println("Waiting for return key");
62+
s.nextLine();
63+
System.out.println("Return key pressed");
64+
65+
notify();
66+
67+
Thread.sleep(2000);
68+
}
69+
}
70+
}
71+
72+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package multithreading;
2+
3+
public class JoinExample implements Runnable {
4+
5+
public void run() {
6+
Thread t = Thread.currentThread();
7+
System.out.println("Current Thread: " + t.getName());
8+
9+
System.out.println("Is Alive? " + t.isAlive());
10+
}
11+
public static void main(String[] args) {
12+
Thread t = new Thread(new JoinExample());
13+
t.start();
14+
15+
try {
16+
// Waits for 1000ms this thread to die
17+
t.join(1000);
18+
} catch (Exception e) {
19+
System.out.print(e);
20+
}
21+
22+
System.out.println("Current Thread: " + t.getName());
23+
System.out.println("Is Alive? " + t.isAlive());
24+
}
25+
}

0 commit comments

Comments
 (0)
0