-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadsEx.java
More file actions
43 lines (36 loc) · 1.2 KB
/
ThreadsEx.java
File metadata and controls
43 lines (36 loc) · 1.2 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
41
42
43
class A extends Thread{ //this is a thread, not ordinary class!
public void run(){
for(int i=1;i<=10;i++){
System.out.println("Hi");
}
}
}
class B extends Thread{
public void run(){
for(int i=1;i<=10;i++){
System.out.println("Hello");
}
}
}
public class ThreadsEx{
public static void main(String args[]){
A objA = new A();
B objB = new B();
// objA.show();
// objB.show();
//MAKE IT WORK PARALLELy
//start is a method in thread class!
//To initialize a thread, use 'start()'
//Every class that extends Thread, must have 'run' method
objA.start(); // start method checks for 'run' in that class and exec it!
objB.start(); // start actually triggers thread to run 'run' method!
//Internally we have a concept 'Scheduler'
//"if you want to exec something, come to me first!"
//if all threads cant exec at same time, then it will go for time scheduler!
//based on allocated time for a thread, it will exec code!
//EXTRA CODE! (to find tot number of threads the code uses and tot cores present in CPU)!
System.out.println(Thread.activeCount());
int cores = Runtime.getRuntime().availableProcessors();
System.out.println("Available Cores/Threads: " + cores);
}
}