Skip to content

Commit b87b2eb

Browse files
committed
Added Multi-thread executor
1 parent 59b5a32 commit b87b2eb

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.dev.thread;
2+
3+
public class InitialThread extends Thread{
4+
5+
@Override
6+
public synchronized void start() {
7+
super.start();
8+
}
9+
10+
@Override
11+
public void run() {
12+
System.out.println("Job-Thread- " + this.getId());
13+
14+
}
15+
16+
@Override
17+
public void interrupt() {
18+
super.interrupt();
19+
}
20+
21+
@Override
22+
public State getState() {
23+
return super.getState();
24+
}
25+
26+
@Override
27+
public long getId() {
28+
return super.getId();
29+
}
30+
31+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.dev.thread;
2+
3+
import java.util.List;
4+
import java.util.ArrayList;
5+
6+
public class MultiExecutor {
7+
8+
private List<Runnable> tasks;
9+
10+
public MultiExecutor(List<Runnable> tasks) {
11+
this.tasks = tasks;
12+
}
13+
14+
/**
15+
* Starts and executes all the tasks concurrently
16+
*/
17+
public void executeAll() {
18+
List<Thread> threads = new ArrayList<>(tasks.size());
19+
20+
for(Runnable task : tasks){
21+
Thread thread = new Thread(task);
22+
threads.add(thread);
23+
}
24+
25+
for(Thread thr : threads){
26+
thr.start();
27+
}
28+
}
29+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.dev.thread;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class ThreadExecutor {
7+
8+
public static void main(String[] args) {
9+
InitialThread thread = new InitialThread();
10+
System.out.println("before thread execution");
11+
thread.start();
12+
System.out.println("after thread execution");
13+
14+
List<Runnable> threads = new ArrayList<>();
15+
for (int i = 0; i < 4; i++) {
16+
threads.add(new Thread(() ->{
17+
System.out.println(Thread.currentThread().getId() + " - " + Thread.currentThread().getName());
18+
}));
19+
}
20+
21+
MultiExecutor multiExecutor = new MultiExecutor(threads);
22+
multiExecutor.executeAll();
23+
}
24+
}

0 commit comments

Comments
 (0)