-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultiThreading1.java
More file actions
40 lines (32 loc) · 1.25 KB
/
MultiThreading1.java
File metadata and controls
40 lines (32 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
//MultiThreading.java
public class MultiThreading extends Thread{
public void run(){
System.out.println("Run Block is Running");
for(int i=0;i<=5;i++){
System.out.print(i);
}
}
}
//Main.java
public class Main {
public static void main(String[] args) {
MultiThreading MT1=new MultiThreading();
MT1.start(); //Used when every thread is executed
MultiThreading MT2=new MultiThreading();
MT2.start();
//!!Important!!
/*
My question is why do these threads print in a seemingly random order
each time? I know threads execute concurrently but would t1 not always
finish before t2 due to the sequential execution of the main process?
Answer: Calling start() on a Thread doesn't necessarily result in the thread
running immediately after. It is possible for other things to happen
in between your calling start() and the first line of your thread's
run() method actually being run. And even once your run() is actually
running, it's also possible that other things happen before, during,
or after your run() method finishes.
*/
//OUTPUTS:
// Produces Random Output.
}
}