Skip to content

Commit c3f3b19

Browse files
committed
setName, sleep and join Methods in Multithreading
1 parent 5a427ba commit c3f3b19

File tree

4 files changed

+125
-0
lines changed

4 files changed

+125
-0
lines changed

Multithreading/10_setName.java

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

Multithreading/11_SleepMathod.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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 Thread-0");
8+
9+
try {
10+
11+
Thread.sleep(1000,500000);
12+
} catch(InterruptedException obj) {
13+
14+
System.out.println("Exception Occure");
15+
}
16+
}
17+
}
18+
}
19+
20+
class Main {
21+
22+
public static void main(String[] args) throws InterruptedException {
23+
24+
MyThread obj = new MyThread();
25+
obj.start();
26+
27+
for(int i = 0;i < 10; i++) {
28+
29+
System.out.println("In Main");
30+
Thread.sleep(1000);
31+
}
32+
}
33+
}

Multithreading/12_joinMethod.java

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

Multithreading/13_joinMethod.java

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

0 commit comments

Comments
 (0)