Skip to content

Commit 705f9e1

Browse files
committed
Creating thread Using Thread Class
1 parent 800e772 commit 705f9e1

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed

Multithreading/1_UseThreadClass.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class MyThread extends Thread {
2+
3+
public void run() {
4+
5+
for(int i = 0; i < 10; i++) {
6+
7+
System.out.println("In Run");
8+
}
9+
}
10+
}
11+
class Main {
12+
13+
public static void main(String[] args) {
14+
15+
MyThread obj = new MyThread();
16+
obj.start();
17+
18+
for(int i = 0; i < 10; i++) {
19+
20+
System.out.println("In main");
21+
}
22+
}
23+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class MyThread extends Thread {
2+
3+
public void run() {
4+
5+
for(int i = 0; i < 10; i++) {
6+
7+
System.out.println("In run");
8+
try {
9+
10+
Thread.sleep(1000);
11+
} catch(InterruptedException obj) {
12+
13+
System.out.println("Exception Handled");
14+
}
15+
}
16+
}
17+
}
18+
class Main {
19+
20+
public static void main(String[] args) {
21+
22+
MyThread obj = new MyThread();
23+
obj.start();
24+
25+
for(int i = 0 ; i < 10; i++) {
26+
27+
System.out.println("In Main");
28+
try {
29+
30+
Thread.sleep(1000);
31+
} catch(InterruptedException ie) {
32+
33+
System.out.println("Exception Handled");
34+
}
35+
}
36+
}
37+
}

Multithreading/3_PrintThreadName.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class MyThread extends Thread {
2+
3+
public void run() {
4+
5+
System.out.println("Child Thread : " + Thread.currentThread().getName());
6+
}
7+
}
8+
class Main {
9+
10+
public static void main(String[] args) throws InterruptedException {
11+
12+
System.out.println("Main Thread : " + Thread.currentThread().getName());
13+
14+
MyThread obj1 = new MyThread();
15+
obj1.start();
16+
17+
MyThread obj2 = new MyThread();
18+
obj2.start();
19+
}
20+
}

0 commit comments

Comments
 (0)