Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 62e97c2

Browse files
author
Rajeev Kumar Singh
committed
Java Threads and Runnables
0 parents commit 62e97c2

File tree

8 files changed

+168
-0
lines changed

8 files changed

+168
-0
lines changed

‎.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.DS_Store
2+
.idea
3+
out
4+
*.iml

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Java Concurrency/Multithreading Examples
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
3+
public class RunnableExample implements Runnable {
4+
5+
public static void main(String[] args) {
6+
System.out.println("Inside : " + Thread.currentThread().getName());
7+
8+
System.out.println("Creating Runnable...");
9+
Runnable runnable = new RunnableExample();
10+
11+
System.out.println("Creating Thread...");
12+
Thread thread = new Thread(runnable);
13+
14+
System.out.println("Starting Thread...");
15+
thread.start();
16+
}
17+
18+
@Override
19+
public void run() {
20+
System.out.println("Inside : " + Thread.currentThread().getName());
21+
}
22+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Created by rajeevkumarsingh on 08/05/17.
3+
*/
4+
public class RunnableExampleAnonymousClass {
5+
6+
public static void main(String[] args) {
7+
System.out.println("Inside : " + Thread.currentThread().getName());
8+
9+
System.out.println("Creating Runnable...");
10+
11+
Runnable runnable = new Runnable() {
12+
@Override
13+
public void run() {
14+
System.out.println("Inside : " + Thread.currentThread().getName());
15+
}
16+
};
17+
18+
System.out.println("Creating Thread...");
19+
Thread thread = new Thread(runnable);
20+
21+
System.out.println("Starting Thread...");
22+
thread.start();
23+
}
24+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* Created by rajeevkumarsingh on 08/05/17.
3+
*/
4+
public class RunnableExampleLambdaExpression {
5+
6+
public static void main(String[] args) {
7+
System.out.println("Inside : " + Thread.currentThread().getName());
8+
9+
System.out.println("Creating Runnable...");
10+
Runnable runnable = () -> {
11+
System.out.println("Inside : " + Thread.currentThread().getName());
12+
};
13+
14+
System.out.println("Creating Thread...");
15+
Thread thread = new Thread(runnable);
16+
17+
System.out.println("Starting Thread...");
18+
thread.start();
19+
20+
}
21+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Created by rajeevkumarsingh on 08/05/17.
3+
*/
4+
public class ThreadExample extends Thread {
5+
6+
@Override
7+
public void run() {
8+
System.out.println("Inside : " + Thread.currentThread().getName());
9+
}
10+
11+
public static void main(String[] args) {
12+
System.out.println("Inside : " + Thread.currentThread().getName());
13+
14+
System.out.println("Creating thread...");
15+
Thread thread = new ThreadExample();
16+
17+
System.out.println("Starting thread...");
18+
thread.start();
19+
}
20+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* Created by rajeevkumarsingh on 08/05/17.
3+
*/
4+
5+
public class ThreadJoinExample {
6+
public static void main(String[] args) {
7+
// Create Thread 1
8+
Thread thread1 = new Thread(() -> {
9+
System.out.println("Entered Thread 1");
10+
try {
11+
Thread.sleep(2000);
12+
} catch (InterruptedException e) {
13+
e.printStackTrace();
14+
}
15+
System.out.println("Exiting Thread 1");
16+
});
17+
18+
// Create Thread 2
19+
Thread thread2 = new Thread(() -> {
20+
System.out.println("Entered Thread 2");
21+
try {
22+
Thread.sleep(4000);
23+
} catch (InterruptedException e) {
24+
e.printStackTrace();
25+
}
26+
System.out.println("Exiting Thread 2");
27+
});
28+
29+
System.out.println("Starting Thread 1");
30+
thread1.start();
31+
32+
System.out.println("Waiting for Thread 1 to complete");
33+
try {
34+
thread1.join(1000);
35+
} catch (InterruptedException e) {
36+
e.printStackTrace();
37+
}
38+
39+
System.out.println("Waited enough! Starting Thread 2 now");
40+
thread2.start();
41+
}
42+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Created by rajeevkumarsingh on 08/05/17.
3+
*/
4+
public class ThreadSleepExample {
5+
6+
public static void main(String[] args) {
7+
System.out.println("Inside : " + Thread.currentThread().getName());
8+
9+
String[] messages = {"If I can stop one heart from breaking,",
10+
"I shall not live in vain.",
11+
"If I can ease one life the aching,",
12+
"Or cool one pain,",
13+
"Or help one fainting robin",
14+
"Unto his nest again,",
15+
"I shall not live in vain"};
16+
17+
Runnable runnable = () -> {
18+
System.out.println("Inside : " + Thread.currentThread().getName());
19+
20+
for(String message: messages) {
21+
System.out.println(message);
22+
try {
23+
Thread.sleep(2000);
24+
} catch (InterruptedException e) {
25+
e.printStackTrace();
26+
}
27+
}
28+
};
29+
30+
Thread thread = new Thread(runnable);
31+
32+
thread.start();
33+
}
34+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /