File tree Expand file tree Collapse file tree 3 files changed +84
-0
lines changed
src/main/java/com/dev/thread Expand file tree Collapse file tree 3 files changed +84
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments