-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadEx.java
More file actions
57 lines (40 loc) · 1.3 KB
/
ThreadEx.java
File metadata and controls
57 lines (40 loc) · 1.3 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
49
50
package com.google;
//first way of creating thread:
//by extending Thread class
public class ThreadEx extends Thread{
@Override
public void run() {
for (int i=0;i<5;i++){
//get name of current thread using getName() method
System.out.println("Thread "+this.getName()+" is running now : "+i);
}
}
public static void main(String[] args) {
//create object of 1st thread
ThreadEx t1=new ThreadEx();
//set name of t1 thread using setName method
t1.setName("FirstThread");
t1.start();//start t1 thread
t1.yield(); //pause t1 temporary
try {
t1.join(); //start t2 after executing t1
} catch (InterruptedException e) {
e.printStackTrace();
}
//create object of second thread
ThreadByImplementing t2=new ThreadByImplementing();
Thread th=new Thread(t2);
th.start();
}
}
//Seconf way of creating thread:
//by implementing Runnable interface
class ThreadByImplementing implements Runnable{
@Override
public void run() {
for (char i='A';i<'F';i++){
//getName() method is not present in Runnable interface so we cant access it here
System.out.println("Thread 2 is running now : "+i);
}
}
}