Skip to content

Commit 4d668af

Browse files
sequential vs threading, closes #76
1 parent 1780a05 commit 4d668af

File tree

8 files changed

+94
-0
lines changed

8 files changed

+94
-0
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
package com.learn.threading.daemon;public class App {
2+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.learn.threading;
2+
3+
public class DeamonThread {
4+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
package com.learn.threading.daemon;public class WorkerThread {
2+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.learn.threading.deadLock;
2+
3+
import com.learn.threading.util.TimerExit;
4+
5+
public class SimpleDeadLock {
6+
7+
public static void main(String[] args) {
8+
9+
// program will exit after 5000 milliseconds
10+
TimerExit timerExit = new TimerExit(5000);
11+
timerExit.exitProgram();
12+
try {
13+
Thread.currentThread().join();
14+
} catch (InterruptedException e) {
15+
e.printStackTrace();
16+
}
17+
System.exit(-1);
18+
}
19+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.learn.threading.sequentialVsThreading;
2+
3+
public class App {
4+
5+
public static void main(String[] args) {
6+
7+
// creating runners
8+
Runner r1 = new Runner("Runner1");
9+
Runner r2 = new Runner("Runner2");
10+
11+
System.out.println("Start --- Sequential Execution");
12+
// sequential executions
13+
r1.execute();
14+
r2.execute();
15+
System.out.println("End --- Sequential Execution");
16+
17+
// creating threads
18+
Thread t1 = new Thread(() -> {
19+
r1.execute();
20+
});
21+
Thread t2 = new Thread(() -> {
22+
r2.execute();
23+
});
24+
25+
System.out.println("Start --- Thread Execution");
26+
// thread executions
27+
t1.start();
28+
t2.start();
29+
30+
System.out.println("End --- Thread Execution");
31+
}
32+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.learn.threading.sequentialVsThreading;
2+
3+
public class Runner {
4+
String name;
5+
6+
public Runner(String name) {
7+
this.name = name;
8+
}
9+
10+
public void execute() {
11+
for (int i = 0; i < 10; i++) {
12+
System.out.println(this.name + "-" + i + " running in " + Thread.currentThread().getName());
13+
}
14+
}
15+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.learn.threading;
2+
3+
public class Incrementer implements Runnable{
4+
5+
public static int i;
6+
public static void increment(){
7+
i++;
8+
}
9+
10+
@Override
11+
public void run() {
12+
for (int j = 0; j <10; j++) {
13+
increment();
14+
}
15+
System.out.println("current value"+i);
16+
}
17+
18+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
package com.learn.threading.util;public class TimerExit {
2+
}

0 commit comments

Comments
 (0)