Skip to content

Commit ebc2124

Browse files
MultiThreading Examples without Docs
1 parent 5ca0a6d commit ebc2124

File tree

3 files changed

+118
-1
lines changed

3 files changed

+118
-1
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.ramaguru.amrita.cys.jpl.multithread;
2+
3+
/**
4+
* This code demonstrates the usage of multiple threads in Java.
5+
*
6+
* @author Ramaguru Radhakrishnan
7+
* @version 1.0
8+
*/
9+
public class MultiThreadMultiClass {
10+
11+
public static void main(String[] args) {
12+
// Create two thread objects
13+
MultiThread thread1 = new MultiThread();
14+
MultiThread thread2 = new MultiThread();
15+
16+
// Start the threads
17+
thread1.start();
18+
thread2.start();
19+
}
20+
}
21+
22+
/**
23+
* The MultiThread class extends Thread and represents a thread that prints numbers from 1 to 5.
24+
*/
25+
class MultiThread extends Thread {
26+
27+
/**
28+
* The run() method contains the code to be executed in the thread.
29+
* It prints the thread ID along with the numbers from 1 to 5 and sleeps for 1 second between each iteration.
30+
*/
31+
@Override
32+
public void run() {
33+
try {
34+
for (int i = 1; i <= 5; i++) {
35+
System.out.println("Thread " + Thread.currentThread().getId() + ": " + i);
36+
Thread.sleep(1000); // Sleep for 1 second
37+
}
38+
} catch (InterruptedException e) {
39+
System.out.println("Thread interrupted.");
40+
}
41+
}
42+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.ramaguru.amrita.cys.jpl.multithread;
2+
3+
/**
4+
* The MultiThreadMultipleClass class demonstrates the usage of multiple threads in Java.
5+
* It creates two threads of different classes, MultiThreadClass1 and MultiThreadClass2, and starts them.
6+
*
7+
* @author Ramaguru Radhakrishnan
8+
* @version 1.0
9+
*/
10+
public class MultiThreadMultipleClass {
11+
12+
public static void main(String[] args) {
13+
// Create two thread objects
14+
MultiThreadClass1 thread1 = new MultiThreadClass1();
15+
MultiThreadClass2 thread2 = new MultiThreadClass2();
16+
17+
// Start the threads
18+
thread1.start();
19+
thread2.start();
20+
}
21+
}
22+
23+
/**
24+
* The MultiThreadClass1 class extends Thread and represents a thread that prints numbers from 1 to 5.
25+
*/
26+
class MultiThreadClass1 extends Thread {
27+
28+
/**
29+
* The run() method contains the code to be executed in the thread.
30+
* It prints the thread ID along with the numbers from 1 to 5 and sleeps for 1 second between each iteration.
31+
*/
32+
@Override
33+
public void run() {
34+
try {
35+
for (int i = 1; i <= 5; i++) {
36+
System.out.println("Thread " + Thread.currentThread().getId() + ": " + i);
37+
Thread.sleep(1000); // Sleep for 1 second
38+
}
39+
} catch (InterruptedException e) {
40+
System.out.println("Thread interrupted.");
41+
}
42+
}
43+
}
44+
45+
/**
46+
* The MultiThreadClass2 class extends Thread and represents a thread that prints numbers from 1 to 20.
47+
*/
48+
class MultiThreadClass2 extends Thread {
49+
50+
/**
51+
* The run() method contains the code to be executed in the thread.
52+
* It prints the thread ID along with the numbers from 1 to 20 and sleeps for 500 milliseconds between each iteration.
53+
*/
54+
@Override
55+
public void run() {
56+
try {
57+
for (int i = 1; i <= 20; i++) {
58+
System.out.println("Thread " + Thread.currentThread().getId() + ": " + i);
59+
Thread.sleep(500); // Sleep for 500 milliseconds
60+
}
61+
} catch (InterruptedException e) {
62+
System.out.println("Thread interrupted.");
63+
}
64+
}
65+
}

lib/jpl_20cys383/src/com/ramaguru/amrita/cys/jpl/multithread/MultiThreadSimple.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
11
package com.ramaguru.amrita.cys.jpl.multithread;
22

3+
/**
4+
* This code demonstrates the usage of multiple threads in Java.
5+
*
6+
* @author Ramaguru Radhakrishnan
7+
* @version 1.0
8+
*/
39
public class MultiThreadSimple extends Thread {
410

11+
/**
12+
* The run() method contains the code to be executed in the thread.
13+
* It prints the thread ID along with the numbers from 1 to 5 and sleeps for 1 second between each iteration.
14+
*/
15+
@Override
516
public void run() {
617
try {
7-
// Code to be executed in the thread
818
for (int i = 1; i <= 5; i++) {
919
System.out.println("Thread " + Thread.currentThread().getId() + ": " + i);
1020
Thread.sleep(1000); // Sleep for 1 second

0 commit comments

Comments
 (0)