Skip to content

Commit d9a6fc1

Browse files
committed
Thread Group and Thread Class Methods
1 parent c3f3b19 commit d9a6fc1

10 files changed

+263
-0
lines changed

Multithreading/14_joinMethod.java

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

Multithreading/15_YieldMethod.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(getName());
6+
}
7+
}
8+
9+
class Main {
10+
11+
public static void main(String[] args) {
12+
13+
MyThread obj = new MyThread();
14+
obj.start();
15+
16+
obj.yield();
17+
18+
System.out.println(Thread.currentThread().getName());
19+
}
20+
}

Multithreading/16_setNameThread.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class MyThread extends Thread {
2+
3+
MyThread() {
4+
5+
}
6+
MyThread(String str) {
7+
8+
super(str);
9+
}
10+
public void run() {
11+
12+
System.out.println(getName());
13+
}
14+
}
15+
16+
class Main {
17+
18+
public static void main(String[] args) {
19+
20+
MyThread obj1 = new MyThread("C2W");
21+
obj1.start();
22+
23+
MyThread obj3 = new MyThread("C2W");
24+
obj3.start();
25+
26+
MyThread obj2 = new MyThread();
27+
obj2.start();
28+
29+
System.out.println(Thread.currentThread().getName());
30+
}
31+
}

Multithreading/17_setNameThread.java

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

Multithreading/18_ThreadGroup.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class MyThread extends Thread {
2+
3+
MyThread(ThreadGroup pThreadGP, String str) {
4+
5+
super(pThreadGP,str);
6+
}
7+
8+
public void run() {
9+
10+
System.out.println(this);
11+
}
12+
}
13+
14+
class Main {
15+
16+
public static void main(String[] args) {
17+
18+
ThreadGroup pThreadGP = new ThreadGroup("Core2Web");
19+
20+
MyThread obj1 = new MyThread(pThreadGP,"C/Cpp & DS");
21+
MyThread obj2 = new MyThread(pThreadGP,"Java DS");
22+
MyThread obj3 = new MyThread(pThreadGP,"Python ML");
23+
24+
obj1.start();
25+
obj2.start();
26+
obj3.start();
27+
}
28+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
class MyThread extends Thread {
2+
3+
MyThread(ThreadGroup threadGP, String str) {
4+
5+
super(threadGP,str);
6+
}
7+
8+
public void run() {
9+
10+
System.out.println(this);
11+
}
12+
}
13+
class Main {
14+
15+
public static void main(String[] args) {
16+
17+
ThreadGroup pThreadGP = new ThreadGroup("Core2Web");
18+
19+
MyThread obj1 = new MyThread(pThreadGP, "C");
20+
MyThread obj2 = new MyThread(pThreadGP, "Java");
21+
MyThread obj3 = new MyThread(pThreadGP, "Python");
22+
23+
obj1.start();
24+
obj2.start();
25+
obj3.start();
26+
27+
ThreadGroup cThreadGP = new ThreadGroup(pThreadGP, "Incubetor");
28+
MyThread obj4 = new MyThread(cThreadGP, "Flutter");
29+
MyThread obj5 = new MyThread(cThreadGP, "ReactJS");
30+
MyThread obj6 = new MyThread(cThreadGP, "SpringBoot");
31+
32+
obj4.start();
33+
obj5.start();
34+
obj6.start();
35+
}
36+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class MyThread implements Runnable {
2+
3+
public void run() {
4+
5+
System.out.println(Thread.currentThread());
6+
}
7+
}
8+
9+
class Main {
10+
11+
public static void main(String[] args) {
12+
13+
ThreadGroup threadGP = new ThreadGroup("C2W");
14+
15+
MyThread obj = new MyThread();
16+
17+
Thread t1 = new Thread(threadGP,obj,"C");
18+
Thread t2 = new Thread(threadGP,obj,"CPP");
19+
Thread t3 = new Thread(threadGP,obj,"JAVA");
20+
21+
t1.start();
22+
t2.start();
23+
t3.start();
24+
}
25+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class MyThread extends Thread {
2+
3+
public void run() {
4+
5+
System.out.println("Active Count of Thread : " + Thread.activeCount());
6+
}
7+
}
8+
9+
class Main {
10+
11+
public static void main(String[] args) {
12+
13+
MyThread obj = new MyThread();
14+
obj.start();
15+
16+
}
17+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class MyThread extends Thread {
2+
3+
MyThread(ThreadGroup pThreadGP, String str) {
4+
5+
super(pThreadGP, str);
6+
}
7+
public void run() {
8+
9+
System.out.println(Thread.currentThread().getThreadGroup().getName());
10+
}
11+
}
12+
13+
class Main {
14+
15+
public static void main(String[] args) throws InterruptedException {
16+
17+
ThreadGroup pThreadGP = new ThreadGroup("C2W");
18+
19+
MyThread t1 = new MyThread(pThreadGP, "C");
20+
MyThread t2 = new MyThread(pThreadGP, "Java");
21+
MyThread t3 = new MyThread(pThreadGP, "PyThon");
22+
23+
t1.start();
24+
t2.start();
25+
t3.start();
26+
}
27+
}
28+
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class MyThread extends Thread {
2+
3+
MyThread(ThreadGroup pThreadGP, String str) {
4+
5+
super(pThreadGP, str);
6+
}
7+
public void run() {
8+
9+
10+
}
11+
}
12+
13+
class Main {
14+
15+
public static void main(String[] args) throws InterruptedException {
16+
17+
ThreadGroup pThreadGP = new ThreadGroup("C2W");
18+
19+
MyThread t1 = new MyThread(pThreadGP, "C");
20+
MyThread t2 = new MyThread(pThreadGP, "Java");
21+
MyThread t3 = new MyThread(pThreadGP, "PyThon");
22+
23+
t1.start();
24+
t2.start();
25+
t3.start();
26+
27+
pThreadGP.list();
28+
}
29+
}
30+

0 commit comments

Comments
 (0)