-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path_108_thread_constructors_in_java.java
More file actions
48 lines (48 loc) · 1.71 KB
/
_108_thread_constructors_in_java.java
File metadata and controls
48 lines (48 loc) · 1.71 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
44
45
46
47
48
/*
Below are some of the commonly used thread constructors in Java
1) Thread()
2) Thread(String name)
3) Thread(Runnable r)
4) Thread(Runnable r, String name)
*/
class myThread1 extends Thread{
public myThread1(String name){ // Thread(String name) constructor
super(name); // sets this thread's name as name, should be first statement in the constructor
System.out.println("The name is: "+name);
System.out.println("Hihi");
}
public void run(){
int i=0;
while(i<4000){
System.out.println("myThread1 is running.");
System.out.println("Dame da yoooo, Kirin. Dame da yo.");
i++;
}
}
}
class myThread2 extends Thread{
public myThread2(String name){ // Thread(String name) constructor
super(name); // sets this thread's name as name, should be first statement in the constructor
System.out.println("The name is: "+name);
System.out.println("Huhu");
}
public void run(){
int i=0;
while(i<4000){
System.out.println("myThread2 is running.");
System.out.println("Baka mittai.");
i++;
}
}
}
public class _108_thread_constructors_in_java {
public static void main(String[] args) {
myThread1 T1 = new myThread1("LOLOLOL");
myThread2 T2 = new myThread2("LILILIL");
// T1.start();
// T2.start();
System.out.println("The id of this thread is: "+T1.getId()); // every thread gets an ID
System.out.println("The id of this thread is: "+T2.getId()); // every thread gets an ID
// we can access this ID using the getID() function
}
}